<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'><p abp="122">Hello</p><p abp="122"> </p><p abp="122">I have found a workaround for the problem, like this:</p><p abp="122"> </p><p abp="122">def findByAge(age):<br> fnc = "lambda p: p.age == {0}".format(age)<br> fn = eval(fnc)<br> doFindPersons(fn)</p><p abp="122"> </p><p abp="122">But, I'd still like to know if it's possible to use lambdas directly, instead of using eval.</p><p abp="122"> </p><p abp="122">Thanks, Tom</p><p abp="122"> </p><div abp="124"><hr id="stopSpelling" abp="125">From: ttustonic@outlook.com<br abp="126">To: ponyorm-list@ponyorm.org<br abp="127">Date: Sun, 3 Aug 2014 00:54:35 +0200<br abp="128">Subject: [PonyORM-list] Using lambda in Query filter<br abp="129"><br abp="130">
<style abp="131"><!--
.ExternalClass .ecxhmmessage P {
padding:0px;
}
.ExternalClass body.ecxhmmessage {
font-size:12pt;
font-family:Calibri;
}
--></style>
<div dir="ltr" abp="132"><p abp="133">Hello</p><p abp="134"> </p><p abp="135">I'm having a problem using passing lambda expressions as a parameter in Query filter method.</p><p abp="136">In the following example call to 'findByAge0' works fine, but calling 'findByAge' raises an exception:</p><p abp="137">pony.orm.core.ExprEvalError: age raises NameError: name 'age' is not defined</p><p abp="138"> </p><p abp="139">db = Database('sqlite', ':memory:') </p><p abp="140">class Person(db.Entity):<br abp="141"> name = Required(unicode)<br abp="142"> age = Required(int)<br abp="143">db.generate_mapping(create_tables=True)</p><p abp="144"><br abp="145">with db_session:<br abp="146"> p1 = Person(name="Bing Bang", age=31)<br abp="147"> p2 = Person(name="Kick Mick", age=75)</p><p abp="148"> </p><p abp="149">def findByAge0(age):<br abp="150"> fn = lambda p: p.age == age<br abp="151"> q = select(p for p in Person)<br abp="152"> q = q.filter(fn)<br abp="153"> with db_session:<br abp="154"> persons = list(q)<br abp="155"> for p in persons:<br abp="156"> print p.name</p><p abp="157"> </p><p abp="158">def findByAge(age):<br abp="159"> fn = lambda p: p.age == age<br abp="160"> doFindPersons(fn)</p><p abp="161"> </p><p abp="162">def doFindPersons(fn):<br abp="163"> q = select(p for p in Person)<br abp="164"> q = q.filter(fn)<br abp="165"> with db_session:<br abp="166"> persons = list(q)<br abp="167"> for p in persons:<br abp="168"> print p.name</p><p abp="169"> </p><p abp="170">findByAge0(75)<br abp="171">findByAge(75)<br abp="172">a = raw_input('[Enter]')<br abp="173"></p><p abp="174"> </p><p abp="175">Am I doing something wrong or it's not possible to use lambda expression as in this example?</p><p abp="176">Thanks in advance,</p><p abp="177">Tom</p><font color="#ffffff" face="DejaVu Sans Mono" size="1" abp="178"><p abp="179"> </p><font color="#ffffff" face="DejaVu Sans Mono" size="1" abp="180"><p abp="181"> </p><font color="#ffffff" face="DejaVu Sans Mono" size="1" abp="182"><p abp="183"> </p><p abp="184"> </p></font></font></font> </div>
<br abp="185">_______________________________________________
ponyorm-list mailing list
ponyorm-list@ponyorm.org
/ponyorm-list</div> </div></body>
</html>