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
Python packages for writing better code

It would be interesting to curate a list of tools that help us write better Python code and save us time. With the exception of the version control tools, everything below is a Python package.

​

**Testing**

Writing and running tests makes it easier to develop robust code.

* [pytest](https://docs.pytest.org/en/latest/) (3,594 stars) - Popular testing framework, can run doctests too.
* [hypothesis](https://hypothesis.readthedocs.io/en/latest/) (3,223 stars) - Property-based testing, e.g. testing `f(a, b) = f(b, a)` for every `a, b`.

**Code linting and and formatting**

Code linting alerts of style violations, while a code formatter also automatically fixes the code.

* [flake8](http://flake8.pycqa.org/en/latest/) (497 stars) - Checks the code for PEP8 violations.
* [black](https://github.com/ambv/black) (7,552 stars) - Automatically formats code, saving you time.

**Documentation**

Tools for documentation, which automate the documentation process.

* [sphinx](http://www.sphinx-doc.org/en/master/) (2,376 stars) - Build docs to html, pdf and other formats. Automatically generate docs from code.

**Version control**

Version control allows going back to checkpoints, creating development branches, cooperating, etc.

* [git](https://git-scm.com/downloads) \- Popular version control tool.
* [github](https://github.com/) \- A platform for projects under git source control. Cooperation and community.

​

​

The above are tools that make my life easier when writing code. There are probably many tools that I do not know about, which could potentially save me even more time and make my code

/r/Python
https://redd.it/ao52qn
Wrote a python script to convert video to ascii videos (with audio)
https://youtu.be/Uh2_dVDH9s4

/r/Python
https://redd.it/aoc0t7
Looking for help with a process.

I have a variable in the form:

y = m[i]*t + b[i]

where I have values for m and b stored in lists. I'm interested in using a loop (or any other suggestion) to have the variable expand, similar to a summation.

y = m[0]*t + b[0] + m[1]*t + b[1] + ... + m[n]*t + b[n]

I would like to have the code accomplish the expansion for me as the goal is to have the number n determined by a user. It's similar to Fourier coefficients.

I greatly appreciate any tips, help or suggestions in this matter. Sorry if I committed any faux-pas up to this point as well.

/r/Python
https://redd.it/aoczku
How a production ready Dockerfile for Django projcet should look like?

It is clear that Docker is a buzzword for developers and I want to use it for my Django projects. The problem is that I can't find any material to learn how to correctly write a production ready Dockerfile for a Django project. Almost in every tutorial I see something like this in Dockerfile they write: **command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"**

And they call it "Production ready Dockerfile". What the f\*ck?! You don't run your server with [manage.py](https://manage.py) runserver command in production. Who the hell does?! I am freaking out that how it is possible that everyone loves Docker, everyone uses it, but no one teaches it correctly. So I need your help people to, get out this f\*cking situation. Thanks in advance.

/r/django
https://redd.it/aobpp8
Gita: a command-line tool to manage multiple repositories

I wrote a command-line tool to manage multiple repositories called [gita](https://github.com/nosarthur/gita). It can do two things

* display the status of multiple git repos such as branch, modification, commit message side by side
* delegate git commands/aliases from any working directory

[screenshot](https://raw.githubusercontent.com/nosarthur/gita/master/screenshot.png)

The color coding represents the relationship between local and remote branches, and the extra symbols (+, \*, and \_) denote the dirtyness of edits.

To install, simply run `pip3 install -U gita`

/r/Python
https://redd.it/aodcvh
IntegrityError: Duplicate Primary Key issue with postgres

Hi,

The database has a few entries that were removed which leaves a gap in the Primary Keys. When testing backup and restore on my dev server, the Primary Key was reset to 1, fine, this is to be expected. However, running:

python manage.py sqlsequencereset microblog

Resets it to the next free number, This is no good, as the ID sequence in the current test db is:

1,2,7,10,12,13,14,17,23,24,25,26

so resetting to 3, just means this will fail in 4 database entries time. I have also tried:

psql
SELECT setval('microblog_blog_id_seq', (SELECT MAX(id) FROM microblog_blog)+1);

Whilst it returns the correct number in the console, and I am able to add another post, looking at the database table in pgAdmin, shows that it has picked the number 3 again, not 27 which is what I want it to do. Literally, I want it to treat any number < max as a no go value.

How can I do this? and if anyone knows why this is not the default behaviour I would be interested to know the reason why?

&#x200B;

/r/django
https://redd.it/aofxtc
Django-Channels: DLL load failed: %1 is not a valid Win32 application

Hi All!

After adding channels app on my project, I started having this error when running [manage.py](https://manage.py) runserver:

&#x200B;

\---------------------------------------------------------------------------------

File "D:\\Python37\\lib\\site-packages\\channels\\[apps.py](https://apps.py)", line 6, in <module>

import daphne.server

File "D:\\Python37\\lib\\site-packages\\daphne\\[server.py](https://server.py)", line 27, in <module>

from twisted.internet.endpoints import serverFromString

File "C:\\Users\\...\\AppData\\Roaming\\Python\\Python37\\site-packages\\twisted\\internet\\[endpoints.py](https://endpoints.py)", line 41, in <module>

from twisted.internet.stdio import StandardIO, PipeAddress

File "C:\\Users\\...\\AppData\\Roaming\\Python\\Python37\\site-packages\\twisted\\internet\\[stdio.py](https://stdio.py)", line 30, in <module>

from twisted.internet import \_win32stdio

File "C:\\Users\\...\\AppData\\Roaming\\Python\\Python37\\site-packages\\twisted\\internet\\\_win32stdio.py", line 9, in <module>

import win32api

ImportError: DLL load failed: %1 is not a valid Win32 application

\-----------------------------------------------------

&#x200B;

It lookslike this error is raised by twisted, which is apparently changing the work directory after importing daphne server.


I had a previous installation of Python on this location, which is not active anymore.

&#x200B;

Does any of you know how to correct this?

/r/django
https://redd.it/aogucs
Jupyter complains of no anaconda3 when anaconda3 was never on my system

I'm trying to set up Jupyter, and for the most part it works fine. However, I'm getting this error:

Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/tornado/web.py", line 1592, in _execute
result = yield result
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 1133, in run
value = future.result()
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 1141, in run
yielded = self.gen.throw(*exc_info)
File "/usr/lib/python3.7/site-packages/notebook/services/sessions/handlers.py", line 73, in post
type=mtype))
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 1133, in run
value = future.result()
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 1141, in run
yielded = self.gen.throw(*exc_info)
File "/usr/lib/python3.7/site-packages/notebook/services/sessions/sessionmanager.py", line 79, in create_session
kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
File "/usr/lib/python3.7/site-packages/tornado/gen.py", line 1133, in run
value = future.result()


/r/IPython
https://redd.it/aogr33
Only allow a route to be accessed from within the flask app

I followed a couple of tutorials and now have a form and a /create-user route which accepts the data and creates a user in the database.

What is the correct way to protect this route so that it only accepts a call from within the flask app?

otherwise, can't people just post directly to it and create users using postman or insomnia.?

/r/flask
https://redd.it/aohws1
Web GUI for Apache logs parser with Flask for beginners

Hi, everyone!

&#x200B;

I made a video on how to make a Web GUI with Flask framework for a Python script using the Apache log parser example.

Detailed explanation for beginners.

&#x200B;

Covered topics:

\- Tools for regular expressions validation

\- What is 'if \_\_name\_\_ == '\_\_main\_\_' and \_\_name\_\_ attribute

\- Creating a Flask application

\- Flask template inheritance

\- Sending data from a Flask app to a HTML template and reverse - from HTML form to the Flask app.

\- Bootsrap 4 layout (a bit)

\- and more

&#x200B;

Youtube video: [https://www.youtube.com/watch?v=4v3NEGfeff4](https://www.youtube.com/watch?v=4v3NEGfeff4)

&#x200B;

Thanks for watching.

/r/Python
https://redd.it/aofwx5
Best practice for home page and authentication?

I'd like to create an app that displays certain information on the home page if a user is logged in.  Take GitHub for example.  If you are logged in, [GitHub.com](https://GitHub.com) is your profile.  But if you're not logged in, it's a sign up form. 

I guess I'm asking what the best practices are for displaying different information at the same route depending on whether a user is logged in?  Would I use a conditional to return different templates depending on authentication status?  Or is there a better/more secure way?

Thanks for any help you all can provide.

/r/flask
https://redd.it/aojg3c
New to python, wanted to show my first 2 proper projects

I am not sure if this is the right place to post this, but I have been practicing python and wanted to show off my recent projects which I worked hard on getting working (And thanks to someone at r/learnpython for helping me with the first)

&#x200B;

The first program is a timer, using tkinter for a GUI. I really prefer having a GUI for all my programs, it makes it very nice to use.

[https://steamuserimages-a.akamaihd.net/ugc/939467109946388671/5F61D994F94187E3F6A4DD3C6FF8130CEF7E91E6/](https://steamuserimages-a.akamaihd.net/ugc/939467109946388671/5F61D994F94187E3F6A4DD3C6FF8130CEF7E91E6/)

My second program uses TURTLE Graphics and Tkinter to make a worse-paint program.

[https://steamuserimages-a.akamaihd.net/ugc/939467109946391937/890D58664BB2D587ACDB39B649129A95489DC540/](https://steamuserimages-a.akamaihd.net/ugc/939467109946391937/890D58664BB2D587ACDB39B649129A95489DC540/)

The arrows allow you to turn and move the the turtle, you can change the values it moves by in the boxes and there is a toggle to lift and drop the pen.

&#x200B;

Thanks for reading!

/r/Python
https://redd.it/aoircd
Flask website example

Hello all,

Has anyone found a good resource for flask based websites? I mean pages created with flask that one can download and then modify for learning purposes. I am looking to learning a bit of flask to build a small page to use as a control for a homemade thermostat.

I find that following a tutorial from the beginning takes a really long time and I think it would be helpful to have a working example that one can modify while reading the documentation.

Cheers

/r/Python
https://redd.it/aogxbe
len("😂") crashes IDLE

Kinda cool, I guess.


(Latest release (3.7.2) for Windows.)

/r/Python
https://redd.it/aolz00
[N] University of Toronto is offering a course on Quantum Machine Learning through edX!

The course has just started a few days ago: [https://www.edx.org/course/quantum-machine-learning](https://www.edx.org/course/quantum-machine-learning)

>By the end of this course, you will be able to:
· Distinguish between quantum computing paradigms relevant for machine learning
· Assess expectations for quantum devices on various time scales
· Identify opportunities in machine learning for using quantum resources
· Implement learning algorithms on quantum computers in Python

/r/MachineLearning
https://redd.it/aodpek
Is BeeWare reliable at the moment ?

hello , I have several months to complete a school project and PyBee is exactly what I am looking for , considering python for building cross-platform apps for mobile phones only, has anyone succeeded in making an App using BeeWare ? Or is it better to learn C# and consider Xamarin ?

P.s: I have fairly good knowledge in C,C++ and several other basic langs.

Thank You, Have a nice day ahead.

/r/Python
https://redd.it/aok052