Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
I want to submit code for others to use, where/how?

I am studying numpy and the function genfromtxt only works with ASCII which sucks for our utf-8 era, IMHO. So I wrote my version of the function compatible with utf-8 (not as cool as the original one but does the trick) how do I post it online for others to use and improve? I guess it should go in github, but how? Also how do I specify the version of Python and numpy there? Also my code is noob code so it could have crappy code. I tried to program it as good as I could but... You know, noobs...

/r/Python
https://redd.it/7e17g2
If I write & complete a Django web application can I then write a REST API for it?

I am completely new to Django & web development in general and I was wondering about it. Can I create my app first and when it is complete write an API on top of it? Or should I account for the API while I am developing the app?

Thank you in advance.

/r/django
https://redd.it/7e1r6x
uploading files to amazon s3

Can anyone give me a walkthrough of cors and policy setup on s3?

I've followed the default tutorials for settings up the AWS settings such as access key, bucket name, with boto and storages.

Collectstatic works fine but whenever logged in to a user account to say upload a profile image it says that the bucket is read-only a 500 error is hit.

My forms work fine everything is fine on local. Just an AWS issue moving to production.

Ideally, only users from the website would be able to upload images. SO, not allowing open access.

/r/django
https://redd.it/7e5vlw
How do I customize a user registration form so it only requires email and password fields?

I'm following this [tutorial](https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html) on making simple registration forms in Django. I'd like to make a user registration form that requires only two fields: "Email" and "Password." No second password field, just one.

So far, My `views.py` looks like this:

def register(request, template="register.html", redirect='/'):
if request.method=="POST":
form= RegisterForm(request.POST)
if form.is_valid():
form.save()
email= form.cleaned_data.get("email")
raw_password= form.cleaned_data.get("password1")
user= authenticate(email=email, password=raw_password)
login(request, user)
return redirect('/')
else:
form= RegisterForm()
return render(request, template, {"form": form})

`forms.py` has this class in it:

class RegisterForm(UserCreationForm):
email= forms.EmailField(label=_("Email"), max_length=254)

class Meta:
model= User
fields= ("email",)

`register.html` looks simple:

{% extends "base.html" %}
{% block main %}
<h2>Register</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
{% endblock main %}

In `urls.py`, I have this line in `urlpatterns`: `url("^register/$", views.register, name="register"),`.

But my registration forms looks like this, with an Email field and two Password fields: http://i.imgur.com/b359A5Z.png. And if I fill out all three fields and hit "Register," I get this error: `UNIQUE constraint failed: auth_user.username`.

Any idea why I'm getting this error? And how can I make sure my form only has two fields: Email and Password?

/r/django
https://redd.it/7e5rxp
How do I edge detect a live video with Canny edge detection?

I'm using python, and I'm kinda new as well.
Does anyone have a method for OpenCV live video feed edge detection?
Thanks

/r/Python
https://redd.it/7e5glv
[D] Machine Learning - WAYR (What Are You Reading) - Week 36

This is a place to share machine learning research papers, journals, and articles that you're reading this week. If it relates to what you're researching, by all means elaborate and give us your insight, otherwise it could just be an interesting paper you've read.

Please try to provide some insight from your understanding and please don't post things which are present in wiki.

Preferably you should link the arxiv page (not the PDF, you can easily access the PDF from the summary page but not the other way around) or any other pertinent links.

Previous weeks :

