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
[AF] How to render a template from a helper function?

Quick question, I need to check the existence of an object in the database in a lot of app routes, so I figured I'd just call a function which checks the said existence and returns an error template if it isn't found. However, when going to my site, 'error.html' is not rendered at all even though the bool is True. What am I missing?

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
foo()
render_template('succes.html')

def foo():
if some_bool_resulting_from_a_check:
return render_template('error.html')


/r/flask
https://redd.it/7cyion
What would be a standard modern python workflow?

I am working on linux, mainly using python for scripting and machine learning.

I am wondering if there is a standard workflow for python developers as of 2017.

Among others are these considered standard/widely used, why, and how?

* Virtual Environments / Virtual Environments Wrappers
* Alternative Interpreters / Notebooks (like ipython/jupyter)
* conda package manager and environments
* Version control
* Others...

What are you guys using ?

PS: Just found [this example](http://tdhopper.com.s3-website-us-east-1.amazonaws.com/conda/). Wondering if it is still up to date..

/r/Python
https://redd.it/7dqqu0
Is it against best practice to have multiple stylesheets?

Currently have 7 routes in my application and for each template I am inhereting a base layout and then importing a stylesheet unique to that template. Would it be a better practice to condense those style sheets into one style.css file? Or is it acceptable to use multiple?

The reason I have multiple is because some class names are identical with different attributes for each template.

/r/flask
https://redd.it/7cw8le
How to access custom setting from within main?

I am writing up a project with a file that contains some helpful functions to be used in the project. Some of these functions require access to a setting (API_KEY) that I put in `settings.py`. When I try to access this setting, I always get the following error:

AttributeError: module 'django.conf.global_settings' has no attribute 'API_KEY'

I *do* have access to *global* settings, but not the project-specific settings. E.g., in my project, I have DEBUG set to `True` and when I enter `settings.DEBUG` I am getting back `False`.

Here's the code I used to try to access it from within my file that is in the app folder (the project is called X and this is in X/Y/utils.py, where Y is the app name):


from django.conf import settings

if __name__ == "__main__":
import django
if not settings.configured:
settings.configure()
django.setup()
print(settings.API_KEY)

Note the above was written partly with the help of SO:
https://stackoverflow.com/questions/7867797/how-do-i-reference-a-django-settings-variable-in-my-models-py
https://stackoverflow.com/questions/32063881/django-python-manage-py-runserver-runtimeerror-settings-already-configured


/r/django
https://redd.it/7dye6a
[P] Fun Project: MSpaint to Terrain Map with GAN

I tried a fun little project to explore some of the possibilities of GANs and the results came out pretty good so I figured I'd share. The goal was to create a network which can take extremely simple images composed of various color regions (where each color corresponds to a particular type of terrain) and generate a realistic looking terrain map. The inputs being the sort of image an average human with no artistic ability could reasonably sketch out in MSpaint.

The GAN is based on AffineLayer's "pix2pix" code with a variety of changes. Primarily I increased the image size to 512x512 pixels and wrote completely new network structures. The generator is composed of an encoder/decoder pair which are mirrored 25 layer ResNets with skip connections between them. The generator loss was skewed strongly toward fooling the discrimintor to emphasize creating completely new terrain features rather than recreating the original image.

I used a terrain map of the entire Earth and randomly sampled 512x512 crops to generate my ground truth set. To create the seed images, these crops were quantized to a color palette of 5 colors and successive mode filters were applied to simplify the features. Each of the five colors corresponded with a different terrain type (blue - water, grey - mountains, green - forest/jungle, yellow - grassland/desert, brown - hills/badlands). In total, about 1300 image pairs were created that spanned the entire globe. The network was trained on these pairs for about 450 epochs (more or less a full day on a gtx1070) before it reached a point that I was happy with the results.

Now for the fun part: pictures. I've posted several image sets below. Each set is based on a single seed image which I drew in MSpaint with the various color regions recolored in different ways to show how the generator adapts the same image structure with different colorings. Notice how the network is able to invent features like lakes and rivers and islands. It also learned to handle the features of color regions and their transitions differently based on overall context.

https://imgur.com/a/mCJrA

https://imgur.com/a/OgY7k

https://imgur.com/a/hROZJ

https://imgur.com/a/nIYkp

Let me know if you have any questions.

/r/MachineLearning
https://redd.it/7dwj1q
Ordering of signals with m2m_changed

Hi everyone,

I've got an issue with my custom signals interacting with m2m_changed. It's frustrating because I've found little documentation on it.

I have a m2m_changed signal which triggers on any change on a specific model. I also have my own signal which should send an e-mail but unfortunately it triggers before the many-to-many field has been updated and thus giving me the wrong info to send in an e-mail.

I know it's not a good practice to rely on signal ordering but I'd really appreciate if you had any advice on this.

/r/django
https://redd.it/7dtjgf
What is the best way to install Python 3.6 on Ubuntu 16.04.1?

What is the best and most compatible way to install Python 3.6.x on Ubuntu 16.04? Should I just grab the source and install to /usr/local?

/r/Python
https://redd.it/7e08sv
I am brand spanking new to Python. Would like to know some of the best places to hang out online to get insight and just read about general things from developers. That is the best way to learn I’ve seen.



/r/Python
https://redd.it/7e0646
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