I have a simple rules/conditions table in my database which is used to generate alerts for one of our systems. I want to create a rules engine or a domain specific language.
A simple rule stored in this table would be..(omitting the relationships here)
if temp > 40 send email
Please note there would be many more such rules. A script runs once daily to evaluate these rules and perform the necessary actions. At the beginning, there was only one rule, so we had the script in place to only support that rule. However we now need to make it more scalable to support different conditions/rules. I have looked into rules engines , but I hope to achieve this in some simple pythonic way. At the moment, I have only come up with eval/exec and I know that is not the most recommended approach. So, what would be the best way to accomplish this??
( The rules are stored as data in database so each object like "temperature", condition like "/=..etc" , value like "40,50..etc" and action like "email, sms, etc.." are stored in the database, i retrieve this to form the condition...if temp 50 send email, that was my idea to then use exec or eval on them to make it live code..but not sure if this is the right approach )
I'm use django-avatar app and can't make it to save thumbnails. The original image save normally in my media dir.
Using the step execution showed that error occurred here
image.save(thumb, settings.AVATAR_THUMB_FORMAT, quality=quality)
I found this line in create_thumbnail:
def create_thumbnail(self, size, quality=None):
# invalidate the cache of the thumbnail with the given size first
invalidate_cache(self.user, size)
try:
orig = self.avatar.storage.open(self.avatar.name, 'rb')
image = Image.open(orig)
quality = quality or settings.AVATAR_THUMB_QUALITY
w, h = image.size
if w != size or h != size:
if w > h:
diff = int((w - h) / 2)
image = image.crop((diff, 0, w - diff, h))
else:
diff = int((h - w) / 2)
image = image.crop((0, diff, w, h - diff))
if image.mode != "RGB":
image = image.convert("RGB")
image = image.resize((size, size), settings.AVATAR_RESIZE_METHOD)
thumb = six.BytesIO()
image.save(thumb, settings.AVATAR_THUMB_FORMAT, quality=quality)
thumb_file = ContentFile(thumb.getvalue())
else:
thumb_file = File(orig)
thumb = self.avatar.storage.save(self.avatar_name(size), thumb_file)
except IOError:
return # What should we do here? Render a "sorry, didn't work" img?
maybe all I need is just some library?
Thanks
If you are familiar with Django, you know that they have a Authentication system with User model. Of course, I have many other tables that have a Foreign Key to this User model.
If I want to delete this user, how do I architect a script (or through mysql itself) to delete every table that is related to this user?
My only worry is that I can do this manually...but if I add a table , but I forget to add that table to my DELETE operation...then I have a row that links to a deleted, non-existing User.
this is my code :
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
#from login import htmlPrefix,get_current_user
class MyModel(db.Model):
blob = db.BlobProperty()
class BaseRequestHandler(webapp.RequestHandler):
def render_template(self, filename, template_args=None):
if not template_args:
template_args = {}
path = os.path.join(os.path.dirname(__file__), 'templates', filename)
self.response.out.write(template.render(path, template_args))
class upload(BaseRequestHandler):
def get(self):
self.render_template('index.html',)
def post(self):
file=self.request.get('file')
obj = MyModel()
obj.blob = db.Blob(file.encode('utf8'))
obj.put()
self.response.out.write('upload ok')
class download(BaseRequestHandler):
def get(self):
#id=self.request.get('id')
o = MyModel.all().get()
#self.response.out.write(''.join('%s: %s <br/>' % (a, getattr(o, a)) for a in dir(o)))
self.response.out.write(o)
application = webapp.WSGIApplication(
[
('/?', upload),
('/download',download),
],
debug=True
)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
my index.html is :
<form action="/" method="post">
<input type="file" name="file" />
<input type="submit" />
</form>
and it show :
<__main__.MyModel object at 0x02506830>
but ,i don't want to see this , i want to download it ,
how to change my code to run,
thanks
Just started looking into metaclasses and while they seem powerful, I can think of other ways to accomplish the same type of thing. I was wondering when metaclasses have been found to be the right answer and why.
I have a model form that I use to update a model.
class Turtle(models.Model):
name = models.CharField(max_length=50, blank=False)
description = models.TextField(blank=True)
class TurtleForm(forms.ModelForm):
class Meta:
model = Turtle
Sometimes I don't need to update the entire model, but only want to update one of the fields. So when I POST the form only has information for the description. When I do that the model never saves because it thinks that the name is being blanked out while my intent is that the name not change and just be used from the model.
turtle_form = TurtleForm(request.POST, instance=object)
if turtle_form.is_valid():
turtle_form.save()
Is there any way to make this happen? Thanks!
I have multiple greenlets sending on a common socket. Is it guaranteed that each package sent via socket.sendall is well separated or do I have to acquire a lock before each call to sendall.
So I want to prevent the following scenario:
g1 sends ABCD
g2 sends 1234
received data is mixed up, for example AB1234CD
expected is either ABCD1234 or 1234ABCD
Update
After a look at the sourcecode I think this scenario cannot happen. But I have to use a lock because g1 or g2 can crash on the sendall. Can someone confirm this?
im using the example from the pylons book
orm.mapper(Comment, comment_table)
orm.mapper(Tag, tag_table)
orm.mapper(Nav, nav_table, polymorphic_on=nav_table.c.type, polymorphic_identity='nav')
orm.mapper(Section, section_table, inherits=Nav, polymorphic_identity='section')
orm.mapper(Page, page_table, inherits=Nav, polymorphic_identity='page', properties={
'comments':orm.relation(Comment, backref='page', cascade='all'),
'tags':orm.relation(Tag, secondary=pagetag_table)
})
i am mostly copying from this, but is there a simple way have a default Page that gets referenced, but if users requests a change, create a new Page object? thanks
i want something similar to this
class DefaultPage(Page):
__init__(self):
self.a = a
self.b = b
self.c = c
orm.mapper(DefaultPage, None, inherits=Nav, yada yada )
In the tornado.web module there is a function called _time_independent_equals:
def _time_independent_equals(a, b):
if len(a) != len(b):
return False
result = 0
for x, y in zip(a, b):
result |= ord(x) ^ ord(y)
return result == 0
It is used to compare secure cookie signatures, and thus the name.
But regarding the implementation of this function, is it just a complex way to say a==b?
formbuild's start_with_layout generates this code
<form action="link" method="post"><table>
how do i add id to it? i wanna add some js things that adds elements
I've found that link: http://code.djangoproject.com/attachment/ticket/8424/time_filters.diff and changed my django 1.2 files by adding taht what you can see there.
But now, when I'm trying to write Entry.objects.filter(pub_date__hour = x) - the result is following error:
Field has invalid lookup: hour
What should I do else, to make it work?
(sorry for my english)
I have a .py file in a directory , which is inside the Django project folder.
I have email settings in my settings.py, but this .py file does not import that file.
How can I specify to Django that settings.py should be used , so that I can use EmailMessage class with the settings that are in my settings.py?
I'm trying to dynamically load a class from a specific module (called 'commands') and the code runs totally cool on my local setup running from a local Django server. This bombs out though when I deploy to Google App Engine. I've tried adding the commands module's parent module to the import as well with no avail (on either setup in that case). Here's the code:
mod = __import__('commands.%s' % command, globals(), locals(), [command])
return getattr(mod, command)
App Engine just throws an ImportError whenever it hits this.
And the clarify, it doesn't bomb out on the commands module. If I have a command like 'commands.cat' it can't find 'cat'.
I will have a set of data (x, y, heading), and I need to animate it in real-time. I am currently using matplotlib to animate (x, y) and it works fine, but I would really like to have some way to indicate heading, ie what direction the object is facing. What would be the best library for this? It seems like PyGame might be able to help me out, but would I have to roll out my own graphing library for it?
Thanks
This line:
used_emails = [row.email for row
in db.execute(select([halo4.c.email], halo4.c.email!=''))]
Returns:
['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
I use this to find a match:
if recipient in used_emails:
If it finds a match I need to pull another field (halo4.c.code) from the database in the same row. Any suggestions on how to do this?
i want to get the values of the last 30 frames of the first wav file and first thirty frames of the second wave file in integer format and stored in a list or array.
i have written the code for joining but during this manupalation i am getting in byte format and tried to convert it to integer but couldn't.
as told before i want to get the frame detail of 1st 30 and last 30 in integer format,and by performing other operations join can be more successful
looking for your help in this,please...
thanking you,
import wave
m=['C:/begpython/S0001_0002.wav', 'C:/begpython/S0001_0001.wav']
i=1
a=m[i]
infiles = [a]
outfile = "C:/begpython/S0001_00367.wav"
data= []
data1=[]
for infile in infiles:
w = wave.open(infile, 'rb')
data1=[w.getnframes]
#print w.readframes(100)
data.append( [w.getparams(), w.readframes(w.getnframes())] )
#print w.readframes(1)
#data1 = [ord(character) for character in data1]
#print data1
#data1 = ''.join(chr(character) for character in data1)
w.close()
print data
output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
output.writeframes(data[0][1])
output.writeframes(data[1][1])
output.writeframes(data[2][1])
output.close()
i want to rename the keys of a dictionary are which are ints, and i need them to be ints with leading zeros's so that they sort correctly.
for example my keys are like:
'1','101','11'
and i need them to be:
'001','101','011'
this is what im doing now, but i know there is a better way
tmpDict = {}
for oldKey in aDict:
tmpDict['%04d'%int(oldKey)] = aDict[oldKey]
newDict = tmpDict
I have two identically-sized numpy.array objects (both one-dimensional), one of which contains a list of starting index positions, and the other of which contains a list of ending index positions (alternatively you could say I have a list of starting positions and window lengths). In case it matters, the slices formed by the starting and ending positions are guaranteed to be non-overlapping. I am trying to figure out how to use these starting and ending positions to form an index for another array object, without having to use a loop.
For example:
import numpy as np
start = np.array([1,7,20])
end = np.array([3,10,25])
Want to reference
somearray[1,2,7,8,9,20,21,22,23,24])