In article <53db8bd8$0$2976$e4fe5...@news2.news.xs4all.nl>,
 Alex van der Spek <zd...@xs4all.nl> 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 that lowercase and uppercase keys are stringed together with & is 
> intentional albeit the actual condition is a bit more tricky.
> 
> I've tried several approaches using eval() on a string built from the dict 
> but landed with just spelling it out literally.

I would certainly avoid eval(), especially if there's any chance 
(including ways you can't possibly imagine right now) of external data 
getting included.  Google for "little bobby tables".

Maybe something like (coding on the fly, not tested):

terms = []
for key in 'abc':
    term = cond[key] and not cond[key.upper()]
    terms.append(term)

bool_value = any(terms)
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to