Best way to evaluate boolean expressions from strings?

Gustavo Narea me at
Mon Apr 27 19:03:01 CEST 2009

Hello, everybody.

I need to evaluate boolean expressions like "foo == 1" or "foo ==1 and
(bar > 2 or bar == 0)" which are defined as strings (in a database or
a plain text file, for example). How would you achieve this?

These expressions will contain placeholders for Python objects (like
"foo" and "bar" in the examples above). Also, the Python objects that
will get injected in the expression will support at least one of the
following operations: "==", "!=", ">", "<", ">=", "<=", "&", "|",

I don't need the ability to import modules, define classes, define
functions, etc. I just need to evaluate boolean expressions defined as
strings (using the Python syntax is fine, or even desirable).

Here's a complete example:

I have the "user_ip" and "valid_ips" placeholders defined in Python as
user_ip = ''

class IPCollection(object):
    def __init__(self, *valid_ips):
        self.valid_ips = valid_ips
    def __contains__(self, value):
        return value in self.valid_ips

valid_ips = IPCollection('', '')

So the following boolean expressions given as strings should be
evaluated as:
 * "user_ip == ''"  --->  False
 * "user_ip == '' or user_ip in valid_ips"  --->  True
 * "user_ip not in valid_ips" ---> False

That's it. How would you deal with this? I would love to re-use
existing stuff as much as possible, that works in Python 2.4-2.6 and
also that has a simple syntax (these expressions may not be written by
technical people; hence I'm not sure about using TALES).

Thanks in advance!

More information about the Python-list mailing list