ExpressionMatcher lets you specify a boolean expression like
foo or (bar and baz) and check whether a given collection (a tuple, a list,
a dict or even a string) fulfils it.
Just pass the expression to be evaluated to ExpressionMatcher and use the
resulting callable to test it against any collection that can handle the
in/not in membership test operators:
>>> matches = ExpressionMatcher("foo or (bar and baz)")
>>> matches(["foo"])
True
>>> matches({"bar"})
False
>>> matches("barbbbaz")
TrueIf you instead want to try it on the command line:
$ ./match.py 'foo or (bar and baz)' bar baz
✅
$ ./match.py 'foo or (bar and baz)' meh
❌ExpressionMatcher uses doctest:
python3 -m doctest -v match.pyTo avoid depending on any parsing library, ExpressionMatcher (ab)uses the
Python AST machinery to parse
the expression, transform it to the actual membership tests and turn it into
real Python code.
This project is licensed under the MIT license, see the LICENSE file for details.