treevalue.tree.common

BaseTree

class treevalue.tree.common.base.BaseTree[source]
Overview:

Base abstract structure of a tree data class.

abstract __delitem__(key)[source]
Overview:

Delete item from this tree node.

Arguments:
  • key (str): Key of the sub tree node.

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

Return self==value.

abstract __getitem__(key)[source]
Overview:

Get item from this tree node, raw value and tree node returns.

Arguments:
  • key (str): Key of the sub tree node.

Returns:
  • return (Any): Tree node or raw value (if the sub node is value node).

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

Return hash(self).

__repr__()[source]

Return repr(self).

abstract __setitem__(key, value)[source]
Overview:

Set item to this tree node, raw value and tree node supported.

Arguments:
  • key (str): Key of the sub tree node.

  • value (Any): Given mode or value to be set.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t['a'] = 3
>>> t  # Tree({'a': 3, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t['x']['e'] = 10
>>> t  # Tree({'a': 3, 'b': 2, 'x': {'c': 3, 'd': 4, 'e': 10}})
__str__()[source]
Overview:

Return tree-formatted string.

Returns:
  • string (str): Tree-formatted string.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}, 'z': [1, 2], 'v': raw({'1': '2'})})
>>> print(t)

The output will be

>>> <Tree 0x7f9fa48b9588 keys: ['a', 'b', 'v', 'x', 'z']>
>>> ├── 'a' --> 1
>>> ├── 'b' --> 2
>>> ├── 'v' --> {'1': '2'}
>>> ├── 'x' --> <Tree 0x7f9fa48b95c0 keys: ['c', 'd']>
>>> │   ├── 'c' --> 3
>>> │   └── 'd' --> 4
>>> └── 'z' --> [1, 2]
abstract actual()[source]
Overview:

Get actual tree node of current tree object. If this is a Tree object, just return itself. If this is a TreeView object, it will return the actually viewed tree node.

Returns:
  • actual (Tree): Actual tree node.

abstract clone(copy_value: Union[None, bool, Callable, Any] = None)[source]
Overview:

Fully clone this tree.

Arguments:
  • 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:
  • cloned (Tree): Cloned new tree.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t2 = t.clone()
>>> t2                 # Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t2 is t            # False
>>> t2['x'] is t['x']  # False
abstract items()[source]
Overview:

Get item iterator of this tree node.

Returns:
  • iter (iter): Item iterator of this tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> for key, value in t.items():
>>>     print(key, value)

The output should be

>>> a 1
>>> b 2
>>> x <Tree 0x7f74f6daaf60 keys: ['c', 'd']>
json()[source]
Overview:

Get full json data of this tree.

Returns:
  • json (dict): Json data

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

Get key iterator of this tree node.

Returns:
  • iter (iter): Key iterator of this tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> for key in t.keys():
>>>     print(key)

The output should be

>>> a
>>> b
>>> x
abstract values()[source]
Overview:

Get value iterator of this tree node.

Returns:
  • iter (iter): Value iterator of this tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> for value in t.values():
>>>     print(value)

The output should be

>>> 1
>>> 2
>>> <Tree 0x7f74f6daaf60 keys: ['c', 'd']>
abstract view(path: List[str])[source]
Overview:

Get view of the current tree.

Arguments:
  • path (List[str]): Viewing path.

Returns:
  • viewed_tree (TreeView): Viewed tree.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t1 = t['x']
>>> t2 = t.view(["x"])
>>>
>>> t1['c']  # 3
>>> t2['c']  # 3
>>>
>>> t.x = Tree({'c': 5, 'd': 6})
>>> t1['c']  # 3
>>> t2['c']  # 5

Tree

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

Tree node data model, based on BaseTree.

__delitem__(key)[source]
Overview:

Delete item from this tree node.

Arguments:
  • key (str): Key of the sub tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> del t['a']
>>> t  # Tree({'b': 2, 'x': {'c': 3, 'd': 4}})
>>> del t['x']['c']
>>> t  # Tree({'b': 2, 'x': {'d': 4}})
__eq__(other)

Return self==value.

__getitem__(key)[source]
Overview:

Get item from this tree node, raw value and tree node returns.

Arguments:
  • key (str): Key of the sub tree node.

Returns:
  • return (Any): Tree node or raw value (if the sub node is value node).

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t['a']       # 1
>>> t['x']       # Tree({'c': 3, 'd': 4})
>>> t['x']['c']  # 3
__hash__()

Return hash(self).

__init__(data: Union[Dict[str, Union[Tree, Any]], Tree])[source]
Overview:

Constructor of Tree, can be dict, Tree, TreeView. When dict passed in, a new tree structure will be created once. When Tree or TreeView passed in, a fully copy will be constructed in this object.

Arguments:
  • data (Union[Dict[str, Union['Tree', Any]], 'Tree']): Any data can be parsed into Tree.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})  # t is a new tree structure
>>> t2 = Tree(t)                                       # t2 is a full copy of t1
>>> t3 = Tree({'a': t, 'b': t2})                       # t3 is a tree with subtree of t and t2 (not copy)
__repr__()

Return repr(self).

__setitem__(key, value)[source]
Overview:

Set item to this tree node, raw value and tree node supported.

Arguments:
  • key (str): Key of the sub tree node.

  • value (Any): Given mode or value to be set.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t['a'] = 3
>>> t  # Tree({'a': 3, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t['x']['e'] = 10
>>> t  # Tree({'a': 3, 'b': 2, 'x': {'c': 3, 'd': 4, 'e': 10}})
__str__()
Overview:

Return tree-formatted string.

Returns:
  • string (str): Tree-formatted string.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}, 'z': [1, 2], 'v': raw({'1': '2'})})
>>> print(t)

The output will be

>>> <Tree 0x7f9fa48b9588 keys: ['a', 'b', 'v', 'x', 'z']>
>>> ├── 'a' --> 1
>>> ├── 'b' --> 2
>>> ├── 'v' --> {'1': '2'}
>>> ├── 'x' --> <Tree 0x7f9fa48b95c0 keys: ['c', 'd']>
>>> │   ├── 'c' --> 3
>>> │   └── 'd' --> 4
>>> └── 'z' --> [1, 2]
actual()[source]
Overview:

Get actual tree node of current tree object. If this is a Tree object, just return itself. If this is a TreeView object, it will return the actually viewed tree node.

Returns:
  • actual (Tree): Actual tree node.

clone(copy_value: Union[None, bool, Callable, Any] = None)[source]
Overview:

Fully clone this tree.

Arguments:
  • 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:
  • cloned (Tree): Cloned new tree.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t2 = t.clone()
>>> t2                 # Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t2 is t            # False
>>> t2['x'] is t['x']  # False
items()[source]
Overview:

Get item iterator of this tree node.

Returns:
  • iter (iter): Item iterator of this tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> for key, value in t.items():
>>>     print(key, value)

The output should be

>>> a 1
>>> b 2
>>> x <Tree 0x7f74f6daaf60 keys: ['c', 'd']>
json()
Overview:

Get full json data of this tree.

Returns:
  • json (dict): Json data

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

Get key iterator of this tree node.

Returns:
  • iter (iter): Key iterator of this tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> for key in t.keys():
>>>     print(key)

The output should be

>>> a
>>> b
>>> x
values()[source]
Overview:

Get value iterator of this tree node.

Returns:
  • iter (iter): Value iterator of this tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> for value in t.values():
>>>     print(value)

The output should be

>>> 1
>>> 2
>>> <Tree 0x7f74f6daaf60 keys: ['c', 'd']>
view(path: List[str])[source]
Overview:

Get view of the current tree.

Arguments:
  • path (List[str]): Viewing path.

Returns:
  • viewed_tree (TreeView): Viewed tree.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t1 = t['x']
>>> t2 = t.view(["x"])
>>>
>>> t1['c']  # 3
>>> t2['c']  # 3
>>>
>>> t.x = Tree({'c': 5, 'd': 6})
>>> t1['c']  # 3
>>> t2['c']  # 5

raw

treevalue.tree.common.tree.raw(value) → treevalue.tree.common.tree._RawWrapped[source]
Overview:

Wrap raw value to init tree or set item, can be used for dict. Can only performs when value is a dict object, otherwise just return the original value.

Arguments:
  • value (:obj:): Original value.

Returns:
  • wrapped (_RawWrapped): Wrapped value.

Example:
>>> t = Tree({
>>>     'a': raw({'a': 1, 'b': 2}),
>>>     'b': raw({'a': 3, 'b': 4}),
>>>     'x': {
>>>         'c': raw({'a': 5, 'b': 6}),
>>>         'd': raw({'a': 7, 'b': 8}),
>>>     }
>>> })
>>>
>>> t['a']  # {'a': 1, 'b': 2}
>>> t['b']  # {'a': 3, 'b': 4}
>>> t['x']['c']  # {'a': 5, 'b': 6}
>>> t['x']['d']  # {'a': 7, 'b': 8}
>>>
>>> t['a'] = raw({'a': 9, 'b': 10})
>>> t['a']  # {'a': 9, 'b': 10}
>>> t['a'] = {'a': 9, 'b': 10}
>>> t['a']  # should be a Tree object when raw not used

TreeView

class treevalue.tree.common.view.TreeView(tree: treevalue.tree.common.tree.Tree, path: List[str])[source]
Overview:

Tree view data model, based on BaseTree.

__delitem__(key)[source]
Overview:

Delete item from this tree node.

Arguments:
  • key (str): Key of the sub tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> del t['a']
>>> t  # Tree({'b': 2, 'x': {'c': 3, 'd': 4}})
>>> del t['x']['c']
>>> t  # Tree({'b': 2, 'x': {'d': 4}})
__eq__(other)

Return self==value.

__getitem__(key)[source]
Overview:

Get item from this tree node, raw value and tree node returns.

Arguments:
  • key (str): Key of the sub tree node.

Returns:
  • return (Any): Tree node or raw value (if the sub node is value node).

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t['a']       # 1
>>> t['x']       # Tree({'c': 3, 'd': 4})
>>> t['x']['c']  # 3
__hash__()

Return hash(self).

__init__(tree: treevalue.tree.common.tree.Tree, path: List[str])[source]
Overview:

Constructor of TreeValue.

Arguments:
  • tree (Tree): Based tree.

  • path (List[str]): Viewing path.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> tv = t.view(["x"])                # tv should be equal to Tree({'c': 3, 'd': 4})
>>> t['x'] = Tree({'c': 5, 'd': 6})
>>> tv                                # tv should be equal to Tree({'c': 5, 'd': 6})
__repr__()

Return repr(self).

__setitem__(key, value)[source]
Overview:

Set item to this tree node, raw value and tree node supported.

Arguments:
  • key (str): Key of the sub tree node.

  • value (Any): Given mode or value to be set.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t['a'] = 3
>>> t  # Tree({'a': 3, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t['x']['e'] = 10
>>> t  # Tree({'a': 3, 'b': 2, 'x': {'c': 3, 'd': 4, 'e': 10}})
__str__()
Overview:

Return tree-formatted string.

Returns:
  • string (str): Tree-formatted string.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}, 'z': [1, 2], 'v': raw({'1': '2'})})
>>> print(t)

The output will be

>>> <Tree 0x7f9fa48b9588 keys: ['a', 'b', 'v', 'x', 'z']>
>>> ├── 'a' --> 1
>>> ├── 'b' --> 2
>>> ├── 'v' --> {'1': '2'}
>>> ├── 'x' --> <Tree 0x7f9fa48b95c0 keys: ['c', 'd']>
>>> │   ├── 'c' --> 3
>>> │   └── 'd' --> 4
>>> └── 'z' --> [1, 2]
actual()[source]
Overview:

Get actual tree node of current tree object. If this is a Tree object, just return itself. If this is a TreeView object, it will return the actually viewed tree node.

Returns:
  • actual (Tree): Actual tree node.

clone(copy_value: Union[None, bool, Callable, Any] = None)[source]
Overview:

Fully clone this tree.

Arguments:
  • 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:
  • cloned (Tree): Cloned new tree.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t2 = t.clone()
>>> t2                 # Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t2 is t            # False
>>> t2['x'] is t['x']  # False
items()[source]
Overview:

Get item iterator of this tree node.

Returns:
  • iter (iter): Item iterator of this tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> for key, value in t.items():
>>>     print(key, value)

The output should be

>>> a 1
>>> b 2
>>> x <Tree 0x7f74f6daaf60 keys: ['c', 'd']>
json()
Overview:

Get full json data of this tree.

Returns:
  • json (dict): Json data

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

Get key iterator of this tree node.

Returns:
  • iter (iter): Key iterator of this tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> for key in t.keys():
>>>     print(key)

The output should be

>>> a
>>> b
>>> x
values()[source]
Overview:

Get value iterator of this tree node.

Returns:
  • iter (iter): Value iterator of this tree node.

Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> for value in t.values():
>>>     print(value)

The output should be

>>> 1
>>> 2
>>> <Tree 0x7f74f6daaf60 keys: ['c', 'd']>
view(path: List[str])[source]
Overview:

Get view of the current tree.

Arguments:
  • path (List[str]): Viewing path.

Returns:
Example:
>>> t = Tree({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
>>> t1 = t['x']
>>> t2 = t.view(["x"])
>>>
>>> t1['c']  # 3
>>> t2['c']  # 3
>>>
>>> t.x = Tree({'c': 5, 'd': 6})
>>> t1['c']  # 3
>>> t2['c']  # 5