Markers is a parser for boolean formulas. You can provide it an expression like a and not (b or c)
as well as variable assignments like a = true, b = false, c = true
and Markers will evaluate true and not (false or true)
=> false
.
Supported tokens are variables, and
, or
, not
, and parentheses.
If you care about the parse tree and not the evaluation on a set of variable assignments you can also use Markers to just parse an expression and emit a recursive Python structure of boolean operators and variables.
Poetry is a requirement
poetry install
$ poetry run markers parse "not a or b"
> BinaryOp(
> kind=BinaryOpKind.OR,
> left=UnaryOp(
> kind=UnaryOpKind.NOT,
> arg=Var(name='a')
> ),
> right=Var(name='b')
> )
$ poetry run markers eval "not a or b" -t b -f a
> True
pytest tests
I started working on Markers when I realized that pytest doesn't expose the internals of its marks feature to plugins. You can hook into the results of evaluating the boolean formula, but not the unevaluated parse tree. Markers per function or class can be accessed on TestItem
objects, for example through the handle_test_completion
hook.
The name "Markers" is a nod to the fact that pytest is inconsistent with its use of "mark" and "marker".
This project borrowed heavily from some online resources and human minds about parsing in general and boolean formula parsing in particular.