Re: eval [was Re: dict to boolean expression, how to?]

2014-08-05 Thread Steven D'Aprano
Duncan Booth wrote: > Steven D'Aprano wrote: [...] >> My refactoring, with the bare minimum use of exec necessary: >> >> https://code.activestate.com/recipes/578918-yet-another-namedtuple/ > > > This may be a silly question, but what would stop you moving the exec > inside the class? I don't

Re: eval [was Re: dict to boolean expression, how to?]

2014-08-05 Thread Duncan Booth
Steven D'Aprano wrote: > Consider the namedtuple implementation in the standard library. > There's a lot of criticism of it, some of it justified. It uses exec > extensively, which means the code is dominated by a giant string > template. This defeats your editor's syntax colouring, makes > refac

Re: eval [was Re: dict to boolean expression, how to?]

2014-08-02 Thread Mark Lawrence
On 02/08/2014 03:59, Steven D'Aprano wrote: My refactoring, with the bare minimum use of exec necessary: https://code.activestate.com/recipes/578918-yet-another-namedtuple/ FTR I get the feed of new recipes from gwene.com.activestate.code.feeds.recipes.langs.python from news.gmane.org. --

Re: eval [was Re: dict to boolean expression, how to?]

2014-08-02 Thread Peter Otten
Steven D'Aprano wrote: > On Fri, 01 Aug 2014 17:44:27 +0200, Peter Otten wrote: > [...] >>> bool = ((df['a'] == 1) & (df['A'] == 0) | >>> (df['b'] == 1) & (df['B'] == 0) | >>> (df['c'] == 1) & (df['C'] == 0)) >> >> This is how it might look without eval(): >> >> #untested >> r

eval [was Re: dict to boolean expression, how to?]

2014-08-01 Thread Steven D'Aprano
On Fri, 01 Aug 2014 17:44:27 +0200, Peter Otten wrote: > Alex van der Spek wrote: > > >> I do know eval() lends itself to code injection but can't say I am >> fully aware of its dangers. It seemed like a handy tool to me. > > In a lab if you don't need to protect your script against attacks fro

Re: dict to boolean expression, how to?

2014-08-01 Thread Terry Reedy
On 8/1/2014 11:02 AM, Steven D'Aprano wrote: On Fri, 01 Aug 2014 09:32:36 -0400, Roy Smith wrote: In article <53db95e6$0$29986$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: eval is almost never the right solution to any problem, and in the very few exceptions, it needs carefu

Re: dict to boolean expression, how to?

2014-08-01 Thread Peter Pearson
On 01 Aug 2014 14:26:38 GMT, Alex van der Spek wrote: [snip] > This newsgroup scares me, it appears to be for professional computer > scientists only, the theoretical part is sometimes too much for this > practical physicist with an old background in FORTRAN. > > Is there a better place to ask q

Re: dict to boolean expression, how to?

2014-08-01 Thread Steven D'Aprano
On Fri, 01 Aug 2014 16:50:51 +0100, Mark Lawrence wrote: >> which can be simplified to: >> >> flag = any( cond[c] == 1 and cond[c.upper()] for c in ['a', 'b', 'c'] ) >> >> > Shouldn't that be cond[c.upper()] == 0 ? Yes it should be, thank you! -- Steven -- https://mail.python.org/mailman/list

Re: dict to boolean expression, how to?

