|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Nutree Tutorial" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "metadata": {}, |
| 14 | + "source": [ |
| 15 | + "Nutree organizes arbitrary object instances in an unobtrusive way. <br>\n", |
| 16 | + "That means, we can add existing objects without havin to derrive from a common \n", |
| 17 | + "base class or having them implement a specific protocol." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "# Setup some sample classes and objects" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 31, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "import uuid\n", |
| 34 | + "\n", |
| 35 | + "\n", |
| 36 | + "class Department:\n", |
| 37 | + " def __init__(self, name: str):\n", |
| 38 | + " self.guid = uuid.uuid4()\n", |
| 39 | + " self.name = name\n", |
| 40 | + "\n", |
| 41 | + " def __str__(self):\n", |
| 42 | + " return f\"Department<{self.name}>\"\n", |
| 43 | + "\n", |
| 44 | + "\n", |
| 45 | + "class Person:\n", |
| 46 | + " def __init__(self, name: str, age: int):\n", |
| 47 | + " self.guid = uuid.uuid4()\n", |
| 48 | + " self.name = name\n", |
| 49 | + " self.age = age\n", |
| 50 | + "\n", |
| 51 | + " def __str__(self):\n", |
| 52 | + " return f\"Person<{self.name} ({self.age})>\"" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "Now create some instances" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": 32, |
| 65 | + "metadata": {}, |
| 66 | + "outputs": [], |
| 67 | + "source": [ |
| 68 | + "development_dep = Department(\"Development\")\n", |
| 69 | + "test__dep = Department(\"Test\")\n", |
| 70 | + "marketing_dep = Department(\"Marketing\")\n", |
| 71 | + "\n", |
| 72 | + "alice = Person(\"Alice\", 25)\n", |
| 73 | + "bob = Person(\"Bob\", 35)\n", |
| 74 | + "claire = Person(\"Claire\", 45)\n", |
| 75 | + "dave = Person(\"Dave\", 55)" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "attachments": {}, |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "Let's organize these objects in a hierarchical structure:" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 33, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [ |
| 91 | + { |
| 92 | + "name": "stdout", |
| 93 | + "output_type": "stream", |
| 94 | + "text": [ |
| 95 | + "Tree<'Organization'>\n", |
| 96 | + "├── <__main__.Department object at 0x111f04e00>\n", |
| 97 | + "│ ├── <__main__.Department object at 0x111b89f70>\n", |
| 98 | + "│ │ ╰── <__main__.Person object at 0x111f05520>\n", |
| 99 | + "│ ╰── <__main__.Person object at 0x111f04da0>\n", |
| 100 | + "├── <__main__.Department object at 0x111edac90>\n", |
| 101 | + "│ ╰── <__main__.Person object at 0x111f06a50>\n", |
| 102 | + "╰── <__main__.Person object at 0x111ed9880>\n" |
| 103 | + ] |
| 104 | + } |
| 105 | + ], |
| 106 | + "source": [ |
| 107 | + "from nutree import Tree\n", |
| 108 | + "\n", |
| 109 | + "tree = Tree(\"Organization\")\n", |
| 110 | + "\n", |
| 111 | + "dev_node = tree.add(development_dep)\n", |
| 112 | + "test_node = dev_node.add(test__dep)\n", |
| 113 | + "mkt_node = tree.add(marketing_dep)\n", |
| 114 | + "\n", |
| 115 | + "tree.add(alice)\n", |
| 116 | + "dev_node.add(bob)\n", |
| 117 | + "test_node.add(claire)\n", |
| 118 | + "mkt_node.add(dave)\n", |
| 119 | + "\n", |
| 120 | + "tree.print()" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "markdown", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "Tree nodes store a reference to the object in the `node.data` attribute.\n", |
| 128 | + "\n", |
| 129 | + "The nodes are formatted by the object's `__repr__` implementation by default. <br>\n", |
| 130 | + "We can overide ths by passing an f-string as `repr` argument:" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": 34, |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [ |
| 138 | + { |
| 139 | + "name": "stdout", |
| 140 | + "output_type": "stream", |
| 141 | + "text": [ |
| 142 | + "Tree<'Organization'>\n", |
| 143 | + "├── Department<Development>\n", |
| 144 | + "│ ├── Department<Test>\n", |
| 145 | + "│ │ ╰── Person<Claire (45)>\n", |
| 146 | + "│ ╰── Person<Bob (35)>\n", |
| 147 | + "├── Department<Marketing>\n", |
| 148 | + "│ ╰── Person<Dave (55)>\n", |
| 149 | + "╰── Person<Alice (25)>\n" |
| 150 | + ] |
| 151 | + } |
| 152 | + ], |
| 153 | + "source": [ |
| 154 | + "tree.print(repr=\"{node.data}\")" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "markdown", |
| 159 | + "metadata": {}, |
| 160 | + "source": [ |
| 161 | + "# Iteration and Searching" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "# Mutation" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "markdown", |
| 173 | + "metadata": {}, |
| 174 | + "source": [ |
| 175 | + "# Data IDs and Clones" |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "code", |
| 180 | + "execution_count": 35, |
| 181 | + "metadata": {}, |
| 182 | + "outputs": [ |
| 183 | + { |
| 184 | + "name": "stdout", |
| 185 | + "output_type": "stream", |
| 186 | + "text": [ |
| 187 | + "Node<'Department<Development>', data_id=287245536>\n", |
| 188 | + "├── Node<'Department<Test>', data_id=287017463>\n", |
| 189 | + "│ ╰── Node<'Person<Claire (45)>', data_id=287245650>\n", |
| 190 | + "╰── Node<'Person<Bob (35)>', data_id=287245530>\n", |
| 191 | + "Node<'Department<Marketing>', data_id=287234761>\n", |
| 192 | + "╰── Node<'Person<Dave (55)>', data_id=287245989>\n", |
| 193 | + "Node<'Person<Alice (25)>', data_id=287234440>\n" |
| 194 | + ] |
| 195 | + } |
| 196 | + ], |
| 197 | + "source": [ |
| 198 | + "tree.print(repr=\"{node}\", title=False)" |
| 199 | + ] |
| 200 | + }, |
| 201 | + { |
| 202 | + "cell_type": "markdown", |
| 203 | + "metadata": {}, |
| 204 | + "source": [ |
| 205 | + "# Serialization" |
| 206 | + ] |
| 207 | + }, |
| 208 | + { |
| 209 | + "cell_type": "code", |
| 210 | + "execution_count": 36, |
| 211 | + "metadata": {}, |
| 212 | + "outputs": [ |
| 213 | + { |
| 214 | + "data": { |
| 215 | + "text/plain": [ |
| 216 | + "[{'data': 'Department<Development>',\n", |
| 217 | + " 'children': [{'data': 'Department<Test>',\n", |
| 218 | + " 'children': [{'data': 'Person<Claire (45)>'}]},\n", |
| 219 | + " {'data': 'Person<Bob (35)>'}]},\n", |
| 220 | + " {'data': 'Department<Marketing>',\n", |
| 221 | + " 'children': [{'data': 'Person<Dave (55)>'}]},\n", |
| 222 | + " {'data': 'Person<Alice (25)>'}]" |
| 223 | + ] |
| 224 | + }, |
| 225 | + "execution_count": 36, |
| 226 | + "metadata": {}, |
| 227 | + "output_type": "execute_result" |
| 228 | + } |
| 229 | + ], |
| 230 | + "source": [ |
| 231 | + "tree.to_dict_list()" |
| 232 | + ] |
| 233 | + }, |
| 234 | + { |
| 235 | + "cell_type": "code", |
| 236 | + "execution_count": 37, |
| 237 | + "metadata": {}, |
| 238 | + "outputs": [ |
| 239 | + { |
| 240 | + "data": { |
| 241 | + "text/plain": [ |
| 242 | + "[(0, {}), (1, {}), (2, {}), (1, {}), (0, {}), (5, {}), (0, {})]" |
| 243 | + ] |
| 244 | + }, |
| 245 | + "execution_count": 37, |
| 246 | + "metadata": {}, |
| 247 | + "output_type": "execute_result" |
| 248 | + } |
| 249 | + ], |
| 250 | + "source": [ |
| 251 | + "list(tree.to_list_iter())" |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "code", |
| 256 | + "execution_count": null, |
| 257 | + "metadata": {}, |
| 258 | + "outputs": [], |
| 259 | + "source": [] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": 38, |
| 264 | + "metadata": {}, |
| 265 | + "outputs": [ |
| 266 | + { |
| 267 | + "name": "stdout", |
| 268 | + "output_type": "stream", |
| 269 | + "text": [ |
| 270 | + "Tree<'4595934048'>\n", |
| 271 | + "╰── 'A'\n" |
| 272 | + ] |
| 273 | + } |
| 274 | + ], |
| 275 | + "source": [ |
| 276 | + "t = Tree._from_list([(0, \"A\")])\n", |
| 277 | + "print(t.format())" |
| 278 | + ] |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "code", |
| 282 | + "execution_count": null, |
| 283 | + "metadata": {}, |
| 284 | + "outputs": [], |
| 285 | + "source": [] |
| 286 | + } |
| 287 | + ], |
| 288 | + "metadata": { |
| 289 | + "kernelspec": { |
| 290 | + "display_name": ".venv", |
| 291 | + "language": "python", |
| 292 | + "name": "python3" |
| 293 | + }, |
| 294 | + "language_info": { |
| 295 | + "codemirror_mode": { |
| 296 | + "name": "ipython", |
| 297 | + "version": 3 |
| 298 | + }, |
| 299 | + "file_extension": ".py", |
| 300 | + "mimetype": "text/x-python", |
| 301 | + "name": "python", |
| 302 | + "nbconvert_exporter": "python", |
| 303 | + "pygments_lexer": "ipython3", |
| 304 | + "version": "3.12.6" |
| 305 | + }, |
| 306 | + "orig_nbformat": 4 |
| 307 | + }, |
| 308 | + "nbformat": 4, |
| 309 | + "nbformat_minor": 2 |
| 310 | +} |
0 commit comments