prodigy.pgcl.util.ref

Mutable References

class Mut[M](read, write)[source]

Bases: Generic[M]

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[[], M]
write: Callable[[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 (M)

Return type:

Mut[M]

static wrap(ref)[source]

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

Parameters:

ref (Mut[M] | M)

Return type:

Mut[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[M])

Return type:

Iterable[Mut[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[Key, M])

Return type:

Iterable[Tuple[Key, Mut[M]]]

static dict_values(values)[source]

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

Parameters:

values (Dict[Key, M])

Return type:

Iterable[Mut[M]]

static tuple(values)[source]

A Mut reference to each value in the tuple. Note that tuples are immutable, meaning assignment to its items will result in an exception.

>>> my_tuple = ('hello', 42, 3.14)
>>> refs = list(Mut.tuple(my_tuple))
>>> [mut.val for mut in refs]
['hello', 42, 3.14]
>>> refs[1].val = 43
Traceback (most recent call last):
    ...
TypeError: 'tuple' object does not support item assignment
Parameters:

values (Tuple[M, ...])

Return type:

Iterable[Mut[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 (Mut[M] | Mut[List[M]])

Return type:

Iterable[Mut[M]]

Ref = prodigy.pgcl.util.ref.Mut | ~M

A Ref is either a Mut or a value.