treevalue.tree.tree

TreeValue

class treevalue.tree.tree.tree.TreeValue(*args, **kwargs)[source]
Overview:

Base framework of tree value. And if the fast functions and operators are what you need, please use FastTreeValue in treevalue.tree.general. The TreeValue class is a light-weight framework just for DIY.

__bool__() → bool[source]
Overview:

Check if the tree value is not empty.

Returns:
  • non_empty (bool): Not empty or do.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}, 'e': {}})
>>> not not t    # True
>>> not not t.x  # True
>>> not not t.e  # False
__contains__(key) → bool[source]
Overview:

Check if attribute is in this tree value.

Arguments:
  • key (str): Attribute name.

Returns:
  • exist (bool): If attribute is in this tree value.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> 'a' in t  # True
>>> 'b' in t  # True
>>> 'c' in t  # False
__delattr__(key)[source]
Overview:

Delete attribute from tree value.

Arguments:
  • key (str): Attribute name.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> del t.a    # t will be TreeValue({'b': 2, 'x': {'c': 3, 'd': 4}})
>>> del t.x.c  # t will be TreeValue({'b': 2, 'x': {'d': 4}})
__eq__(other)[source]
Overview:

Check the equality of two tree values.

Arguments:
Returns:
  • equal (bool): Equality.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> clone(t) == t                                                # True
>>> t == TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 5}})      # False
>>> t == TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})      # True
>>> t == FastTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})  # False (type not match)
__getattr__(key)[source]
Overview:

Get item from this tree value.

Arguments:
  • key (str): Attribute name.

Returns:
  • attr (:obj:): Target attribute value.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t.a    # 1
>>> t.b    # 2
>>> t.x.c  # 3
__getstate__()[source]
Overview:

Serialize operation, can support pickle.dumps.

Examples:
>>> import pickle
>>> from treevalue import TreeValue
>>>
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3}})
>>> bin_ = pickle.dumps(t)  # dump it to binary
>>> pickle.loads(bin_)      #  TreeValue({'a': 1, 'b': 2, 'x': {'c': 3}})
__hash__()[source]
Overview:

Hash value of current object.

Returns:
  • hash (int): Hash code of current object.

__init__(data: Union[treevalue.tree.common.base.BaseTree, TreeValue, dict])[source]
Overview:

Constructor of TreeValue.

Arguments:
  • data: (Union[BaseTree, 'TreeValue', dict]): Original data to init a tree value, can be a BaseTree, TreeValue or dict.

Example:
>>> TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> # this is the tree:
>>> # <root> -+--> a (1)
>>> #         +--> b (2)
>>> #         +--> x
>>> #              +--> c (3)
>>> #              +--> d (4)
__iter__()[source]
Overview:

Get iterator of this tree value.

Returns:
  • iter (iter): Iterator for keys and values.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': 3})
>>> for key, value in t:
>>>     print(key, value)

The output will be:

>>> a 1
>>> b 2
>>> x 3
__len__()[source]
Overview:

Get count of the keys.

Returns:
  • length (int): Count of the keys.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> len(t)    # 3
>>> len(t.x)  # 2
__repr__()[source]
Overview:

Get representation format of tree value.

Returns:
  • repr (str): Representation string.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> repr(t)  # <TreeValue 0xffffffff keys: ['a', 'b', 'x']>, the is may be different
__setattr__(key, value)[source]
Overview:

Set sub node to this tree value.

Arguments:
  • key (str): Attribute name.

  • value (:obj:): Sub value.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t.a = 3                 # t will be TreeValue({'a': 3, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t.b = {'x': 1, 'y': 2}  # t will be TreeValue({'a': 3, 'b': {'x': 1, 'y': 2}, 'x': {'c': 3, 'd': 4}})
__setstate__(tree: treevalue.tree.common.tree.Tree)[source]
Overview:

Deserialize operation, can support pickle.loads.

Arguments:
  • tree (Tree): Deserialize tree.

Examples:
>>> import pickle
>>> from treevalue import TreeValue
>>>
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3}})
>>> bin_ = pickle.dumps(t)  # dump it to binary
>>> pickle.loads(bin_)      #  TreeValue({'a': 1, 'b': 2, 'x': {'c': 3}})
__str__()[source]
Overview:

