Skip to content

gregorybchris/markers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Markers

Simple parser for boolean formulas

About

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.

Installation

Poetry is a requirement

poetry install

CLI usage

Parsing a boolean formula

$ 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')
> )

Evaluating a boolean formula with variable assignments

$ poetry run markers eval "not a or b" -t b -f a
> True

Running tests

pytest tests

Motivation

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

Acknowledgements

This project borrowed heavily from some online resources and human minds about parsing in general and boolean formula parsing in particular.