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
Passing variables within functions

Hello everyone,

I have few function which look something like this:

@login_required
def get_pages(request, startDate='2017-01-01', endDate='today'):
do_something..

@login_required
def get_speed(request, startDate='2017-01-01', endDate='today'):
do_something..

etc..

These functions get the data for the correct period and save it in my db. Now I want a new function which calls them changing the startDate & endDate parameter, so I can update metrics daily.

I tried something like this:

def update_last2days(requests):
get_speed('yesterday','today') # The API accepts yesterday & today as input format. Consider it as YYYY-MM-DD
get_pages('yesterday','today')
return redirect('/report?updated=true')

I also tried:

def update_last2days(requests):
get_speed(request, 'yesterday','today')
get_pages(request, 'yesterday','today')
return redirect('/report?updated=true')

But I keep getting `'str' object has no attribute 'user'` and I can't figure out why. How can I call a function with new parameters?

P.s. when I add the values `today` and `yesterday` to the main function all works just fine.

/r/django
https://redd.it/6ccwy3
Porting a program I wrote in python to be a web app. Have questions about how to structure data files.

Okay, so I recently learned python so that I could write a drop logging program for an MMO that I play. It might be sloppy (I don't know, this is my first coding project ever), but all of the features I initially envisioned function properly and I want other people to be able to use it now. Initially I was going to package it into an .exe and distribute that, but after some research and thinking on it for a bit, I've decided it makes more sense and will be better in the long run to make a web app with the same functionality for users to access.

My question is, my drop logger utilizes text files to pull drop table data for monsters from as well as save drop logs to. (I.e. a text file named "monster name droptable.txt" with a tab separated column/row structure for the items it can drop like item name, item quantity, etc...).

Drop logs that users create are also saved and loaded from txt files. I've gone through a few tutorials and it seems Flask would be perfect for making a simple web app that functions the same as my program. But, after looking into databases, I'm not sure how to store my data for retrieval. Is it still okay/efficient to just have static text files on the server and if I want to edit a drop table for a monster, or add a new monster, I can just edit/add a text file? Or should I store this information in a database. I feel like each monster would need it's own database, so is there a limit/problem with having a ton of databases?

As for user saved files. If a user wants to save a drop log to load at a later time, (or download to their computer) I knew I'd have to use a database to store this data (at least a filepath to their specific text drop log file), but should I store the drop logs themselves in databases?

Sorry if these are newbie questions, I'm just ready to jump into this and rebuild my program as a web app, but I don't even know which tutorials I need to be looking at because I don't know what's best. I don't want to do it one way only to find out a few days/weeks from now that it was a bad decision and backtrack to square one.

Thank you for any help!

/r/flask
https://redd.it/6ccncc
Anyone tried using Brython in Flask/Python apps? What were your thoughts?

[Brython](http://brython.info/) is a Python-to-JavaScript tool, for anyone unaware.

/r/flask
https://redd.it/6br4vb
Logging number of requests to a rest api

Hi Reddit, I am working on tracking the number of times our rest api endpoints are called over time. What are some best practices for doing this?

I'm thinking I could write some python to update a JSON file each time the API is called, however this API is called thousands of times a day and am afraid this might be way to expensive of a process to launch each time it's called.

Any help is appreciated!

/r/flask
https://redd.it/6bqf4e
Question: How to verify that a user is posting (POST) to a flask webapp only using my CLI?

I am building an application which send POST request to an endpoint. I want to block the user from manually posting the POST request using some tools. I want to make sure that the user is posting to the flask webapp from my python script. What is the best way to implement this?

/r/Python
https://redd.it/6cfi2i
Kalliope v0.4.4 is out !

Kalliope is a modular always-on voice controlled personal assistant designed for home automation.

This new release brings new features, fixes many bugs and new plugins (called neurons !).

You can check details, videos and the neuron market place on the github page : https://kalliope-project.github.io/

The target is a rasp3 on raspbian, but you can quickly test it on a Ubuntu. You just need a microphone and a speaker/heapphones.

Python 2.7, 3.4, 3.5 and 3.6 are supported.

The project is open source under the MIT licence.

Find sources on github : https://github.com/kalliope-project/kalliope



Here is the changelog for the v0.4.4:

- Fix: Uppercase in order/parameters/global variables are now handled correctly
- Fix: usage of integer in neuron parameters
- Fix: encoding with special character
- Refactor main controller. Use a LIFO to allow full usage of kalliope via API (even with neurotransmitter)
- Add a systemd script to start kalliope automatically
docker testing
- python 3 support 3.4, 3.5, 3.6
- Increase testing code coverage
- Fix: Raspberry performance. CPU usage from 120% to 15%
- Input value refactoring. "args" parameter replaced by jinja templating
- Review TTS overriding config in neuron declaration
- Fix: accapela TTS
- LED and mute button support for Raspberry Pi
- Player modularity


Enjoy !

/r/Python
https://redd.it/6cgwic
I'm having problems with Django/nginx/uwsgi and uploading files larger than ~4MB

So, I don't know if this is the correct location to ask these questions, but I'm, well, kinda hopeless now. I've read through halve the internet for weeks without any luck.

I'm *very new* to hosting Django applications (or actually, I'm new to hosting and Linux in general too) so maybe I keep doing something stupid. Anyway, the problem:

I'm working on a (personal) Django website. In the Django admin, there's a page where you can upload multiple images and a optional pdf. So far so good. It works flawlessly on my local machine. However, when I run this on the server, things get weird.

When the *total* request size is smaller than ~4MB (e.g. 1 pdf of 2MB, or 4 images of 700KB etc.) everything works fine. When the request size is larger, the files get saved on the server without a single problem. **But**, they then become inaccessible and I get nginx 403 errors on those files. The Linux permissions are actually different. A 'good' file gets 664 as permissions, a 'bad' file only has 600.

I'm having this problem for weeks now and I'm so frustrated now. To the point that I'm just getting demotivated to even work on it. I've tried so many things, even completely resetting the whole server multiple times, but this stupid ass problem just keeps re-appearing. If *anyone* has the solution or the golden tip, I'd be sooo grateful.

Thanks for reading! :)