Get the structure of the tree.

Returns:
  • str (str): Returned string.

_attr_extern(key)[source]
Overview:

External protected function for support the unfounded attributes. Default is raise a KeyError.

Arguments:
  • key (str): Attribute name.

Returns:
  • return (:obj:): Anything you like, and if it is not able to validly return anything, just raise an exception here.

jsonify

treevalue.tree.tree.utils.jsonify(tree: _TreeValue)[source]
Overview:

Dump TreeValue object to json data.

Arguments:
  • tree (_TreeValue): Tree value object.

Returns:
  • json (dict): Dumped json data.

Example:
>>> jsonify(TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}}))  # {'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}}

view

treevalue.tree.tree.utils.view(tree: _TreeValue, path: List[str]) → _TreeValue[source]
Overview:

Create a TreeValue object which is a view of given tree.

Arguments:
  • tree (_TreeValue): Tree value object.

  • path (List[str]): Path of the view.

Returns:
  • tree (_TreeValue): Viewed tree value object.

Example:
>>> view(TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}}), ['x'])  # TreeValue({'c': 3, 'd': 4})

clone

treevalue.tree.tree.utils.clone(tree: _TreeValue, copy_value: Union[None, bool, Callable, Any] = None) → _TreeValue[source]
Overview:

Create a fully clone of the given tree.

Arguments:
  • tree (_TreeValue): Tree value object

  • copy_value (Union[None, bool, Callable, Any]): Deep copy value or not, default is None which means do not deep copy the values. If deep copy is required, just set it to True.

Returns:
  • tree (_TreeValue): Cloned tree value object.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> clone(t.x)  # TreeValue({'c': 3, 'd': 4})

typetrans

treevalue.tree.tree.utils.typetrans(tree: treevalue.tree.tree.tree.TreeValue, return_type: Type[_TreeValue]) → _TreeValue[source]
Overview:

Transform tree value object to another tree value type. Attention that in this function, no copy will be made, the original tree value and the transformed tree value are using the same space area.

Arguments:
  • tree (TreeValue): Tree value object

  • return_type (Type[_TreeValue]): Target tree value type

Returns:
  • tree (_TreeValue): Transformed tree value object.

Example:
>>> t = MyTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> typetrans(t, TreeValue)  # TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})

mapping

treevalue.tree.tree.utils.mapping(tree: _TreeValue, func: Callable) → _TreeValue[source]
Overview:

Do mapping on every value in this tree.

Arguments:
  • tree (_TreeValue): Tree value object

  • func (Callable): Function for mapping

Returns:
  • tree (_TreeValue): Mapped tree value object.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> mapping(t, lambda x: x + 2)  # TreeValue({'a': 3, 'b': 4, 'x': {'c': 5, 'd': 6}})
>>> mapping(t, lambda: 1)        # TreeValue({'a': 1, 'b': 1, 'x': {'c': 1, 'd': 1}})
>>> mapping(t, lambda x, p: p)   # TreeValue({'a': ('a',), 'b': ('b',), 'x': {'c': ('x', 'c'), 'd': ('x', 'd')}})

mask

treevalue.tree.tree.utils.mask(tree: _TreeValue, mask_: Union[treevalue.tree.tree.tree.TreeValue, bool], remove_empty: bool = True) → _TreeValue[source]
Overview:

Filter the element in the tree with a mask

Arguments:
  • tree (_TreeValue): Tree value object

  • mask_ (TreeValue): Tree value mask object

  • remove_empty (bool): Remove empty tree node automatically, default is True.

Returns:
  • tree (_TreeValue): Filtered tree value object.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> mask(t, TreeValue({'a': True, 'b': False, 'x': False}))                    # TreeValue({'a': 1})
>>> mask(t, TreeValue({'a': True, 'b': False, 'x': {'c': True, 'd': False}}))  # TreeValue({'a': 1, 'x': {'c': 3}})

filter_

treevalue.tree.tree.utils.filter_(tree: _TreeValue, func: Callable, remove_empty: bool = True) → _TreeValue[source]
Overview:

Filter the element in the tree with a predict function.

