probably.util

probably.util.dedup_list(data)[source]

Deduplicate a list using a set, preserving the ordering of the list.

>>> dedup_list([1,2,3,3])
[1, 2, 3]
Parameters:

data (List[TypeVar(T)]) –

Return type:

List[TypeVar(T)]

Mutable References

class probably.util.ref.Mut(read, write)[source]

A mutable reference to a value of type M, represented a getter and a setter. Use val to read and write the value.

Mut is useful to transform an AST while walking it.

Parameters:
read: Callable[[], TypeVar(M)]
write: Callable[[TypeVar(M)], None]
property val: M

Property that wraps read and write.

static alloc(value)[source]

Create a new Mut in a new anonmyous location.

Parameters:

value (TypeVar(M)) –

Return type:

Mut[TypeVar(M)]

static wrap(ref)[source]

If ref isn’ already a Mut, wrap it with alloc().

Parameters:

ref (Union[Mut[TypeVar(M)], TypeVar(M)]) –

Return type:

Mut[TypeVar(M)]

static list(values)[source]

Create a Mut for each element in the list.

>>> my_list = [1, 2, 3]
>>> ref_list = list(Mut.list(my_list))
>>> ref_list[0].val = 15
>>> my_list
[15, 2, 3]
Parameters:

values (List[TypeVar(M)]) –

Return type:

Iterable[Mut[TypeVar(M)]]

static dict_items(values)[source]

Create a Mut for each key-value pair in the dict.

>>> my_dict = {'hello': 42}
>>> refs = list(Mut.dict_items(my_dict))
>>> refs[0][1].val = 43
>>> my_dict
{'hello': 43}
Parameters:

values (Dict[TypeVar(Key), TypeVar(M)]) –

Return type:

Iterable[Tuple[TypeVar(Key), Mut[TypeVar(M)]]]

static dict_values(values)[source]

A Mut reference to each value in the map. See dict_items().

Parameters:

values (Dict[TypeVar(Key), TypeVar(M)]) –

Return type:

Iterable[Mut[TypeVar(M)]]

static iterate(reference)[source]

If this is a reference to a list of elements, apply list(). Otherwise return only the reference itself.

>>> list(Mut.iterate(Mut.alloc('a')))
[Mut(val='a')]

>>> list(Mut.iterate(Mut.alloc(['a', 'b'])))
[Mut(val='a'), Mut(val='b')]
Parameters:

reference (Union[Mut[TypeVar(M)], Mut[List[TypeVar(M)]]]) –

Return type:

Iterable[Mut[TypeVar(M)]]

probably.util.ref.Ref

A Ref is either a Mut or a value.

alias of Union[Mut, M]

Lark Expression Parser

Generate Lark grammars for expressions from operator tables. The algorithm is pretty simple, but easy to mess up when doing it by hand.

>>> table = [
...    [infixl("plus", "+"), infixl("minus", "-")],
...    [prefix("neg", "-"), atom("parens", '"(" test ")"'), atom("int", "/[0-9]+/")]
... ]
>>> print(build_expr_parser(table, "test"))
?test: test_0
?test_0: test_1
    | test_0 "+" test_1 -> plus
    | test_0 "-" test_1 -> minus
?test_1: "-" test_1 -> neg
    | "(" test ")" -> parens
    | /[0-9]+/ -> int
class probably.util.lark_expr_parser.Assoc(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
LEFT = 1
RIGHT = 2
probably.util.lark_expr_parser.infixl(name, op)[source]
Parameters:
Return type:

Union[_InfixOperator, _PrefixOperator, _PostfixOperator, _AtomOperator]

probably.util.lark_expr_parser.infixr(name, op)[source]
Parameters:
Return type:

Union[_InfixOperator, _PrefixOperator, _PostfixOperator, _AtomOperator]

probably.util.lark_expr_parser.prefix(name, op)[source]
Parameters:
Return type:

Union[_InfixOperator, _PrefixOperator, _PostfixOperator, _AtomOperator]

probably.util.lark_expr_parser.postfix(name, op)[source]
Parameters:
Return type:

Union[_InfixOperator, _PrefixOperator, _PostfixOperator, _AtomOperator]

probably.util.lark_expr_parser.atom(name, rule_str)[source]
Parameters:
  • name (str) –

  • rule_str (str) –

Return type:

Union[_InfixOperator, _PrefixOperator, _PostfixOperator, _AtomOperator]

probably.util.lark_expr_parser.OperatorTable

An OperatorTable contains a list of precedence levels in ascending order.

alias of List[List[Union[_InfixOperator, _PrefixOperator, _PostfixOperator, _AtomOperator]]]

probably.util.lark_expr_parser.build_expr_parser(table, rule_name)[source]

Generate Lark grammar from the operator table.

Parameters:
  • table (List[List[Union[_InfixOperator, _PrefixOperator, _PostfixOperator, _AtomOperator]]]) –

  • rule_name (str) –

Return type:

str