[PonyORM-list] Using lambda in Query filter

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Fri Aug 8 15:02:14 UTC 2014


Hi Tomislav!

The behavior that you described was a bug. I fixed it and pushed the fix to
the GitHub.

We didn't encounter this bug earlier because until this moment we always
defined lambdas right inside the parentheses, like in the following example:

    age = 20
    query = Person.select(lambda p: p.age == age)

or

    query = select(p for p in Persons)
    query = query.filter(lambda p: p.age == age)

But in your example you define lambda in one namespace and then use it in
another:

    def make_lambda(age):
        return lambda p: p.age == age

    fn = make_lambda(20)
    query = select(p for p in Persons)
    query = query.filter(fn)

When lambda is used in such way, it takes value of 'age' outer variable not
from locals or globals, but from closures.
Pony ORM 0.5.1 dindn't know how to take variable values from closures. but
this is fixed now, and you can take the latest version from GitHub:
https://github.com/ponyorm/pony

We plan to release the new version Pony ORM 0.5.2 on Monday which includes
this fix, and you will be able to install it from PyPI using
pip install pony --upgrade

Thanks for the reporting!

Regards,
Alexander



On Sun, Aug 3, 2014 at 3:36 AM, Tomislav Tustonic <ttustonic at outlook.com>
wrote:

> 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.com
> To: ponyorm-list at ponyorm.com
> Date: Sun, 3 Aug 2014 00:54:35 +0200
> Subject: [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
>
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20140808/83d05b13/attachment.html>


More information about the ponyorm-list mailing list