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
Abstract Classes

Im not sure I fully understand the usages of abstract classes. I know that they are useful when you want subclasses to inherit repeating fields, but how does this make working with repeating fields useful despite just making you type less? What kind of special actions can you perform on abstract classes? Am I missing something

/r/django
https://redd.it/7efjuk
[AF] Passing Python variables on the server to React component

Hi folks, a bit late to the party.

A bit of background: I'm practising with Flask + React in [this repo](https://github.com/CamiloGarciaLaRotta/fullstack_app_boilerplate/tree/vars_as_props).

Here is my issue on [SO](https://stackoverflow.com/questions/47256340/flask-passing-python-variables-on-the-server-to-react-component?noredirect=1#comment81469485_47256340).

But I figured I could ask you guys, in a more general sense, what are the heuristics when one wants to pass variables from the server side to a component at the first rendering of the html template:

* Do devs usually avoid this situation altogether?
* Is it advised to use other component mounting approaches for other than through a main index.jsx?
* What are the DO's and DONT's when working in a mixed environment like this? (i.e. Flask using templates and all the jsx components linked to the html in a single bundle)



Thank you very much in advance,

any advice is appreciated


/r/flask
https://redd.it/7cmsb3
What's everyone working on this week?

Tell /r/python what you're working on this week! You can be bragging, grousing, sharing your passion, or explaining your pain. Talk about your current project or your pet project; whatever you want to share.


/r/Python
https://redd.it/7ei2o7
Best strategies for incorporating data from a RESTful api: forms vs serializers?

**tl;dr**
When pulling data into my django database from an api, should I use forms, or a [django rest framework](http://www.django-rest-framework.org/) serializer?

**What I did**
Based on some discussion in [another thread](https://www.reddit.com/r/django/comments/7e0olm/how_to_save_json_api_response_in_database/), I built an ultra-simple example of how to extract data from an api, in this case the api for the video game Destiny 2:
https://github.com/cortical-iv/restit

Note this is essentially a port of UltimateDjango's [How to Consume REST APIs with Django & Python Requests](https://ultimatedjango.com/blog/how-to-consume-rest-apis-with-django-python-reques/).

My goal was to follow Section 12.1 of Two Scoops, and *Validate all incoming data with django forms.* But what I actually did was a two step process that I'm now thinking was sort of dumb (I pasted the code below the fold):

1. Bind data about path parameters in the url into a form.
2. After extracting the needed data from the response to `requests`, use a django rest framework *serializer* to validate and save that data.

Now that this is up and working, I realize I didn't actually really follow the advice from Two Scoops of importing through forms. I imported required *path* information (user name) through a form, but then did all the heavy lifting with the serializer. I now think it would be better to replace step 2 with another form (a SaveUser form) that itself includes all the fields I need in my database.

**Why I don't like what I did**
Why am I thinking my approach smells? A couple of reasons:

1. The serializer is overkill. I shouldn't need to invoke the django rest framework to do what I am doing. Why not stick with django forms until I need something more powerful?
2. I am doing so much processing on the Destiny 2 end points, that I can't even just apply the serializer to the output of `requests`. This may not be obvious from the simple example at my project.
3. Forms are awesome and I'm not even using them. Once I make some fields unique, calling `serializer.save()` just triggers a validation error. OTOH, using forms, you can feed it an instance to simply *update* the db row without having to do a lot of extra work. Forms have lots of cool stuff like this so it isn't clear, again, why a serializer from DRF is that helpful.

On the other hand, I am a django noob, so maybe there are benefits to pulling data in via a serializer that I don't appreciate. So I'm asking for advice: when pulling data from an api into a db (using `requests`), should I use a serializer, or a form? If a form, which I'm starting to think is the right answer, I'm a little confused about why the project I copied, from UltimateDjango, even used a serializer?

What conditions for that embedly api consumer made it smarter in his case, but not my case?

-----
Here is the `save_user` function I'm using, which contains the main logic:

def add_user(save_user_form):
"""
Validate form data containing 'display_name', request their info from server,
and pull in via a serializer. Returns dictionary with two keys:
'flag': 1 if successful, 0 if not, and None if that user is not a D2 player in PS4.
'message': message about the outcome (success, validation error, etc).
"""
add_result = {'flag': 0, 'message': ''}
if save_user_form.is_valid():
user_name = save_user_form.cleaned_data['display_name']
search_url = search_destiny_player_url(user_name)
search_response = requests.get(search_url, headers = {"X-API-Key": D2_KEY})
user_data = search_response.json()['Response']
if user_data:
submission_data = {'display_name': user_data[0]['displayName'], 'user_id': user_data[0]['membershipId']}
try:
serializer = SearchPlayerSerializer(data = submission_data)
print(repr(serializer)) #for debu
gging
if serializer.is_valid():
serializer.save() #save data to database
add_result['flag'] = 1
add_result['message'] = 'Successfully added {0}'.format(user_name)
else:
msg = "Invalid serializer. serializer.errors: {0}".format(serializer.errors)
add_result['message'] = msg
except Exception as e:
msg = "Exception using serializer: {0}. Exception type: {1}.".format(e, e.__class__.__name__)
add_result['message'] = msg
else:
msg = "'{0}' is not a Destiny2 player on PS4".format(user_name)
add_result['message'] = msg
else: #not valid
msg = "save_user_form not valid. error: {0}".format(save_user_form.errors)
add_result['message'] = msg

return add_result




/r/django
https://redd.it/7eigs7
username collisions

We are building a site where our users upload files. Django's API uses the 'username.'

When we handle registration we just use email. It seems that Django (or maybe it is AllAuth) just truncates the email to build the username:

email: name@gmail.com --> username: name

This is fine for the most part. And Django may even handle username collisions because because it actually works off of the ID and just displays 'username' for human friendly reasons (IDK).

The issue I foresee is as follows: If we build file paths based on username, e.g. all of user1's files are in files/user1, we have a potential collision because at this point, on say AWS S3 we are no longer using Django's (potential) collision handling.

TL DR: what happens if we have two users, one with username1@gmail.com and the other with username1@yahoo.com AND Django sets username to be a truncated email AND we build file paths off of username?

/r/django
https://redd.it/7emdcg
[D] Optimization Algorithms: Math and Code

A colleague of mine had mentioned that they were getting asked quite a few questions about optimization algorithms in their interviews for deep learning positions. I decided to make a quick post about the important ones over at 3dbabove: https://3dbabove.com/2017/11/14/optimizationalgorithms/

/r/MachineLearning
https://redd.it/7ehxky
How to create multiple user registration forms with flask-user

Hello
I understand that flask-user allows users with various roles to login. Based on the different roles that are available to users, each user can access routes that are available to that user and role.


However, I am trying to set up multiple registration pages for each user-role.
E.g. users with admin role have one signup page which asks for a lot of information up front , whereas users with regular role will have a simplified signup page just asking for basic details.
How can this be achieved using flask-user.


One good example would be Employers and Candidates on a job portal. How can we have 2 different forms - one for employers to sign up and another one for candidate sign up - for example..

Thanks

/r/flask
https://redd.it/7b5z77
Our Jupyter/IPython client for iPad now includes introductory notebooks on Python, NumPy, Matplotlib and SciPy that you can run without any preliminary configuration. Sign up for beta to run on your iPad!
https://juno.sh/juno-v-1-0-6-beta/

/r/IPython
https://redd.it/7embl0
I want to learn Python web development but I'm not sure if I should learn Flask or Django framework.



/r/Python
https://redd.it/7ena2q
Integrate bar chart in Flask app with database

I created a Flask app with a flask-sqlalchemy database. I would like one of the pages to show a bar chart which fetches data from said database, but Google hasn't helped much so far. Maybe someone has some hot tip? :)

/r/flask
https://redd.it/7eps8j
What is the design decision for different clients (e.g., jupyter) to share the same ipython kernels?

WHat the problem with it?

/r/IPython
https://redd.it/7ek04k
Can a admin inline access the parent attributes?

class ChildInline(admin.TabularInline):
model = # I want this to change depending on the Parent's model selection

class ParentAdmin(admin.ModelAdmin):
inlines = [ChildInline]

/r/django
https://redd.it/7et5eg