-----------

Running on a Linode server with:
Ubuntu 17.04, Python 3, Django 1.11, Nginx, Uwsgi

/r/django
https://redd.it/6cfg3c
Render? Redirect? When validating form...

Hello, so I'm few week in with Flask, it's real fun, I enjoy it quite a bit. But there are still things I don't understand.

Now I'm fighting with this:

I've got a page `issue_status.html` which contains 2 tables with data acquired from a database. This is a route to which I link my links.

@app.route('/issue_status/')
def issue_status():
cur, db = get_db(cursor=True)
issues = cur.execute("SELECT * FROM issues").fetchall()
resolved, unresolved = [], []

for row in issues:
issue = row[1]
found = cur.execute("SELECT * FROM resolved_issues WHERE issue = ?", [issue]).fetchall()
if len(found) > 0:
resolved.append(row)
else:
unresolved.append(row)

all_issues = {'resolved': resolved, 'unresolved': unresolved}
today = datetime.datetime.today().strftime('%Y-%m-%d')
return render_template('issue_status.html', all_issues=all_issues, today=today)

The page contains a form too for adding new issues. It's in the same `issue_status.html` page. When user clicks on `submit`, it would go to this function:

@app.route('/add_issue/', methods=['GET', 'POST'])
def add_issue():
if not session.get('logged_in'):
abort(401)

cur, db = get_db(cursor=True)
issue = request.form.get('issue')
version = ""
date_resolved = ""

existing = cur.execute("SELECT * FROM issues WHERE issue = ?", [issue]).fetchall()
if len(existing) > 0:
flash("This issue already exists...", 'danger')
return redirect(url_for('issue_status'))
....
....
....
flash('New issue was successfully added into database.', 'success')
return redirect(url_for('issue_status'))

**The problem:** I wanted to add to this function **form validation** but don't know how/what to return if the validation fails.

Because when I render the page, I'm missing the data parsed to `issue_status.html` from `issue_status` function. And when I tried to redirect to `issue_status`, that failed somehow too.

I would like to somehow only show under each field (Issue number, username, Description) a form invalid error without refreshing the page. How to do this elegantly?

