Basic Usage

In this part, basic usages of TreeValue will be introduced one by one with sample code and graph to explain them.

Create a tree

You can easily create a tree value object based on FastTreeValue.

1
2
3
4
5
from treevalue import FastTreeValue

if __name__ == '__main__':
    t = FastTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
    print(t)

The result should be

1
2
3
4
5
6
<FastTreeValue 0x7f44bc2ccd60 keys: ['a', 'b', 'x']>
├── 'a' --> 1
├── 'b' --> 2
└── 'x' --> <FastTreeValue 0x7f44bcfab160 keys: ['c', 'd']>
    ├── 'c' --> 3
    └── 'd' --> 4

A simple tree value structure is created successfully with the structure below.

../../_images/create_a_tree.gv.svg

Edit the tree

After the tree is created, you can access and edit it with __getattr__, __setattr__ and __delattr__.

 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
import os

from treevalue import FastTreeValue, raw

if __name__ == '__main__':
    t = FastTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
    print("Original tree:", t, sep=os.linesep)

    # Get values
    print("Value of t.a: ", t.a)
    print("Value of t.x.c:", t.x.c)
    print("Value of t.x:", t.x, sep=os.linesep)

    # Set values
    t.a = 233
    print("Value after t.a = 233:", t, sep=os.linesep)
    t.x.d = -1
    print("Value after t.x.d = -1:", t, sep=os.linesep)
    t.x = {'e': 5, 'f': 6}
    print("Value after t.x = {'e': 5, 'f': 6}:", t, sep=os.linesep)
    t.x.g = raw({'e': 5, 'f': 6})
    print("Value after t.x.g = raw({'e': 5, 'f': 6}):", t, sep=os.linesep)

    # Delete values
    del t.x.g
    print("Value after del t.x.g:", t, sep=os.linesep)

The result 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Original tree:
<FastTreeValue 0x7f794292c400 keys: ['a', 'b', 'x']>
├── 'a' --> 1
├── 'b' --> 2
└── 'x' --> <FastTreeValue 0x7f794289a9d0 keys: ['c', 'd']>
    ├── 'c' --> 3
    └── 'd' --> 4

Value of t.a:  1
Value of t.x.c: 3
Value of t.x:
<FastTreeValue 0x7f794289a9d0 keys: ['c', 'd']>
├── 'c' --> 3
└── 'd' --> 4

Value after t.a = 233:
<FastTreeValue 0x7f794292c400 keys: ['a', 'b', 'x']>
├── 'a' --> 233
├── 'b' --> 2
└── 'x' --> <FastTreeValue 0x7f794289a9d0 keys: ['c', 'd']>
    ├── 'c' --> 3
    └── 'd' --> 4

Value after t.x.d = -1:
<FastTreeValue 0x7f794292c400 keys: ['a', 'b', 'x']>
├── 'a' --> 233
├── 'b' --> 2
└── 'x' --> <FastTreeValue 0x7f794289a9d0 keys: ['c', 'd']>
    ├── 'c' --> 3
    └── 'd' --> -1

Value after t.x = {'e': 5, 'f': 6}:
<FastTreeValue 0x7f794292c400 keys: ['a', 'b', 'x']>
├── 'a' --> 233
├── 'b' --> 2
└── 'x' --> <FastTreeValue 0x7f7941a18eb0 keys: ['e', 'f']>
    ├── 'e' --> 5
    └── 'f' --> 6

Value after t.x.g = raw({'e': 5, 'f': 6}):
<FastTreeValue 0x7f794292c400 keys: ['a', 'b', 'x']>
├── 'a' --> 233
├── 'b' --> 2
└── 'x' --> <FastTreeValue 0x7f7941a18eb0 keys: ['e', 'f', 'g']>
    ├── 'e' --> 5
    ├── 'f' --> 6
    └── 'g' --> {'e': 5, 'f': 6}

Value after del t.x.g:
<FastTreeValue 0x7f794292c400 keys: ['a', 'b', 'x']>
├── 'a' --> 233
├── 'b' --> 2
└── 'x' --> <FastTreeValue 0x7f7941a18eb0 keys: ['e', 'f']>
    ├── 'e' --> 5
    └── 'f' --> 6

The values on the tree has been changed or deleted properly. And the full life circle of the tree t is like below.

../../_images/edit_tree_1.gv.svg../../_images/edit_tree_2.gv.svg

Do index or slice calculation on the tree

Index and slice index operation can be applied all once, like the example below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import os

from treevalue import FastTreeValue

if __name__ == '__main__':
    t = FastTreeValue({
        'a': [1, 2, 3],
        'b': [4, 9, 16],
        'x': {
            'c': [11, 13, 17],
            'd': [-2, -4, -8]
        }
    })
    print("Result of t[0]:", t[0], sep=os.linesep)  # __getitem__ operator
    print("Result of t[::-1]:", t[::-1], sep=os.linesep)
    print("Result of t.x[1:]:", t.x[1:], sep=os.linesep)

The result should be

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
Result of t[0]:
<FastTreeValue 0x7fcdb17d8e50 keys: ['a', 'b', 'x']>
├── 'a' --> 1
├── 'b' --> 4
└── 'x' --> <FastTreeValue 0x7fcdb17d8ee0 keys: ['c', 'd']>
    ├── 'c' --> 11
    └── 'd' --> -2

