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
Free review copies of "Cracking Codes with Python" are available. (Physical print copies, not ebook!)

Cracking Codes with Python teaches complete beginners how to program in the Python programming language. The book features the source code to several ciphers and hacking programs for these ciphers.

If you'd like to receive a free print copy of Cracking Codes with Python, please email the author at al@inventwithpython.com. You'll receive a free ebook to read, and upon posting a review to Amazon, you will receive a complementary print review copy of the book.

Please post an honest review. You will receive a print book no matter what your rating is. The only requirements are that the review be a minimum of 100 words, you must live within the United States (due to shipping costs), and that you email me first before writing the review.

Because the book is under a Creative Commons license you can also, as always, read the HTML book online for free at https://inventwithpython.com/cracking/

/r/Python
https://redd.it/8acbs9
How do I use/ access a foreign key in views in Django?

I am making a programing etymology site in Django. It will have the etymology/history of programing terms. or

For example the term LET (in ECMA6):
> Let is a mathematical statement that was adopted by early programming languages like Scheme and Basic.

I am not clear on how to write views.

For instance, I want to return the term let and the text of the let terms. Let is in the Term table and text is in the Etymology table.

**EXAMPLE**

Here is what I would like to see returned for term_name=Let.

LET:

Let is a mathematical statement that was adopted by early programming languages like Scheme and Basic. Variables are considered low level entities not suitable for higher levels of abstraction, thus the desire of many language designers to introduce similar but more powerful concepts like in Clojure, F#, Scala, where let might mean a value, or a variable that can be assigned....



**MODELS**

Here are my models.

class Term(models.Model):
term_name=models.CharField(max_length=150)
term_type=models.CharField(max_length=150) #type of command
term_pub_date=models.DateTimeField('Term Pub Date')

def __str__(self):
return self.term_name

class Etymology(models.Model):
term = models.ForeignKey(Term, on_delete=models.CASCADE)
etymology_text=models.TextField()
classification=models.CharField(max_length=150) #prog or tool reference
etymology_votes = models.IntegerField(default=0)
# rating=
# user=
etymology_pub_date=models.DateTimeField('Ety date published')

def __str__(self):
return self.etymology_text[:50] + "..."


class Comment(models.Model):
etymology = models.ForeignKey(Etymology, on_delete=models.CASCADE)
comment_text= models.TextField()
comment_votes = models.IntegerField(default=0)
#rating=
#user=
comment_pub_date=models.DateTimeField('com pub date')

def __str__(self):
return self.comment_text[:50] + "..."

How do I write a view which accesses two tables at once. How would I write the view to return what I want to see from my models?

FYI:
Using Django 2.02 and Python 3.6. Also, I not highly experienced so simple


Citation:

JavaScript?, W. (2018). Why was the name 'let' chosen for block-scoped variable declarations in JavaScript?.

/r/django
https://redd.it/8ad8pe
[AF] Factoryboy and Foreign Keys

I'm using factoryboy to build factories (for testing) but I'm not sure what to do with the foreign keys. I have a scenario similar to this (using flask-sqlalchemy):

class User(db.Model):
id = db.Column(db.Integer, primary_key = True)
posts = db.relationship('Post', backref = 'author')

