[PonyORM-list] Using lambda in Query filter

Tomislav Tustonic ttustonic at outlook.com
Sat Aug 2 23:36:23 UTC 2014


Hello I have found a workaround for the problem, like this: def findByAge(age):
    fnc = "lambda p: p.age == {0}".format(age)
    fn = eval(fnc)
    doFindPersons(fn) But, I'd still like to know if it's possible to use lambdas directly, instead of using eval. Thanks, Tom From: ttustonic at outlook.comTo: ponyorm-list at ponyorm.comDate: Sun, 3 Aug 2014 00:54:35 +0200Subject: [PonyORM-list] Using lambda in Query filter


Hello I'm having a problem using passing lambda expressions as a parameter in Query filter method.In the following example call to 'findByAge0' works fine, but calling 'findByAge' raises an exception:pony.orm.core.ExprEvalError: age raises NameError: name 'age' is not defined db = Database('sqlite', ':memory:') class Person(db.Entity):    name = Required(unicode)    age = Required(int)db.generate_mapping(create_tables=True)with db_session:    p1 = Person(name="Bing Bang", age=31)    p2 = Person(name="Kick Mick", age=75) def findByAge0(age):    fn = lambda p: p.age == age    q = select(p for p in Person)    q = q.filter(fn)    with db_session:        persons = list(q)        for p in persons:            print p.name def findByAge(age):    fn = lambda p: p.age == age    doFindPersons(fn) def doFindPersons(fn):    q = select(p for p in Person)    q = q.filter(fn)    with db_session:        persons = list(q)        for p in persons:            print p.name findByAge0(75)findByAge(75)a = raw_input('[Enter]') Am I doing something wrong or it's not possible to use lambda expression as in this example?Thanks in advance,Tom     		 	   		  
_______________________________________________
ponyorm-list mailing list
ponyorm-list at ponyorm.com
/ponyorm-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20140803/bcb92006/attachment.html>


More information about the ponyorm-list mailing list