Arguments:
  • tree (_TreeValue): Tree value object

  • func (Callable): Function for filtering

  • remove_empty (bool): Remove empty tree node automatically, default is True.

Returns:
  • tree (_TreeValue): Filtered tree value object.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> filter_(t, lambda x: x < 3)                  # TreeValue({'a': 1, 'b': 2})
>>> filter_(t, lambda x: x < 3, False)           # TreeValue({'a': 1, 'b': 2, 'x': {}})
>>> filter_(t, lambda x: x % 2 == 1)             # TreeValue({'a': 1, 'x': {'c': 3}})
>>> filter_(t, lambda x, p: p[0] in {'b', 'x'})  # TreeValue({'b': 2, 'x': {'c': 3, 'd': 4}})

union

treevalue.tree.tree.utils.union(*trees: treevalue.tree.tree.tree.TreeValue, return_type=None, **kwargs)[source]
Overview:

Union tree values together.

Arguments:
  • trees (_TreeValue): Tree value objects.

  • mode (:obj:): Mode of the wrapping (string or TreeMode both okay), default is strict.

  • return_type (Optional[Type[_ClassType]]): Return type of the wrapped function, default is TreeValue.

  • inherit (bool): Allow inherit in wrapped function, default is True.

  • missing (:obj:): Missing value or lambda generator of when missing, default is MISSING_NOT_ALLOW, which means raise KeyError when missing detected.

Returns:
  • result (TreeValue): Unionised tree value.

Example:
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> tx = mapping(t, lambda v: v % 2 == 1)
>>> union(t, tx)  # TreeValue({'a': (1, True), 'b': (2, False), 'x': {'c': (3, True), 'd': (4, False)}})

subside

treevalue.tree.tree.utils.subside(value, dict_: bool = True, list_: bool = True, tuple_: bool = True, return_type: Optional[Type[_TreeValue]] = None, **kwargs) → _TreeValue[source]
Overview:

Drift down the structures (list, tuple, dict) down to the tree’s value.

Arguments:
  • value (:obj:): Original value object, may be nested dict, list or tuple.

  • dict_ (bool): Enable dict subside, default is True.

  • list_ (bool): Enable list subside, default is True.

  • tuple_ (bool): Enable list subside, default is True.

  • mode (:obj:): Mode of the wrapping (string or TreeMode both okay), default is strict.

  • return_type (Optional[Type[_ClassType]]): Return type of the wrapped function, will be auto detected when there is exactly one tree value type in this original value, otherwise the default will be TreeValue.

  • inherit (bool): Allow inherit in wrapped function, default is True.

  • missing (:obj:): Missing value or lambda generator of when missing, default is MISSING_NOT_ALLOW, which means raise KeyError when missing detected.

Returns:
  • return (_TreeValue): Subsided tree value.

Example:
>>> data = {
>>>     'a': TreeValue({'a': 1, 'b': 2}),
>>>     'x': {
>>>         'c': TreeValue({'a': 3, 'b': 4}),
>>>         'd': [
>>>             TreeValue({'a': 5, 'b': 6}),
>>>             TreeValue({'a': 7, 'b': 8}),
>>>         ]
>>>     },
>>>     'k': '233'
>>> }
>>>
>>> tree = subside(data)
>>> # tree should be --> TreeValue({
>>> #    'a': raw({'a': 1, 'k': '233', 'x': {'c': 3, 'd': [5, 7]}}),
>>> #    'b': raw({'a': 2, 'k': '233', 'x': {'c': 4, 'd': [6, 8]}}),
>>> #}), all structures above the tree values are subsided to the bottom of the tree.

rise

treevalue.tree.tree.utils.rise(tree: _TreeValue, dict_: bool = True, list_: bool = True, tuple_: bool = True, template=<SingletonMark 'no_rise_template'>)[source]
Overview:

Make the structure (dict, list, tuple) in value rise up to the top, above the tree value.

Arguments:
  • tree (_TreeValue): Tree value object

  • dict_ (bool): Enable dict rise, default is True.

  • list_ (bool): Enable list rise, default is True.

  • tuple_ (bool): Enable list rise, default is True.

  • template (:obj:): Rising template, default is NO_RISE_TEMPLATE, which means auto detect.

