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
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
Need help designing database model
Hi, I'm trying to build a simple website that tracks Teachers and when they are available. I would like to query the database to check to see if they are available to cover a class but the only condition is that they cannot have 4 classes in a row (including the coverage).
Thing is, I am having trouble designing the model class to make it easier to check the given condition.
The way I'm thinking of making the model (in pseudo code)
class Teacher(models.Model):
#some information about the teacher
class Class(models.Model):
# I was thinking to have the Class model be a foreignkey to the Teacher model
class Weekday(models.Model):
# have the Weekday model be a foreignkey to the Class model
class Period(models.Model):
# I was thinking to make the Period model be a foreignkey to the Weekday model
I know it's really not much to go on but I'm having such a hard time thinking of how to design the database to make it easier to query in views.py so I apologize in advance.
EDIT: In hopes of being clearer, here is what I would have in Excel.
I would have a table with the teacher's name. Then for each row would have Period 1 through Period 8 and for each column, would be the Monday through Friday.
/r/django
https://redd.it/7b0l4d
Hi, I'm trying to build a simple website that tracks Teachers and when they are available. I would like to query the database to check to see if they are available to cover a class but the only condition is that they cannot have 4 classes in a row (including the coverage).
Thing is, I am having trouble designing the model class to make it easier to check the given condition.
The way I'm thinking of making the model (in pseudo code)
class Teacher(models.Model):
#some information about the teacher
class Class(models.Model):
# I was thinking to have the Class model be a foreignkey to the Teacher model
class Weekday(models.Model):
# have the Weekday model be a foreignkey to the Class model
class Period(models.Model):
# I was thinking to make the Period model be a foreignkey to the Weekday model
I know it's really not much to go on but I'm having such a hard time thinking of how to design the database to make it easier to query in views.py so I apologize in advance.
EDIT: In hopes of being clearer, here is what I would have in Excel.
I would have a table with the teacher's name. Then for each row would have Period 1 through Period 8 and for each column, would be the Monday through Friday.
/r/django
https://redd.it/7b0l4d
reddit
Need help designing database model • r/django
Hi, I'm trying to build a simple website that tracks Teachers and when they are available. I would like to query the database to check to see if...
Dynamic variable value on HTML with Django
I'm looking to display a constantly-changing python variable (that's read from a websocket server) onto a HTML file, currently i'm using a Django tag as it follows:
**templatetags/mytag.py**
from django import template
register = template.Library()
current_value = 0
@register.filter
def c_value(placeholder):
return current_value
#more code that modifies current_value reading from a websocket server
**index.html**
{% load mytag %}
<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( {{ placeholder|c_value }} );
}
, 1000);
}
mytimer();
</script>
#some code from the website
<span id="stuff">Holder</span>
However naturally '{{ placeholder|c_value }}' only outputs the first ever value of 'current_value', which is 0 in this case, and so the source code of index.html ends up being:
**source code after '{{ placeholder|c_value }}'**
<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( 0 );
}
, 1000);
}
mytimer();
</script>
Which is not desired since we would like to print the changing-value of 'current_value' each second.
What is the normal approach for these kind of dynamic texts? Many thanks!
/r/django
https://redd.it/82l23c
I'm looking to display a constantly-changing python variable (that's read from a websocket server) onto a HTML file, currently i'm using a Django tag as it follows:
**templatetags/mytag.py**
from django import template
register = template.Library()
current_value = 0
@register.filter
def c_value(placeholder):
return current_value
#more code that modifies current_value reading from a websocket server
**index.html**
{% load mytag %}
<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( {{ placeholder|c_value }} );
}
, 1000);
}
mytimer();
</script>
#some code from the website
<span id="stuff">Holder</span>
However naturally '{{ placeholder|c_value }}' only outputs the first ever value of 'current_value', which is 0 in this case, and so the source code of index.html ends up being:
**source code after '{{ placeholder|c_value }}'**
<script>
function mytimer() {
setInterval(function(){
$('#stuff').html( 0 );
}
, 1000);
}
mytimer();
</script>
Which is not desired since we would like to print the changing-value of 'current_value' each second.
What is the normal approach for these kind of dynamic texts? Many thanks!
/r/django
https://redd.it/82l23c
reddit
Dynamic variable value on HTML with Django • r/django
I'm looking to display a constantly-changing python variable (that's read from a websocket server) onto a HTML file, currently i'm using a Django...
how to display conditional html in a template shared between several routes ?
hello, so i have two different routes that return the same template,
@app.route('/')
def home:
...
return render_template('default.html')
@app.route('/name')
def name:
...
name = #some code
return render_template('default.html',name=name)
a chunk of code of that template is the following
{% if name is not none %}
<p>customer name is : {{ name }}</p>
{% endif %}
it appears that in the route ('/'), the name is evaluated as not none, so it goes through the if statement and the html is displayed (with blank where the name is supposed to be displayed)
i want my html part to be either fully displayed with the name if there is one, or nothing. how can i do ?
/r/flask
https://redd.it/8h765l
hello, so i have two different routes that return the same template,
@app.route('/')
def home:
...
return render_template('default.html')
@app.route('/name')
def name:
...
name = #some code
return render_template('default.html',name=name)
a chunk of code of that template is the following
{% if name is not none %}
<p>customer name is : {{ name }}</p>
{% endif %}
it appears that in the route ('/'), the name is evaluated as not none, so it goes through the if statement and the html is displayed (with blank where the name is supposed to be displayed)
i want my html part to be either fully displayed with the name if there is one, or nothing. how can i do ?
/r/flask
https://redd.it/8h765l
reddit
r/flask - how to display conditional html in a template shared between several routes ?
1 votes and 8 so far on reddit
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
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
reddit
r/django - Need advice on implementing an app using Django
2 votes and 1 so far on reddit
How to make Django Manager and Model's interactions follow the Open/Closed Principle?
I am designing models for my Django App and am concerned with decoupling the caller's logic from the model's implementation, so that future changes to the model itself do not require changes downstream in the codebase. In short adhering to the Open/Closed Principle (OCP).
I am wondering what is a best practice to implement this while leveraging Django's framework best.
Conceptually, I believe it would make sense to do the following:
1. Custom Manager
/r/django
https://redd.it/1hsh1rz
I am designing models for my Django App and am concerned with decoupling the caller's logic from the model's implementation, so that future changes to the model itself do not require changes downstream in the codebase. In short adhering to the Open/Closed Principle (OCP).
I am wondering what is a best practice to implement this while leveraging Django's framework best.
Conceptually, I believe it would make sense to do the following:
from django.db import models
class FooManager(models.Manager):
def is_active(self):
return self.filter(is_active=True)
class Foo(models.Model):
_bar = models.CharField(max_length=100, db_column="bar")
is_active = models.BooleanField(default=True)
objects = FooManager()
@property
def bar(self):
return self._bar
@bar.setter
def bar(self, value):
if not self._bar:
self._bar = value
else:
#some setter logic
pass
1. Custom Manager
/r/django
https://redd.it/1hsh1rz
Reddit
From the django community on Reddit
Explore this post and more from the django community