class Post(db.Model):
id = db.Column(db.Integer, primary_key = True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

Right now, I have implemented this:

class BaseFactory(SQLAlchemyModelFactory):
class Meta:
abstract = True
sqlalchemy_session = db.session

class UserFactory(BaseFactory):
id = Sequence (lambda n: n)
posts = SubFactory(PostFactory)
class Meta:
model = User

class PostFactory(BaseFactory):
id = Sequence (lambda n: n)
user_id = ????
class Meta:
model = Post

What goes into my `user_id` field?

/r/flask
https://redd.it/8a7db8
Are there any reasons to use Flask-Bcrypt over passlib with bcrypt?

I am used to using passlib with bcrypt to hash passwords. It seems like the Flask-Bcrypt extension is very popular to use with Flask, but are there any specific reasons I should use that over passlib? At a quick glance, they seem to do the same thing, but also low level encryption isn't really my strong area. Thanks for any advice.

/r/flask
https://redd.it/89xbdl
Suggestions regarding what to learn in Python for GUI Programming?

I am a beginner in python and want to learn GUI Programming, but am a little confused as to which library to choose, therefore I want suggestions regarding which library to choose to learn which is popular and has varied amount of functions to work on.

/r/Python
https://redd.it/8agrdx
Dynamical id's for WTForms

I'm rendering a template with a list of lists (df_list) as variable.

On that page I'm making a table where one of the cells is an input field that needs to have the same id as the string displayed in one of the other cells. I can accomplish this like so:

<form method="POST" action="/something">
{{ form.csrf_token }}
<table>
{% for line in df_list %}
<tr>
<td>{{ line[0] }}</td>
<td>{{ line[1] }}</td>
<td>{{ line[2] }}</td>
<td>{{ line[3] }}</td>
<td>{{ line[4] }}</td>
<td>{{ line[5] }}</td>
<td>{{ line[6] }}</td>
<td>
<input id="{{ line[0] }}" class="pick_date"></input>
</td>
</tr>
{% endfor %}
</table>
</form>

This works but I can't find a way to do something similar using WTForms.

Replacing the <input ...> line with something like this is obviously no good:

{{ form.name(id="{{ line[0] }}") }}

What would be a good way to dynamically assign id's to WTForms in this case?

**Edit: typos and clarification**


/r/flask
https://redd.it/89ybot
What are some amazing blogs related to Python?

I have followed Miguel Grinberg's blog ( https://blog.miguelgrinberg.com/ ) and Sentdex blog ( https://pythonprogramming.net/ ). I was looking for blogs that focus on a particular module entirely like Web development or Image Processing or GUI or anything related to Python.

/r/Python
https://redd.it/8aigrf
Recruiting statisticians/data analysts participants for a research study on career success

I am a graduate student conducting a research study about the skill and knowledge sets of statisticians/data analysts and their perceived career success. Participation involves filling out a questionnaire, which is expected to take approximately 10-15 minutes to complete. Participants must be 18 years old or older. If you are interested in participating, [click here]( https://usmep.co1.qualtrics.com/SE/?SID=SV_cw0L49DrDO04RsF&Q_JFE=0) to take the survey. If you have any questions or concerns, I can be reached at Harold.noble@eagles.usm.edu. Thank you very much for your consideration

/r/pystats
https://redd.it/8ahzj1
Testing of Celery Tasks in Django

Hoping for just some general information here. I have a Celery Task that performs a few functions and then updates a model with new information from the functions.

I want to be able to test

\> that the task completes successfully

\> that the model updates successfully

And I want to be able to debug any errors inside the task. I have read alot of loose instructions but am not entirely sure which way to start.

I have used

@override\_settings\(

DEBUG=True,

CELERY\_TASK\_ALWAYS\_EAGER=True,

CELERY\_TASK\_EAGER\_PROPOGATES=True,

\)

to set appropriate settings for the test I want to run.

The test completes without failure, however, I KNOW that the task should be throwing exceptions and it does not.

How best to go about getting the exception info I need from the task in the django testing environment?

/r/django
https://redd.it/8aljqr
How to tackle Heroku 30 secs request timeout limit

I have a Django application pushed on Heroku which sometimes takes more than 30 secs time to process the data, hence I get the request timeout error. Is there any way to increase the timeout value to say around 2-5 min Or should I look for other web hosting alternatives.

/r/django
https://redd.it/8aij8x
[N] PyTorch as of April is installable via `pip install torch`

http://pytorch.org/ updated on April 3rd to allow `pip install torch`

/r/MachineLearning
https://redd.it/8aj2bj
Downloading files from MongoDB to the user

My project has MongoDB storing a users "file structure". Since the structure is a dict of dicts, eventually bottoming out at a string representation of the individual files, I'm trying to find a way to get django to zip all files in the structure and download them to the user when the user clicks a button. I'm not entirely sure how to go about this if anyone can post any resources or have advice on doing something simliar?

/r/django
https://redd.it/8an3zx
Best way to test Django app running on docker

I'm adding tests to a large Django application in order to facilitate upgrades to python 3 and more recent versions of Django. The app relies on postgres, elasticsearch, and redis. Docker-compose works well with for this, allowing me to start a clean install of the app with versions matched exactly to what is in production.

I feel like integration tests are important because the search function, redis features, etc are intertwined. The problem I'm running into with the integration tests is using selenium. I tried running a headless version of chrome within docker (selenium/standalone-chrome-debug) but the tests are very slow, and sometimes hang. It really does not seem feasible to use selenium within docker. How are others dealing with this problem? From what I can tell I have these options:

1. Stop using selenium and move exclusively to something like Django TestCase

2. Refactor the tests so that I start the services and expose the ports with docker, but then run the main Django tests through a regular virtualenv. I'm not a big fan of this because I would have to change environment variables that point to the services just to run tests.

3. Somehow use my local version of chrome on my MacBook to run selenium tests, even though everything is running on docker. Not sure if this is possible?

Am I missing something? I would love to hear how others are solving this issue. I love docker so far but this hurdle is becoming frustrating.

/r/django
https://redd.it/8aneug
[AF] Authenticating internal APIs

I built a website using flask_login for handling 'logged in' permissions. This extension works great for protecting my views using `@login_required`, but it does nothing to protect my APIs (as far as I'm aware). How do I protect my internal API's? Do I integrate token-based JWT authentication?

EDIT: I had my decorators in the wrong order, sorry. `@login_required` wasn't working for me so I figured that it doesn't work with APIs for some reason. I actually discovered my problem: I placed my `l@login_required` decorator before my route handler, not after. Noob mistake.

/r/flask
https://redd.it/8akawc