otf.pack.tree
: In memory trees#
Convert to and from Node
.
otf.pack
convert directly between different types and doesn’t have any
intermediate representation of values. Having access to a simple representation
makes it easier to write tests and debug application.
This is the easiest way to programmatically check what a python object looks like when it’s reduced:
>>> v = (1, 2)
>>> explode([(1, 2)])
[Custom(constructor='tuple', args=([1, 2],), kwargs={})]
You can also leverage this module to check other reducers:
>>> import otf.pack.text
>>> otf.pack.reduce_text("{4: I.do_not.exist(1)}", NodeBuilder())
{4: Custom(constructor='I.do_not.exist', args=(1,), kwargs={})}
API:#
- otf.pack.tree.Node = int | float | None | str | bytes | bool | otf.pack.tree.Custom | otf.pack.tree.Reference | otf.pack.tree.Mapping | dict[typing.Any, typing.Any] | list[typing.Any]#
- class otf.pack.tree.Mapping(data)#
A key value container
Usually mapping are represented as dictionaries:
>>> explode({None: "empty"}) # All the exploded keys are hashable {None: 'empty'}
When the reduced value cannot be safely represented as a dictionary (because some keys are not hashable) we use a
Mapping
:>>> from pprint import pp >>> pp(explode({(): "empty"}), width=60) Mapping(data=[(Custom(constructor='tuple', args=([],), kwargs={}), 'empty')])
Note that the mapping cannot implement the
collections.Mapping
interface because it might contain duplicate keys:>>> v1 = (1,) >>> v2 = (2,) >>> e = explode({1: v1, v1: v2, v2: 2}) >>> pp(e, width=60) Mapping(data=[(1, Custom(constructor='tuple', args=([1],), kwargs={})), (Reference(offset=3), Custom(constructor='tuple', args=([2],), kwargs={})), (Reference(offset=3), 2)])
- class otf.pack.tree.Reference(offset)#
Denotes a shared reference.
This is a reference to the object that appeared offset nodes ago in a depth first traversal of the tree.
- Parameters
offset (int) –
- class otf.pack.tree.Custom(constructor, *args, **kwargs)#
Denotes a type with a custom de-serialisation function
- Parameters
constructor (str) – The name of the function used to do the implosion of custom types
value (Node) – A serialisable value that can be passed as an argument to constructor to recreate the original value