If you want to see details what I have till now, the WebApp is at [github.com](https://github.com/SonGokussj4/beta-issues)

Any help would be appreciated.

[Screenshot](http://i.imgur.com/zKXV17W.jpg) for visual aid.

/r/flask
https://redd.it/6cidqh
Just installed django, but get ModuleNotFoundError when I run "django-admin --version" (traceback indluded)

Traceback (most recent call last):
File "C:\Users\X\AppData\Local\Programs\Python\Python36-32\Scripts\django-admin-script.py", line 11, in <module>
load_entry_point('django==1.11.1', 'console_scripts', 'django-admin')()
File "c:\users\x\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 560, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "c:\users\x\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2648, in load_entry_point
return ep.load()
File "c:\users\x\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2302, in load
return self.resolve()
File "c:\users\x\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2308, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "c:\users\x\appdata\local\programs\python\python36-32\lib\site-packages\django\__init__.py", line 3, in <module>
from django.utils.version import get_version
ModuleNotFoundError: No module named 'django.utils'

/r/django
https://redd.it/6chwky
Rest Framework URL Config

I've set up rest_framwork in my Django app in an attempt to make an api for my project. Issue is I run into this error (AttributeError: 'RegexURLResolver' object has no attribute 'default_args')
when trying to run the app. Anybody else have this problem?


/r/django
https://redd.it/6cjyrt
So atom, pycharm, and visual studio have a thing where you type partial word such as str, and it shows "started" or "substraction" for auto complete choices. Is there an addon, or other editor that also shows a short definition of each and quickly accessible?

Say you float a mouse over it or toggle a key while hovering one of the choices and it provides a short description with a hover tag.

Example here: https://www.w3schools.com/tags/tag_abbr.asp

/r/Python
https://redd.it/6ckjde
Need Django REST Framework Suggestion

I am trying to achieve this request body and response body

POST /issues/
> {
"Issue": {
"title": "python",
"owner": "test",
"description": "test",
"status": "test",
"projects": "test"
}
}


GET /issues/

> {
"Issue": [
{
"id": 1,
"title": "test",
"owner": "test",
"description": "test",
"status": "test",
"projects": "test"
},
{
"id": 2,
"title": "test",
"owner": "test",
"description": "test",
"status": "test",
"projects": "test"
}, .... ]}


The code in views.py


class CreateView(generics.ListCreateAPIView):

queryset = Issue.objects.all()

serializer_class = IssueSerializer

def list(self, request):
queryset = self.get_queryset()
serializer = IssueSerializer(queryset, many=True)
return Response({"Issue" : serializer.data})

def create(self, request):

serializer = self.get_serializer(data=request.data['issue'])
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)



is there a better way to do this(ignore the tabs)?

/r/djangolearning
https://redd.it/6chhs5
Do you use Cython?

I watched today Alex Orlov's PyCon 2017 presentation "Cython as a Game Changer for Efficiency" ([link](https://www.youtube.com/watch?v=_1MSX7V28Po)). I tried it years ago but I've never used it actually. Do you use Cython? It hurts portability but it can greatly improve efficiency.

/r/Python
https://redd.it/6cific
Autopopulating grid layout?

I'm just getting into wagtail, getting back into webdev. Last time I was doing web stuff, there were no frameworks, so it is a bit of a learning curve for me.

I'm wondering if there is an easy way(i.e package or template) that would allow for a grid stye layout of stores/posts, which would auto update each time a new post was made, similar to the mroe popular news/blog sites like nerdist or whatever.

Or is this something that has to be built from scratch?

/r/django
https://redd.it/6clzqo
CSRF Verification Failure on Localhost?

This is an extension on my last topic https://www.reddit.com/r/django/comments/6c1q2c/added_google_recaptcha_and_i_get_ssl_certificate/. I didn't really get a solution to.

I now tried a different technique and get a different error message than from all the other stuff I've done before. I followed instructions from this website on setting up stunnel: http://userpath.co/blog/a-simple-way-to-run-https-on-localhost/

and then used this command:
`HTTPS=on python manage.py runserver`

I get this message: https://pastebin.com/JXWVWNJq

My contact form html template has a csrf token
https://pastebin.com/Pcnmw3pA

Of course the major question is what am I doing wrong?
Second question is can https be run on localhost?

If I try https://localhost:8000 I get "This site can’t provide a secure connection localhost sent an invalid response." So I understand that the 403 is saying that my website is unsecure, but the redirect to google recaptcha is.


/r/django
https://redd.it/6chqsz
Structuring Python code

A while back there was a link posted aimed at beginners on how to structure Python code, files etc... Does anyone happen to have a link discussing this?

Thanks in advance

/r/Python
https://redd.it/6cmjj4
Django with Docker and Pycharm

Is anyone using a Django project inside a docker container in the Pycharm IDE?
I tried it but I can't figure out how to setup Pycharm for using the python interpreter.
Maybe someone could help me?

/r/django
https://redd.it/6cmy62