2014-08-01 Thread Mark Lawrence
On 01/08/2014 15:26, Alex van der Spek wrote: On Fri, 01 Aug 2014 12:45:12 +, Alex van der Spek wrote: With a dict like so: cond = {'a': 1, 'b': 1, 'c': 1, 'A': 0, 'B', 0, 'C':0} how would you make a boolean expression like this: bool = (('a' == 1) & ('A' == 0) | ('b' =

Re: dict to boolean expression, how to?

2014-08-01 Thread Mark Lawrence
On 01/08/2014 16:24, Steven D'Aprano wrote: I don't know of anyone here who is an expert in pandas, so if you ask questions which are specific to pandas, we may run into the limits of our knowledge. If you can find a dedicated pandas mailing list or other forum, they may help too, but I don't kno

Re: dict to boolean expression, how to?

2014-08-01 Thread Mark Lawrence
On 01/08/2014 14:28, Steven D'Aprano wrote: On Fri, 01 Aug 2014 12:45:12 +, Alex van der Spek wrote: With a dict like so: cond = {'a': 1, 'b': 1, 'c': 1, 'A': 0, 'B', 0, 'C':0} how would you make a boolean expression like this: bool = (('a' == 1) & ('A' == 0) | ('b' ==

Re: dict to boolean expression, how to?

2014-08-01 Thread Peter Otten
Alex van der Spek wrote: > I do know eval() lends itself to code injection but can't say I am > fully aware of its dangers. It seemed like a handy tool to me. In a lab if you don't need to protect your script against attacks from outside eval() (and exec()) is fine. If the data fed to eval() is

Re: dict to boolean expression, how to?

2014-08-01 Thread Steven D'Aprano
On Fri, 01 Aug 2014 14:26:38 +, Alex van der Spek wrote: [...] > bool = ((df['a'] == 1) & (df['A'] == 0) | > (df['b'] == 1) & (df['B'] == 0) | > (df['c'] == 1) & (df['C'] == 0)) > > I do know eval() lends itself to code injection but can't say I am fully > aware of its dange

Re: dict to boolean expression, how to?

2014-08-01 Thread marco . nawijn
Hi, Are you aware of the Python operator module? It provides function equivalents of all (most?) python operator. So instead of a==b, you can state operator.eq(a,b). As a result, you can loop over the key/value pairs in the dict and built your logic with the operator.eq, operator.and_, and operat

Re: dict to boolean expression, how to?

2014-08-01 Thread Steven D'Aprano
On Fri, 01 Aug 2014 09:32:36 -0400, Roy Smith wrote: > In article <53db95e6$0$29986$c3e8da3$54964...@news.astraweb.com>, > Steven D'Aprano wrote: > >> eval is almost never the right solution to any problem, and in the very >> few exceptions, it needs careful handling by an expert to ensure you'

Re: dict to boolean expression, how to?

2014-08-01 Thread Ned Batchelder
On 8/1/14 8:45 AM, Alex van der Spek wrote: With a dict like so: cond = {'a': 1, 'b': 1, 'c': 1, 'A': 0, 'B', 0, 'C':0} how would you make a boolean expression like this: bool = (('a' == 1) & ('A' == 0) | ('b' == 1) & ('B' == 0) | ('c' == 1) & ('C' == 0)) The fact t

Re: dict to boolean expression, how to?

2014-08-01 Thread Alex van der Spek
On Fri, 01 Aug 2014 12:45:12 +, Alex van der Spek wrote: > With a dict like so: > > cond = {'a': 1, 'b': 1, 'c': 1, > 'A': 0, 'B', 0, 'C':0} > > how would you make a boolean expression like this: > > bool = (('a' == 1) & ('A' == 0) | > ('b' == 1) & ('B' == 0) | > ('c

Re: dict to boolean expression, how to?

2014-08-01 Thread Roy Smith
In article <53db95e6$0$29986$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > eval is almost never the right solution to any problem, and in the very > few exceptions, it needs careful handling by an expert to ensure you're > not introducing serious security bugs. Corollary to th

Re: dict to boolean expression, how to?

2014-08-01 Thread Steven D'Aprano
On Fri, 01 Aug 2014 12:45:12 +, Alex van der Spek wrote: > With a dict like so: > > cond = {'a': 1, 'b': 1, 'c': 1, > 'A': 0, 'B', 0, 'C':0} > > how would you make a boolean expression like this: > > bool = (('a' == 1) & ('A' == 0) | > ('b' == 1) & ('B' == 0) | > ('c

Re: dict to boolean expression, how to?

2014-08-01 Thread Roy Smith
In article <53db8bd8$0$2976$e4fe5...@news2.news.xs4all.nl>, Alex van der Spek wrote: > With a dict like so: > > cond = {'a': 1, 'b': 1, 'c': 1, > 'A': 0, 'B', 0, 'C':0} > > how would you make a boolean expression like this: > > bool = (('a' == 1) & ('A' == 0) | > ('b' == 1) &

Re: dict to boolean expression, how to?

2014-08-01 Thread Chris Angelico
On Fri, Aug 1, 2014 at 10:45 PM, Alex van der Spek wrote: > how would you make a boolean expression like this: > > bool = (('a' == 1) & ('A' == 0) | > ('b' == 1) & ('B' == 0) | > ('c' == 1) & ('C' == 0)) Not sure what the use of this is, because 'a' will never be equal to 1. Are y

dict to boolean expression, how to?

2014-08-01 Thread Alex van der Spek
With a dict like so: cond = {'a': 1, 'b': 1, 'c': 1, 'A': 0, 'B', 0, 'C':0} how would you make a boolean expression like this: bool = (('a' == 1) & ('A' == 0) | ('b' == 1) & ('B' == 0) | ('c' == 1) & ('C' == 0)) The fact that lowercase and uppercase keys are stringed tog