flib.base.parser module

Multi-Purpose Expression Parser.

class Expression(tokens: List[flib.base.parser.Token], vocabulary: flib.base.parser.Vocabulary, mapping: Optional[dict] = None)

Bases: object

as_func(compile: bool = True) → Callable
as_string(translate: Optional[dict] = None) → str
eval(*args, **kwds) → Any
origin
simplify(values: Optional[dict] = None) → flib.base.parser.Expression
subst(key: str, expr: Union[Expression, str]) → flib.base.parser.Expression

Substitute variable in expression.

symbols
variables
class Parser(vocabulary: Optional[flib.base.parser.Vocabulary] = None)

Bases: object

eval(expression: str, *args, **kwds) → Any
expression
parse(expression: str, variables: Optional[Tuple[str, ...]] = None) → flib.base.parser.Expression
success
class PyBuiltin

Bases: flib.base.parser.PyOperators

Python3 Operators and Builtins.

class PyOperators

Bases: flib.base.parser.Vocabulary

Python3 Operators.

This vocabulary is based on https://docs.python.org/3/reference/expressions.html

Hint

In difference to standard Python interpreter, some expressions are not valid: Invalid: x + -y -> Valid: x + (-y)

class Symbol(type: int, key: str, value: Any, priority: int = 0, builtin: bool = False, factory: bool = False)

Bases: object

Data Class for Parser Symbols.

builtin = False
factory = False
priority = 0
class Token(type: int, id: Union[str, int] = 0, priority: int = 0, value: Any = 0, key: str = '')

Bases: object

id = 0
key = ''
priority = 0
value = 0
class Vocabulary

Bases: set

Base Class for Parser Vocabularies.

get(type: int, key: str) → flib.base.parser.Symbol

Get symbol from vocabulary.

search(type: Optional[int] = None, builtin: Optional[bool] = None) → Dict[str, flib.base.parser.Symbol]

Search for symbols within the vocabulary.

Parameters:
  • type – Integer parameter representing the type of symbols.
  • builtin – Optional Boolean parameter representing the ‘builtin’ flag of the symbols. For ‘True’, only symbols are returned, that are marked to be builtin symbols, for ‘False’ only symbols, that are marked not to be builtin. By default the ‘builtin’ flag is ignored in the search result.
Returns:

OrderedDict containing Symbols in reverse lexical order to prioritize symbols with greater lenght.

pack(a: Any, b: Any) → flib.base.parser._Pack

Pack Arguments together.

parse(expression: str, variables: Optional[Tuple[str, ...]] = None, vocabulary: Optional[flib.base.parser.Vocabulary] = None) → flib.base.parser.Expression