|1-10|11-20|21-30|31-40|
|----|-----|-----|-----|
|[Week 1](https://www.reddit.com/r/MachineLearning/comments/4qyjiq/machine_learning_wayr_what_are_you_reading_week_1/)|[Week 11](https://www.reddit.com/r/MachineLearning/comments/57xw56/discussion_machine_learning_wayr_what_are_you/)|[Week 21](https://www.reddit.com/r/MachineLearning/comments/60ildf/d_machine_learning_wayr_what_are_you_reading_week/)|[Week 31](https://www.reddit.com/r/MachineLearning/comments/6s0k1u/d_machine_learning_wayr_what_are_you_reading_week/)||
|[Week 2](https://www.reddit.com/r/MachineLearning/comments/4s2xqm/machine_learning_wayr_what_are_you_reading_week_2/)|[Week 12](https://www.reddit.com/r/MachineLearning/comments/5acb1t/d_machine_learning_wayr_what_are_you_reading_week/)|[Week 22](https://www.reddit.com/r/MachineLearning/comments/64jwde/d_machine_learning_wayr_what_are_you_reading_week/)|[Week 32](https://www.reddit.com/r/MachineLearning/comments/72ab5y/d_machine_learning_wayr_what_are_you_reading_week/)||
|[Week 3](https://www.reddit.com/r/MachineLearning/comments/4t7mqm/machine_learning_wayr_what_are_you_reading_week_3/)|[Week 13](https://www.reddit.com/r/MachineLearning/comments/5cwfb6/d_machine_learning_wayr_what_are_you_reading_week/)|[Week 23](https://www.reddit.com/r/MachineLearning/comments/674331/d_machine_learning_wayr_what_are_you_reading_week/)|[Week 33](https://www.reddit.com/r/MachineLearning/comments/75405d/d_machine_learning_wayr_what_are_you_reading_week/)||
|[Week 4](https://www.reddit.com/r/MachineLearning/comments/4ub2kw/machine_learning_wayr_what_are_you_reading_week_4/)|[Week 14](https://www.reddit.com/r/MachineLearning/comments/5fc5mh/d_machine_learning_wayr_what_are_you_reading_week/)|[Week 24](https://www.reddit.com/r/MachineLearning/comments/68hhhb/d_machine_learning_wayr_what_are_you_reading_week/)|[Week 34](https://www.reddit.com/r/MachineLearning/comments/782js9/d_machine_learning_wayr_what_are_you_reading_week/)||
|[Week 5](https://www.reddit.com/r/MachineLearning/comments/4xomf7/machine_learning_wayr_what_are_you_reading_week_5/)|[Week 15](https://www.reddit.com/r/MachineLearning/comments/5hy4ur/d_machine_learning_wayr_what_are_you_reading_week/)|[Week 25](https://www.reddit.com/r/MachineLearning/comments/69teiz/d_machine_learning_wayr_what_are_you_reading_week/)|[Week 35](https://www.reddit.com/r/MachineLearning/comments/7b0av0/d_machine_learning_wayr_what_are_you_reading_week/)||
|[Week 6](https://www.reddit.com/r/MachineLearning/comments/4zcyvk/machine_learning_wayr_what_are_you_reading_week_6/)|[Week 16](https://www.reddit.com/r/MachineLearning/comments/5kd6vd/d_machine_learning_wayr_what_are_you_reading_week/)|[Week 26](https://www.reddit.com/r/MachineLearning/comments/6d7nb1/d_machine_learning_wayr_what_are_you_reading_week/)||
|[Week 7](https://www.reddit.com/r/MachineLearning/comments/52t6mo/machine_learning_wayr_what_are_you_reading_week_7/)|[Week 17](https://www.reddit.com/r/MachineLearning/comments/5ob7dx/discussion_machine_learning_wayr_what_are_you/)|[Week 27](https://www.reddit.com/r/MachineLearning/comments/6gngwc/d_machine_learning_wayr_what_are_you_reading_week/)||
|[Week 8](https://www.reddit.com/r/MachineLearning/comments/53heol/machine_learning_wayr_what_are_you_reading_week_8/)|[Week 18](https://www.reddit.com/r/MachineLearning/comments/5r14yd/discussion_machine_learning_wayr_what_are_you/)|[Week 28](https://www.reddit.com/r/MachineLearning/comments/6jgdva/d_machine_lear
[AF] New to Flask. Best ways to deploy to EC2?

I'm pretty new to Flask overall. I recently converted a basic site of mine to use (basic) Flask. I was curious about the best/easiest ways to deploy a Flask app to EC2? I've heard Docker is an option, but I'm not exactly sure how it would work since I'm not super familiar with Docker. I've also heard that virtualenvs are a good (but not very simple?) method as well.

Anyways, any help is appreciated! Thanks!

**EDIT: Thanks for all of the suggestions everyone! I'm testing out AWS CodeStar right now. If that doesn't work I'll try Zappa or ELB!**

/r/flask
https://redd.it/7cpnrg
Vim Python IDE

/r/Python
https://redd.it/7e6pes
Using custom bootstrap instead of Django templates

I have created a web app using Django, and while I find the default Admin backend useful, its a headache to customize in certain cases. I want to develop a dashboard like interface and do away with Django's Admin templates. I want to end up with something like [this](https://adminlte.io/themes/AdminLTE/index2.html) I have a REST API in place using DjangoRestFramework, but I don't understand how this could work well with plain html pages. I still want Django WSGI to "serve" my pages through Apache. What's the best way to go with this?

/r/django
https://redd.it/7e1tk3
Python code is running too slow, do I need to find a way to allocate more memory to python process?

I have python code that can extract a zip file (tar.gz) . If I use the code, it takes a really long time (~1.5 hour) but if I unzip it directly using 7zip it takes less than 5 minutes, so I am guessing there is something impeding the processing power of python.

I am trying to run this code https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/udacity/1_notmnist.ipynb

for convenience, here a version of the code that doesn't require numpy, scripy, etc. EDIT. The formatting is messed up. In meanwhile just look at the stack over flow implementation.

https://stackoverflow.com/questions/47392697/python-code-is-running-too-slow-do-i-need-to-find-a-way-to-allocate-more-memory

> url = 'https://commondatastorage.googleapis.com/books1000/'

> last_percent_reported = None

> data_root = '.' # Change me to store data elsewhere
>
>
> def download_progress_hook(count, blockSize, totalSize):

> """A hook to report the progress of a download. This is mostly intended for users with

> slow internet connections. Reports every 5% change in download progress.

> """

> global last_percent_reported

> percent = int(count * blockSize * 100 / totalSize)
>
> if last_percent_reported != percent:

> if percent % 5 == 0:

> sys.stdout.write("%s%%" % percent)

> sys.stdout.flush()

> else:

> sys.stdout.write(".")

> sys.stdout.flush()
>
> last_percent_reported = percent
>
>
> def maybe_download(filename, expected_bytes, force=False):

> """Download a file if not present, and make sure it's the right size."""

> dest_filename = os.path.join(data_root, filename)

> if force or not os.path.exists(dest_filename):

> print('Attempting to download:', filename)

> filename, _ = urlretrieve(url + filename, dest_filename, reporthook=download_progress_hook)

> print('\nDownload Complete!')

> statinfo = os.stat(dest_filename)

> if statinfo.st_size == expected_bytes:

> print('Found and verified', dest_filename)

> else:

> raise Exception(

> 'Failed to verify ' + dest_filename + '. Can you get to it with a browser?')

> return dest_filename
>
>
> train_filename = maybe_download('notMNIST_large.tar.gz', 247336696)

> test_filename = maybe_download('notMNIST_small.tar.gz', 8458043)
>
> num_classes = 10
>
> def maybe_extract(filename, force=False):

> root = os.path.splitext(os.path.splitext(filename)[0])[0] # remove .tar.gz

> if os.path.isdir(root) and not force:

> # You may override by setting force=True.

> print('%s already present - Skipping extraction of %s.' % (root, filename))

> else:

> print('Extracting data for %s. This may take a while. Please wait.' % root)

> tar = tarfile.open(filename)

> sys.stdout.flush()

> tar.extractall(data_root)

> tar.close()

> data_folders = [

> os.path.join(root, d) for d in sorted(os.listdir(root))

> if os.path.isdir(os.path.join(root, d))]

> if len(data_folders) != num_classes:

> raise Exception(

> 'Expected %d folders, one per class. Found %d instead.' % (

> num_classes, len(data_folders)))

> print(data_folders)

> return data_folders

>
> train_folders = maybe_extract(train_filename)

> test_folders = maybe_extract(test_filename)

I am using 64-bit 3.6.3 python on a 64-bit windows 10 laptop, with 12 gigs of RAM.

/r/Python
https://redd.it/7e8nni
Taskmap: A python framework for managing task dependencies and executing coroutines across multiple cores
https://github.com/n-s-f/taskmap

/r/Python
https://redd.it/7eag9s
If I built almost exactly the same site using pure Django vs DRF + VueJS, what would be the performance differences ?

Say I have an online shop that gets about 1000 hits per day.

One version uses Django for the templating, one uses Vue with Django-Rest-Framework for everything under the hood.

What would be the performance differences ? What is the cost of templating with Django? Would the gap be significant enough to warrant changing between one or the other ? Or is the impact minimal, leaving the decision be based on other factors like UX ?

/r/django
https://redd.it/7ebqq4
How to save JSON API response in database?

Hi, I am new to django and am currently trying to connect to third-party JSON API, retrieve information and save it into the database. I am able to retrieve the data from views.py but I want to run a API query every 1 minute and save updated results into the database. Can anyone help me with it?

/r/django
https://redd.it/7e0olm
Deploying a Flask app into Docker Swarm (xpost /r/python)

[Link first because this is what everyone wants](https://github.com/justanr/flaskbb-swarm)

I recently set up a docker swarm in my home lab and I realized that just putting tooling (traefik, portainer, registry, etc) on it was silly. So I decided to document how I went about deploying a non-trivial application: [FlaskBB](https://github.com/sh4nks/flaskbb) -- which disclosure I'm a maintainer on. I'm also defining "non-trivial" as has multiple moving parts that work together in interesting ways (for some definition of interesting) -- in this case, there is the actual application container, a celery container, redis, a postgres database and finally [an nginx container](https://i.imgflip.com/1z9w62.jpg).

The entrypoint is Traefik running as a Swarm service, which then hands off traffic to the Nginx container which decides to serve either static content or pass to the uWSGI container (in the future, I plan on using frontend rules to partition this on the Traefik layer rather than the nginx layer). Docker networks are used to partition which services are allowed to talk to each other. Secrets and Configs are used to separate configuration logic from the code running in containers.

The nuts and bolts are specific to FlaskBB but hopefully can provide a template for usage with other applications. I tried to be thorough without delving down rabbit holes too much, but let me know if I missed something or should've been clearer some where. :)

/r/flask
https://redd.it/7cmwpz