Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75489

Re: dict to boolean expression, how to?

From Peter Otten <__peter__@web.de>
Subject Re: dict to boolean expression, how to?
Date 2014-08-01 17:44 +0200
Organization None
References <53db8bd8$0$2976$e4fe514c@news2.news.xs4all.nl> <53dba39e$0$2976$e4fe514c@news2.news.xs4all.nl>
Newsgroups comp.lang.python
Message-ID <mailman.12517.1406907906.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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 completely 
under your control (think collections.namedtuple) eval() is also fine.

Adding a public web interface on such a lab application means trouble.

> This newsgroup scares me, 

If you dare say that you are not scared enough ;)

> 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.

That sounds like you are experienced enough to say "There may be problems 
with this code, but I choose not to care about them this time -- at my own 
risk"
 
> Is there a better place to ask questions of this nature?

There is the tutor mailing list which is mostly geared at absolute 
beginners, does more hand-holding, and the threads are more likely to stay 
on topic. You might take a look, but with your background you are probably 
better off here.

> I am sorry, the problem is ill posed.
> 
> 'a', 'A' and so forth are my failed attempt to shorthand.
> 
> In reality the dict's keys are column names in a pandas dataframe df.
> 
> The boolean expression would therefore look like:
> 
> 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
result = functools.reduce(operator.or_, ((v == 1) & (df[k.upper()] == 0) for 
k, v in df.items() if k.islower()))

And here is an eval-based solution:

# untested
expr = "|".join(
    "((df[{}] == 1) | (df[{}] == 0))".format(c, c.upper()) 
    for c in df is c.islower())
result = eval(expr)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

dict to boolean expression, how to? Alex van der Spek <zdoor@xs4all.nl> - 2014-08-01 12:45 +0000
  Re: dict to boolean expression, how to? Chris Angelico <rosuav@gmail.com> - 2014-08-01 23:04 +1000
  Re: dict to boolean expression, how to? Roy Smith <roy@panix.com> - 2014-08-01 09:24 -0400
  Re: dict to boolean expression, how to? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-01 13:28 +0000
    Re: dict to boolean expression, how to? Roy Smith <roy@panix.com> - 2014-08-01 09:32 -0400
      Re: dict to boolean expression, how to? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-01 15:02 +0000
        Re: dict to boolean expression, how to? Terry Reedy <tjreedy@udel.edu> - 2014-08-01 18:16 -0400
    Re: dict to boolean expression, how to? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-01 16:50 +0100
      Re: dict to boolean expression, how to? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-01 16:35 +0000
  Re: dict to boolean expression, how to? Alex van der Spek <zdoor@xs4all.nl> - 2014-08-01 14:26 +0000
    Re: dict to boolean expression, how to? marco.nawijn@colosso.nl - 2014-08-01 08:07 -0700
    Re: dict to boolean expression, how to? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-01 15:24 +0000
      Re: dict to boolean expression, how to? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-01 17:03 +0100
    Re: dict to boolean expression, how to? Peter Otten <__peter__@web.de> - 2014-08-01 17:44 +0200
      eval [was Re: dict to boolean expression, how to?] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-02 02:59 +0000
        Re: eval [was Re: dict to boolean expression, how to?] Peter Otten <__peter__@web.de> - 2014-08-02 09:43 +0200
        Re: eval [was Re: dict to boolean expression, how to?] Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-02 08:59 +0100
        Re: eval [was Re: dict to boolean expression, how to?] Duncan Booth <duncan.booth@invalid.invalid> - 2014-08-05 11:57 +0000
          Re: eval [was Re: dict to boolean expression, how to?] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-06 02:25 +1000
    Re: dict to boolean expression, how to? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-01 17:12 +0100
    Re: dict to boolean expression, how to? Peter Pearson <ppearson@nowhere.invalid> - 2014-08-01 17:00 +0000
  Re: dict to boolean expression, how to? Ned Batchelder <ned@nedbatchelder.com> - 2014-08-01 10:29 -0400

csiph-web