[PonyORM-list] Using lambda in Query filter

Tomislav Tustonic ttustonic at outlook.com
Sat Aug 2 22:54:35 UTC 2014


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     		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20140803/a8c28734/attachment.html>


More information about the ponyorm-list mailing list