Returns:
  • risen (:obj:): Risen value.

Example:
>>> t = TreeValue({'x': raw({'a': [1, 2], 'b': [2, 3]}), 'y': raw({'a': [5, 6, 7], 'b': [7, 8]})})
>>> dt = rise(t)
>>> # dt will be {'a': <TreeValue 1>, 'b': [<TreeValue 2>, <TreeValue 3>]}
>>> # TreeValue 1 will be TreeValue({'x': [1, 2], 'y': [5, 6, 7]})
>>> # TreeValue 2 will be TreeValue({'x': 2, 'y': 7})
>>> # TreeValue 3 will be TreeValue({'x': 3, 'y': 8})
>>>
>>> t2 = TreeValue({'x': raw({'a': [1, 2], 'b': [2, 3]}), 'y': raw({'a': [5, 6], 'b': [7, 8]})})
>>> dt2 = rise(t2)
>>> # dt2 will be {'a': [<TreeValue 1>, <TreeValue 2>], 'b': [<TreeValue 3>, <TreeValue 4>]}
>>> # TreeValue 1 will be TreeValue({'x': 1, 'y': 5})
>>> # TreeValue 2 will be TreeValue({'x': 2, 'y': 6})
>>> # TreeValue 3 will be TreeValue({'x': 2, 'y': 7})
>>> # TreeValue 4 will be TreeValue({'x': 3, 'y': 8})
>>>
>>> dt3 = rise(t2, template={'a': None, 'b': None})
>>> # dt3 will be {'a': <TreeValue 1>, 'b': <TreeValue 2>}
>>> # TreeValue 1 will be TreeValue({'x': [1, 2], 'y': [5, 6]})
>>> # TreeValue 2 will be TreeValue({'x': [2, 3], 'y': [7, 8]})

reduce_

treevalue.tree.tree.utils.reduce_(tree: _TreeValue, func)[source]
Overview

Reduce the tree to value.

Arguments:
  • tree (_TreeValue): Tree value object

  • func (:obj:): Function for reducing

Returns:
  • result (:obj:): Reduce result

Examples:
>>> from functools import reduce
>>>
>>> t = TreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> reduce_(t, lambda **kwargs: sum(kwargs.values()))  # 10, 1 + 2 + (3 + 4)
>>> reduce_(t, lambda **kwargs: reduce(lambda x, y: x * y, list(kwargs.values())))  # 24, 1 * 2 * (3 * 4)

NO_RISE_TEMPLATE

treevalue.tree.tree.utils.NO_RISE_TEMPLATE

Means no template is given to the rise function, and the decorated function will automatically try to match the format patterns as template.

graphics

treevalue.tree.tree.graph.graphics(*trees, title: Optional[str] = None, cfg: Optional[dict] = None, dup_value: Union[bool, Callable, type, Tuple[Type, ]] = False, repr_gen: Optional[Callable] = None, node_cfg_gen: Optional[Callable] = None, edge_cfg_gen: Optional[Callable] = None) → graphviz.graphs.Digraph[source]
Overview:

Draw graph by tree values. Multiple tree values is supported.

Args:
  • trees: Given tree values, tuples of Tuple[TreeValue, str] or tree values are both accepted.

  • title (Optional[str]): Title of the graph.

  • cfg (Optional[dict]): Configuration of the graph.

  • dup_value (Union[bool, Callable, type, Tuple[Type, ...]]): Value duplicator, set True to make value with same id use the same node in graph, you can also define your own node id algorithm by this argument. Default is False which means do not use value duplicator.

  • repr_gen (Optional[Callable]): Representation format generator, default is None which means using repr function.

  • node_cfg_gen (Optional[Callable]): Node configuration generator, default is None which means no configuration.

  • edge_cfg_gen (Optional[Callable]): Edge configuration generator, default is None which means no configuration.

Returns:
  • graph (Digraph): Generated graph of tree values.

Here is an example of graphics function. The source code is

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np

from treevalue import FastTreeValue, graphics, TreeValue


class MyFastTreeValue(FastTreeValue):
    pass


