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
Making a thread from an object's method: reasonable, or Bad Idea?

I have a class I'm instantiating in my project that I feel would benefit from having one of its methods be a continuously-running background operation, say, for the sake of simplicity,

from time import sleep

class Foo():
def __init__(self):
#some init code
pass


def my_method(self):
while True:
#do something
sleep(1)

Would it be wise to do something like:

from threading import Thread

foo = Foo()
t = Thread(target=foo.my_method)
t.start()

If not, what are the potential pitfalls/drawbacks/reasons for it being a bad idea?

/r/Python
http://redd.it/59wqdy
How to handle non-unique entrys to a database with SQLAlchemy?

I am making a database with SQL alchemy and i want people to be able to have the same username, but it doesn't allow me, every time i enter a score and username, the username must be different.

Here's the code, this is just test code but the same principals will be used in my final product.

Also if i cant get around this, how do i handle it without it just going to the jninja page.



@app.route('/test', methods=['GET', 'POST'])
def test():
testF=testForm()
if request.method == 'POST':
if testF.validate_on_submit() == False:
return render_template('test.html', form=testF)
else:
#Do shit
newUser = User(username=testF.Name.data,score=testF.Score.data)
db.session.add(newUser)
db.session.commit()
return render_template('test.html', form=testF)


elif request.method == 'GET':
return render_template('test.html', form=testF)

class testForm(Form):
Name = TextField("Name")
Score = IntegerField("Score")
submit = SubmitField("Send")

class User(db.Model):
__tablename__='User'
id=db.Column('id',db.Integer,primary_key=True,autoincrement=True)
username=db.Column(db.String(45), unique=False)
score=db.Column(db.Integer(), unique=False)

/r/flask
https://redd.it/6duetb
Need advice on implementing an app using Django

I have a simple python app and I need to know what is the best practice to turn it into a Django app. My app is something like this:

Class abc():

#some attributes
A=[6,7,6,4,3]
B=[6,8,9,[7,1,1000]]

#some methods
Def aaa(self):
#do something on A and B
Return something

Def bbb(self):
###do something using previous methods and attributes
Return something

X=abc()
X.bbb()

I already know how to turn classes into Django models, but the twist is:

1. Most of those attributes are long lists of lists
2. Methods return lists which then gets used in other methods
3. I also need advice on how to get user input for those list of lists (formsets maybe, but how?)

/r/django
https://redd.it/8w14ac
request.method == "POST"

HI ALL I'm quite new to flask, and web dev in general, and trying to wrap my head around Flask's commonly used (at least in the tutorials I've watched)

if request.method == "POST":
#do some things


I don't see an equivalent "GET" version of this in the tutorials, so I'm not quite sure what's happening. If I'm submitting from a form, why do I need this if statement?

cheers

/r/flask
https://redd.it/ghesin