Debian Codegraph @

docs/intro.rst

raw
1 Introduction
2 ============
3
4 Overview
5 --------
6
7 `anytree` is split into the following parts:
8
9 **Node Classes**
10
11 * :any:`Node`: a simple tree node with at least a name attribute and any number of additional attributes.
12 * :any:`AnyNode`: a generic tree node and any number of additional attributes.
13 * :any:`NodeMixin`: extends any python class to a tree node.
14
15 **Node Resolution**
16
17 * :any:`Resolver`: retrieve node via absolute or relative path.
18 * :any:`Walker`: walk from one node to an other.
19
20 **Tree Iteration Strategies**
21
22 * :any:`PreOrderIter`: iterate over tree using pre-order strategy
23 * :any:`PostOrderIter`: iterate over tree using post-order strategy
24 * :any:`LevelOrderIter`: iterate over tree using level-order strategy
25 * :any:`LevelOrderGroupIter`: iterate over tree using level-order strategy returning group for every level
26 * :any:`ZigZagGroupIter`: iterate over tree using level-order strategy returning group for every level
27
28 **Tree Rendering**
29
30 * :any:`RenderTree` using the following styles:
31 * :any:`AsciiStyle`
32 * :any:`ContStyle`
33 * :any:`ContRoundStyle`
34 * :any:`DoubleStyle`
35
36 Basics
37 ------
38
39 The only tree relevant information is the `parent` attribute.
40 If `None` the node is root node.
41 If set to another node, the node becomes the child of it.
42
43 >>> from anytree import Node, RenderTree
44 >>> udo = Node("Udo")
45 >>> marc = Node("Marc")
46 >>> lian = Node("Lian", parent=marc)
47 >>> print(RenderTree(udo))
48 Node('/Udo')
49 >>> print(RenderTree(marc))
50 Node('/Marc')
51 └── Node('/Marc/Lian')
52
53 Every node has a :any:`children` attribute with a tuple of all children:
54
55 >>> udo.children
56 ()
57 >>> marc.children
58 (Node('/Marc/Lian'),)
59 >>> lian.children
60 ()
61
62 **Add: Single Node Attach**
63
64 Just set the parent attribute and the node becomes a child node:
65
66 >>> marc.parent = udo
67 >>> print(RenderTree(udo))
68 Node('/Udo')
69 └── Node('/Udo/Marc')
70 └── Node('/Udo/Marc/Lian')
71
72 **Delete: Single Node Detach**
73
74 A node becomes a root node, if you set the parent attribute to `None`:
75
76 >>> lian.is_root
77 False
78 >>> lian.parent = None
79 >>> lian.is_root
80 True
81
82 The node is deleted from the tree:
83
84 >>> print(RenderTree(udo))
85 Node('/Udo')
86 └── Node('/Udo/Marc')
87
88 **Modify Multiple Child Nodes**
89
90 Assume the following tree:
91
92 >>> n = Node("n")
93 >>> a = Node("a", parent=n)
94 >>> b = Node("b", parent=n)
95 >>> c = Node("c", parent=n)
96 >>> d = Node("d")
97 >>> n.children
98 (Node('/n/a'), Node('/n/b'), Node('/n/c'))
99
100 Modifying the children attribute modifies multiple child nodes.
101 It can be set to any iterable.
102
103 >>> n.children = [a, b]
104 >>> n.children
105 (Node('/n/a'), Node('/n/b'))
106
107 Node `c` is removed from the tree.
108 In case of an existing reference, the node `c` does not vanish and is the root of its own tree.
109
110 >>> c
111 Node('/c')
112
113 Adding works likewise.
114
115 >>> d
116 Node('/d')
117 >>> n.children = [a, b, d]
118 >>> n.children
119 (Node('/n/a'), Node('/n/b'), Node('/n/d'))
120 >>> d
121 Node('/n/d')
122
123
124 Detach/Attach Protocol
125 ----------------------
126
127 A node class implementation might implement the notification slots
128 ``_pre_detach(parent)``, ``_post_detach(parent)``,
129 ``_pre_attach(parent)``, ``_post_attach(parent)``.
130
131 These methods are *protected* methods,
132 intended to be overwritten by child classes of :any:`NodeMixin`/:any:`Node`.
133 They are called on modifications of a nodes `parent` attribute.
134 Never call them directly from API.
135 This will corrupt the logic behind these methods.
136
137 >>> class NotifiedNode(Node):
138 ... def _pre_detach(self, parent):
139 ... print("_pre_detach", parent)
140 ... def _post_detach(self, parent):
141 ... print("_post_detach", parent)
142 ... def _pre_attach(self, parent):
143 ... print("_pre_attach", parent)
144 ... def _post_attach(self, parent):
145 ... print("_post_attach", parent)
146
147 Notification on attach:
148
149 >>> a = NotifiedNode("a")
150 >>> b = NotifiedNode("b")
151 >>> c = NotifiedNode("c")
152 >>> c.parent = a
153 _pre_attach NotifiedNode('/a')
154 _post_attach NotifiedNode('/a')
155
156 Notification on change:
157
158 >>> c.parent = b
159 _pre_detach NotifiedNode('/a')
160 _post_detach NotifiedNode('/a')
161 _pre_attach NotifiedNode('/b')
162 _post_attach NotifiedNode('/b')
163
164 If the parent equals the old value, the notification is not triggered:
165
166 >>> c.parent = b
167
168 Notification on detach:
169
170 >>> c.parent = None
171 _pre_detach NotifiedNode('/b')
172 _post_detach NotifiedNode('/b')
173
174
175 .. important::
176 An exception raised by ``_pre_detach(parent)`` and ``_pre_attach(parent)`` will **prevent** the tree structure to be updated.
177 The node keeps the old state.
178 An exception raised by ``_post_detach(parent)`` and ``_post_attach(parent)`` does **not rollback** the tree structure modification.
179
180
181 Custom Separator
182 ----------------
183
184 By default a slash character (`/`) separates nodes.
185 This separator can be overwritten:
186
187 >>> class MyNode(Node):
188 ... separator = "|"
189
190 >>> udo = MyNode("Udo")
191 >>> dan = MyNode("Dan", parent=udo)
192 >>> marc = MyNode("Marc", parent=udo)
193 >>> print(RenderTree(udo))
194 MyNode('|Udo')
195 ├── MyNode('|Udo|Dan')
196 └── MyNode('|Udo|Marc')
197
198 The resolver takes the custom separator also into account:
199
200 >>> from anytree import Resolver
201 >>> r = Resolver()
202 >>> r.glob(udo, "|Udo|*")
203 [MyNode('|Udo|Dan'), MyNode('|Udo|Marc')]