if __name__ == '__main__':
    t = MyFastTreeValue({
        'a': 1,
        'b': np.array([[5, 6], [7, 8]]),
        'x': {
            'c': 3,
            'd': 4,
            'e': np.array([[1, 2], [3, 4]])
        },
    })
    t2 = TreeValue({'ppp': t.x, 'x': {'t': t, 'y': t.x}})

    g = graphics(
        (t, 't'), (t2, 't2'),
        title="This is a demo of 2 trees.",
        cfg={'bgcolor': '#ffffff00'},
    )
    g.render('graphics.dat.gv', format='svg')

The generated graphviz source code should be

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// This is a demo of 2 trees.
digraph this_is_a_demo_of_2_trees {
	graph [bgcolor="#ffffff00" label="This is a demo of 2 trees."]
	node_7f7e129e6f10 [label=t color="#3eb24899" fillcolor="#73ff7e99" fontcolor="#003304ff" fontname="Times-Roman bold" penwidth=3 shape=diamond style=filled]
	node_7f7e12024250 [label=t2 color="#5b3eb299" fillcolor="#9673ff99" fontcolor="#0d0033ff" fontname="Times-Roman bold" penwidth=3 shape=diamond style=filled]
	value__node_7f7e129e6f10__a [label=1 color="#3eb24800" fillcolor="#73ff7e99" fontcolor="#003304ff" fontname="Times-Roman" penwidth=1.5 shape=box style=filled]
	node_7f7e129e6f10 -> value__node_7f7e129e6f10__a [label=a arrowhead=dot arrowsize=0.5 color="#1f9929cc" fontcolor="#00730aff" fontname="Times-Roman bold"]
	value__node_7f7e129e6f10__b [label="array([[5, 6],
       [7, 8]])" color="#3eb24800" fillcolor="#73ff7e99" fontcolor="#003304ff" fontname="Times-Roman" penwidth=1.5 shape=box style=filled]
	node_7f7e129e6f10 -> value__node_7f7e129e6f10__b [label=b arrowhead=dot arrowsize=0.5 color="#1f9929cc" fontcolor="#00730aff" fontname="Times-Roman bold"]
	node_7f7e199bf160 [label="t.x" color="#3eb24899" fillcolor="#73ff7e99" fontcolor="#003304ff" fontname="Times-Roman bold" penwidth=1.5 shape=ellipse style=filled]
	node_7f7e129e6f10 -> node_7f7e199bf160 [label=x arrowhead=vee arrowsize=1.0 color="#1f9929cc" fontcolor="#00730aff" fontname="Times-Roman bold"]
	node_7f7e12024250 -> node_7f7e199bf160 [label=ppp arrowhead=vee arrowsize=1.0 color="#3d1f99cc" fontcolor="#1d0073ff" fontname="Times-Roman bold"]
	node_7f7e12024c70 [label="t2.x" color="#5b3eb299" fillcolor="#9673ff99" fontcolor="#0d0033ff" fontname="Times-Roman bold" penwidth=1.5 shape=ellipse style=filled]
	node_7f7e12024250 -> node_7f7e12024c70 [label=x arrowhead=vee arrowsize=1.0 color="#3d1f99cc" fontcolor="#1d0073ff" fontname="Times-Roman bold"]
	value__node_7f7e199bf160__c [label=3 color="#3eb24800" fillcolor="#73ff7e99" fontcolor="#003304ff" fontname="Times-Roman" penwidth=1.5 shape=box style=filled]
	node_7f7e199bf160 -> value__node_7f7e199bf160__c [label=c arrowhead=dot arrowsize=0.5 color="#1f9929cc" fontcolor="#00730aff" fontname="Times-Roman bold"]
	value__node_7f7e199bf160__d [label=4 color="#3eb24800" fillcolor="#73ff7e99" fontcolor="#003304ff" fontname="Times-Roman" penwidth=1.5 shape=box style=filled]
	node_7f7e199bf160 -> value__node_7f7e199bf160__d [label=d arrowhead=dot arrowsize=0.5 color="#1f9929cc" fontcolor="#00730aff" fontname="Times-Roman bold"]
	value__node_7f7e199bf160__e [label="array([[1, 2],
       [3, 4]])" color="#3eb24800" fillcolor="#73ff7e99" fontcolor="#003304ff" fontname="Times-Roman" penwidth=1.5 shape=box style=filled]
	node_7f7e199bf160 -> value__node_7f7e199bf160__e [label=e arrowhead=dot arrowsize=0.5 color="#1f9929cc" fontcolor="#00730aff" fontname="Times-Roman bold"]
	node_7f7e12024c70 -> node_7f7e129e6f10 [label=t arrowhead=vee arrowsize=1.0 color="#3d1f99cc" fontcolor="#1d0073ff" fontname="Times-Roman bold"]
	node_7f7e12024c70 -> node_7f7e199bf160 [label=y arrowhead=vee arrowsize=1.0 color="#3d1f99cc" fontcolor="#1d0073ff" fontname="Times-Roman bold"]
}

The graph should be

../../_images/graphics.dat.gv.svg

Also, graphics function can support value duplication. For if the value nodes are using the same object, they will be displayed in the same node of the generated graph, such as the source code below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import numpy as np

from treevalue import FastTreeValue, graphics


class MyFastTreeValue(FastTreeValue):
    pass


if __name__ == '__main__':
    t = MyFastTreeValue({
        'a': [4, 3, 2, 1],
        'b': np.array([[5, 6], [7, 8]]),
        'x': {
            'c': np.array([[5, 7], [8, 6]]),
            'd': {'a', 'b', 'c'},
            'e': np.array([[1, 2], [3, 4]])
        },
    })
    t1 = MyFastTreeValue({
        'aa': t.a,
        'bb': np.array([[5, 6], [7, 8]]),
        'xx': {
            'cc': t.x.c,
            'dd': t.x.d,
            'ee': np.array([[1, 2], [3, 4]])
        },
    })

    g = graphics(
        (t, 't'), (t1, 't1'),
        (MyFastTreeValue({'a': t, 'b': t1, 'c': [1, 2], 'd': t1.xx}), 't2'),
        # Here is the dup value, with several types
        # np.ndarray and list type will use the same value node,
        # but set type is not in this tuple, so will not share the same node.
        dup_value=(np.ndarray, list),
        title="This is a demo of 2 trees with dup value.",
        cfg={'bgcolor': '#ffffff00'},
    )
    g.render('graphics_dup_value.dat.gv', format='svg')

The graph of the case with dup_value should be

../../_images/graphics_dup_value.dat.gv.svg

The return value’s type of function graphics is class graphviz.dot.Digraph, from the opensource library graphviz, for further information of this project and graphviz.dot.Digraph’s usage, take a look at:

dump

treevalue.tree.tree.io.dump(t: _TreeType, file, compress=None)[source]
Overview:

Dump tree value to file.

Arguments:
  • t (_TreeType): Tree value object.

  • file: Target dump file.

  • compress: Compress object, may be compression function, tuple of functions or module (compress and decompress required).

dumps

treevalue.tree.tree.io.dumps(t: _TreeType, compress=None) → bytes[source]
Overview:

Dump tree value to file.

Arguments:
  • t (_TreeType): Tree value object.

  • compress: Compress object, may be compression function, tuple of functions or module (compress and decompress required).

Returns:
  • data (bytes): Dumped binary data.

load

treevalue.tree.tree.io.load(file, type_: Type[_TreeType] = <class 'treevalue.tree.tree.tree.TreeValue'>, decompress: Optional[Callable] = None) → _TreeType[source]
Overview:

Load tree value object from file.

Arguments:
  • file: Original file.

  • type_ (Type[_TreeType]): Type of tree value, default is TreeValue.

  • decompress (Optional[Callable]): Decompress function, default is None which means do not do any decompression.

Returns:
  • tree (_TreeType): Tree value object.

loads

treevalue.tree.tree.io.loads(data: Union[bytes, bytearray], type_: Optional[Type[_TreeType]] = <class 'treevalue.tree.tree.tree.TreeValue'>, decompress: Optional[Callable] = None) → _TreeType[source]
Overview:

Load tree value object from file.

Arguments:
  • data (Union[bytes, bytearray]): Binary data.

  • type_ (Type[_TreeType]): Type of tree value, default is TreeValue.

  • decompress (Optional[Callable]): Decompress function, default is None which means do not do any decompression.

Returns:
  • tree (_TreeType): Tree value object.