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
__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}})
__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.