Result of t[::-1]:
<FastTreeValue 0x7fcdb17da6a0 keys: ['a', 'b', 'x']>
├── 'a' --> [3, 2, 1]
├── 'b' --> [16, 9, 4]
└── 'x' --> <FastTreeValue 0x7fcdb17da820 keys: ['c', 'd']>
    ├── 'c' --> [17, 13, 11]
    └── 'd' --> [-8, -4, -2]

Result of t.x[1:]:
<FastTreeValue 0x7fcdb17dae50 keys: ['c', 'd']>
├── 'c' --> [13, 17]
└── 'd' --> [-4, -8]

The structures oof the trees is like the graph below.

../../_images/index_operation.gv.svg../../_images/slice_index_operation.gv.svg

Do calculation on the tree

Common calculation is supported in treevalue.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import os

from treevalue import FastTreeValue

if __name__ == '__main__':
    t1 = FastTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
    t2 = FastTreeValue({'a': 3, 'b': 7, 'x': {'c': 14, 'd': -5}})

    print("Result of t1 + t2:", t1 + t2, sep=os.linesep)  # __add__ operator
    print("Result of t1 - t2:", t1 - t2, sep=os.linesep)  # __sub__ operator
    print("Result of t1 ^ t2:", t1 ^ t2, sep=os.linesep)  # __xor__ operator
    print("Result of t1 + t2 * (-4 + t1 ** t2)", t1 + t2 * (-4 + t1 ** -t2))  # mathematics calculation

The result 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
26
27
28
29
30
Result of t1 + t2:
<FastTreeValue 0x7f7cc8c6cf10 keys: ['a', 'b', 'x']>
├── 'a' --> 4
├── 'b' --> 9
└── 'x' --> <FastTreeValue 0x7f7cc8ca2220 keys: ['c', 'd']>
    ├── 'c' --> 17
    └── 'd' --> -1

Result of t1 - t2:
<FastTreeValue 0x7f7cc8ca29a0 keys: ['a', 'b', 'x']>
├── 'a' --> -2
├── 'b' --> -5
└── 'x' --> <FastTreeValue 0x7f7cc8ca2a60 keys: ['c', 'd']>
    ├── 'c' --> -11
    └── 'd' --> 9

Result of t1 ^ t2:
<FastTreeValue 0x7f7cc8ca2fa0 keys: ['a', 'b', 'x']>
├── 'a' --> 2
├── 'b' --> 5
└── 'x' --> <FastTreeValue 0x7f7cc8c8d280 keys: ['c', 'd']>
    ├── 'c' --> 13
    └── 'd' --> -1

Result of t1 + t2 * (-4 + t1 ** t2) <FastTreeValue 0x7f7cc8faf0d0 keys: ['a', 'b', 'x']>
├── 'a' --> -8.0
├── 'b' --> -25.9453125
└── 'x' --> <FastTreeValue 0x7f7cc8c8de80 keys: ['c', 'd']>
    ├── 'c' --> -52.999997072947785
    └── 'd' --> -5096

The values is processed one to one between the tree. The structures of the trees involved in __add__ calculation is like below.

../../_images/calculation_add.gv.svg../../_images/calculation_sub_and_xor.gv.svg

Actually, More common operators are supported in treevalue.

Make function tree supported

Sometimes we need to do some complex calculation which are not able to be represented by raw operators.

In this situation, we can wrap the common function to tree supported function like the code below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import os

from treevalue import FastTreeValue, func_treelize


@func_treelize()
def gcd(a, b):  # GCD calculation
    while True:
        r = a % b
        a, b = b, r
        if r == 0:
            break

    return a


if __name__ == '__main__':
    t1 = FastTreeValue({'a': 2, 'b': 30, 'x': {'c': 4, 'd': 9}})
    t2 = FastTreeValue({'a': 4, 'b': 48, 'x': {'c': 6, 'd': 54}})

    print("Result of gcd(t1, t2):", gcd(t1, t2), sep=os.linesep)
    print("Result of gcd(12, 9):", gcd(12, 9))

The result should be

1
2
3
4
5
6
7
8
9
Result of gcd(t1, t2):
<TreeValue 0x7f7a1d12ff10 keys: ['a', 'b', 'x']>
├── 'a' --> 2
├── 'b' --> 6
└── 'x' --> <TreeValue 0x7f7a1d131220 keys: ['c', 'd']>
    ├── 'c' --> 2
    └── 'd' --> 9

Result of gcd(12, 9): 3

Luckily, the wrapped function can still used as the original function as well.

The structure of the trees in this part is like below.

../../_images/tree_support_1.gv.svg../../_images/tree_support_2.gv.svg

Besides, the func_treelize function will never change the original logical properties of the original function. In the example below, the calculation with original values instead of usage of the trees can be processed properly with the result of the primitive value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from treevalue import func_treelize


@func_treelize()
def gcd(a, b):  # GCD calculation
    while True:
        r = a % b
        a, b = b, r
        if r == 0:
            break

    return a


if __name__ == '__main__':
    print("gcd(6, 8):", gcd(6, 8))
    print("gcd(900, 768):", gcd(900, 768))

The output should be like below, the gcd function can still support the greatest common divisor of the primitive integers.

1
2
gcd(6, 8): 2
gcd(900, 768): 12

For further information of how the tree-supported function works, take a look at How the treelized function works , this note may give you more information.