Skip to content

ExpressionMatcher - check if a collection matches a boolean expression

License

Notifications You must be signed in to change notification settings

em-/python-ast-expression-matcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ExpressionMatcher - check if a collection matches a boolean expression

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.

Getting Started

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")
True

If 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
❌

Running the tests

ExpressionMatcher uses doctest:

python3 -m doctest -v match.py

Built With

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

License

This project is licensed under the MIT license, see the LICENSE file for details.

About

ExpressionMatcher - check if a collection matches a boolean expression

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages