|
5 | 5 | "cell_type": "markdown",
|
6 | 6 | "metadata": {},
|
7 | 7 | "source": [
|
8 |
| - "# Nutree Tutorial" |
| 8 | + "# Nutree Overview" |
9 | 9 | ]
|
10 | 10 | },
|
11 | 11 | {
|
12 | 12 | "cell_type": "markdown",
|
13 | 13 | "metadata": {},
|
14 | 14 | "source": [
|
15 | 15 | "Nutree organizes arbitrary object instances in an unobtrusive way. <br>\n",
|
16 |
| - "That means, we can add existing objects without having to derrive from a common \n", |
17 |
| - "base class or having them implement a specific protocol." |
| 16 | + "That means, we can add existing objects without having to derive from a common \n", |
| 17 | + "base class or implement a specific protocol." |
18 | 18 | ]
|
19 | 19 | },
|
20 | 20 | {
|
|
30 | 30 | },
|
31 | 31 | {
|
32 | 32 | "cell_type": "code",
|
33 |
| - "execution_count": 10, |
| 33 | + "execution_count": 2, |
34 | 34 | "metadata": {},
|
35 | 35 | "outputs": [],
|
36 | 36 | "source": [
|
|
65 | 65 | },
|
66 | 66 | {
|
67 | 67 | "cell_type": "code",
|
68 |
| - "execution_count": 11, |
| 68 | + "execution_count": 3, |
69 | 69 | "metadata": {},
|
70 | 70 | "outputs": [],
|
71 | 71 | "source": [
|
|
93 | 93 | },
|
94 | 94 | {
|
95 | 95 | "cell_type": "code",
|
96 |
| - "execution_count": 12, |
| 96 | + "execution_count": 4, |
97 | 97 | "metadata": {},
|
98 | 98 | "outputs": [
|
99 | 99 | {
|
100 | 100 | "name": "stdout",
|
101 | 101 | "output_type": "stream",
|
102 | 102 | "text": [
|
103 | 103 | "Tree<'Organization'>\n",
|
104 |
| - "├── <__main__.Department object at 0x106e6e690>\n", |
105 |
| - "│ ├── <__main__.Department object at 0x106e6d640>\n", |
106 |
| - "│ │ ╰── <__main__.Person object at 0x106e6e990>\n", |
107 |
| - "│ ╰── <__main__.Person object at 0x10683b200>\n", |
108 |
| - "├── <__main__.Department object at 0x106e6d7f0>\n", |
109 |
| - "│ ╰── <__main__.Person object at 0x106e6c6b0>\n", |
110 |
| - "╰── <__main__.Person object at 0x106e6e660>\n" |
| 104 | + "├── <__main__.Department object at 0x1072ef050>\n", |
| 105 | + "│ ├── <__main__.Department object at 0x1072bd9a0>\n", |
| 106 | + "│ │ ╰── <__main__.Person object at 0x1074dd790>\n", |
| 107 | + "│ ╰── <__main__.Person object at 0x1072ef770>\n", |
| 108 | + "├── <__main__.Department object at 0x1072eed50>\n", |
| 109 | + "│ ╰── <__main__.Person object at 0x1074dc320>\n", |
| 110 | + "╰── <__main__.Person object at 0x1072ef500>\n" |
111 | 111 | ]
|
112 | 112 | }
|
113 | 113 | ],
|
|
135 | 135 | "Tree nodes store a reference to the object in the `node.data` attribute.\n",
|
136 | 136 | "\n",
|
137 | 137 | "The nodes are formatted by the object's `__repr__` implementation by default. <br>\n",
|
138 |
| - "We can overide ths by passing an f-string as `repr` argument:" |
| 138 | + "We can overide this by passing an f-string as `repr` argument:" |
139 | 139 | ]
|
140 | 140 | },
|
141 | 141 | {
|
142 | 142 | "cell_type": "code",
|
143 |
| - "execution_count": 13, |
| 143 | + "execution_count": 5, |
144 | 144 | "metadata": {},
|
145 | 145 | "outputs": [
|
146 | 146 | {
|
|
162 | 162 | "tree.print(repr=\"{node.data}\")"
|
163 | 163 | ]
|
164 | 164 | },
|
| 165 | + { |
| 166 | + "cell_type": "markdown", |
| 167 | + "metadata": {}, |
| 168 | + "source": [] |
| 169 | + }, |
| 170 | + { |
| 171 | + "cell_type": "markdown", |
| 172 | + "metadata": {}, |
| 173 | + "source": [ |
| 174 | + "## Special Data Types\n", |
| 175 | + "\n", |
| 176 | + "### Plain Strings\n", |
| 177 | + "\n", |
| 178 | + "We can add simple string objects the same way as any other object\n", |
| 179 | + "(Note also, how we make use of code chaining in this example):" |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "cell_type": "code", |
| 184 | + "execution_count": 13, |
| 185 | + "metadata": {}, |
| 186 | + "outputs": [ |
| 187 | + { |
| 188 | + "name": "stdout", |
| 189 | + "output_type": "stream", |
| 190 | + "text": [ |
| 191 | + "Tree<'4578652576'>\n", |
| 192 | + "╰── 'A'\n", |
| 193 | + " ├── 'a1'\n", |
| 194 | + " ╰── 'a2'\n", |
| 195 | + " ╰── 'B'\n" |
| 196 | + ] |
| 197 | + } |
| 198 | + ], |
| 199 | + "source": [ |
| 200 | + "tree = Tree()\n", |
| 201 | + "tree.add(\"A\").add(\"a1\").up().add(\"a2\").up(-1).add(\"B\")\n", |
| 202 | + "tree.print()" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": 11, |
| 208 | + "metadata": {}, |
| 209 | + "outputs": [ |
| 210 | + { |
| 211 | + "name": "stdout", |
| 212 | + "output_type": "stream", |
| 213 | + "text": [ |
| 214 | + "Tree<'4572026816'>\n", |
| 215 | + "├── 'A'\n", |
| 216 | + "│ ├── 'a1'\n", |
| 217 | + "│ ╰── 'a2'\n", |
| 218 | + "╰── 'B'\n" |
| 219 | + ] |
| 220 | + } |
| 221 | + ], |
| 222 | + "source": [ |
| 223 | + "Tree().add(\"A\").add(\"a1\").up().add(\"a2\").up(2).add(\"B\").tree.print()" |
| 224 | + ] |
| 225 | + }, |
| 226 | + { |
| 227 | + "cell_type": "code", |
| 228 | + "execution_count": 7, |
| 229 | + "metadata": {}, |
| 230 | + "outputs": [ |
| 231 | + { |
| 232 | + "name": "stdout", |
| 233 | + "output_type": "stream", |
| 234 | + "text": [ |
| 235 | + "Tree<'4571953776'>\n", |
| 236 | + "├── 'A'\n", |
| 237 | + "│ ╰── 'C'\n", |
| 238 | + "│ ╰── 'E'\n", |
| 239 | + "├── 'B'\n", |
| 240 | + "╰── 'D'\n" |
| 241 | + ] |
| 242 | + } |
| 243 | + ], |
| 244 | + "source": [ |
| 245 | + "t = Tree._from_list([(0, \"A\"), (0, \"B\"), (1, \"C\"), (0, \"D\"), (3, \"E\")])\n", |
| 246 | + "print(t.format())" |
| 247 | + ] |
| 248 | + }, |
165 | 249 | {
|
166 | 250 | "cell_type": "markdown",
|
167 | 251 | "metadata": {},
|
|
172 | 256 | },
|
173 | 257 | {
|
174 | 258 | "cell_type": "code",
|
175 |
| - "execution_count": 25, |
| 259 | + "execution_count": 8, |
176 | 260 | "metadata": {},
|
177 | 261 | "outputs": [
|
178 | 262 | {
|
179 |
| - "data": { |
180 |
| - "text/plain": [ |
181 |
| - "Node<'Person<Alice (25)>', data_id=275672678>" |
182 |
| - ] |
183 |
| - }, |
184 |
| - "execution_count": 25, |
185 |
| - "metadata": {}, |
186 |
| - "output_type": "execute_result" |
| 263 | + "ename": "KeyError", |
| 264 | + "evalue": "'<__main__.Person object at 0x1072ef500>'", |
| 265 | + "output_type": "error", |
| 266 | + "traceback": [ |
| 267 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 268 | + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", |
| 269 | + "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mtree\u001b[49m\u001b[43m[\u001b[49m\u001b[43malice\u001b[49m\u001b[43m]\u001b[49m\n", |
| 270 | + "File \u001b[0;32m~/prj/git/nutree/nutree/tree.py:167\u001b[0m, in \u001b[0;36mTree.__getitem__\u001b[0;34m(self, data)\u001b[0m\n\u001b[1;32m 164\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfind_all(data)\n\u001b[1;32m 166\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m res:\n\u001b[0;32m--> 167\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mdata\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 168\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(res) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 169\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m AmbiguousMatchError(\n\u001b[1;32m 170\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mdata\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m has \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(res)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m occurrences. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 171\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUse tree.find_all() or tree.find_first() to resolve this.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 172\u001b[0m )\n", |
| 271 | + "\u001b[0;31mKeyError\u001b[0m: '<__main__.Person object at 0x1072ef500>'" |
| 272 | + ] |
187 | 273 | }
|
188 | 274 | ],
|
189 | 275 | "source": [
|
|
333 | 419 | "outputs": [],
|
334 | 420 | "source": []
|
335 | 421 | },
|
336 |
| - { |
337 |
| - "cell_type": "code", |
338 |
| - "execution_count": 20, |
339 |
| - "metadata": {}, |
340 |
| - "outputs": [ |
341 |
| - { |
342 |
| - "name": "stdout", |
343 |
| - "output_type": "stream", |
344 |
| - "text": [ |
345 |
| - "Tree<'4410903680'>\n", |
346 |
| - "├── 'A'\n", |
347 |
| - "│ ╰── 'C'\n", |
348 |
| - "│ ╰── 'E'\n", |
349 |
| - "├── 'B'\n", |
350 |
| - "╰── 'D'\n" |
351 |
| - ] |
352 |
| - } |
353 |
| - ], |
354 |
| - "source": [ |
355 |
| - "t = Tree._from_list([(0, \"A\"), (0, \"B\"), (1, \"C\"), (0, \"D\"), (3, \"E\")])\n", |
356 |
| - "print(t.format())" |
357 |
| - ] |
358 |
| - }, |
359 | 422 | {
|
360 | 423 | "cell_type": "code",
|
361 | 424 | "execution_count": null,
|
|
0 commit comments