From 66d63881e7fd9ffcfa14414ddebb122bbc9183e2 Mon Sep 17 00:00:00 2001 From: Martin Wendt Date: Sun, 13 Oct 2024 09:03:11 +0200 Subject: [PATCH] Update walkthrough.ipynb --- docs/jupyter/walkthrough.ipynb | 143 ++++++++++++++++++--------------- 1 file changed, 77 insertions(+), 66 deletions(-) diff --git a/docs/jupyter/walkthrough.ipynb b/docs/jupyter/walkthrough.ipynb index 96be2ee..ea39645 100644 --- a/docs/jupyter/walkthrough.ipynb +++ b/docs/jupyter/walkthrough.ipynb @@ -17,6 +17,40 @@ "base class or implement a specific protocol." ] }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tree<'Hello'>\n", + "├── 'N'\n", + "│ ╰── 'u'\n", + "╰── 'T'\n", + " ├── 'r'\n", + " ╰── 'ee'\n" + ] + } + ], + "source": [ + "from nutree import Tree\n", + "\n", + "tree = Tree(\"Hello\")\n", + "tree.add(\"N\").add(\"u\").up(2).add(\"T\").add(\"r\").up().add(\"ee\")\n", + "tree.print()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Strings can be added to a tree, but in a real-world scenario we want need to \n", + "handle ordinary objects:" + ] + }, { "cell_type": "markdown", "metadata": { @@ -25,12 +59,13 @@ } }, "source": [ - "# Setup some sample classes and objects" + "# Setup some sample classes and objects\n", + "Let's define a simple class hierarchy" ] }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -60,12 +95,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now create some instances" + "and create some instances" ] }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -95,7 +130,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -103,13 +138,13 @@ "output_type": "stream", "text": [ "Tree<'Organization'>\n", - "├── <__main__.Department object at 0x106a116d0>\n", - "│ ├── <__main__.Department object at 0x106a12180>\n", - "│ │ ╰── <__main__.Person object at 0x106a11f40>\n", - "│ ╰── <__main__.Person object at 0x1060092e0>\n", - "├── <__main__.Department object at 0x106a10530>\n", - "│ ╰── <__main__.Person object at 0x106a11040>\n", - "╰── <__main__.Person object at 0x106a12a80>\n" + "├── <__main__.Department object at 0x105869d30>\n", + "│ ├── <__main__.Department object at 0x10586b200>\n", + "│ │ ╰── <__main__.Person object at 0x10586b7a0>\n", + "│ ╰── <__main__.Person object at 0x10586b680>\n", + "├── <__main__.Department object at 0x1058689e0>\n", + "│ ╰── <__main__.Person object at 0x105869f70>\n", + "╰── <__main__.Person object at 0x10586b320>\n" ] } ], @@ -142,7 +177,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -174,16 +209,16 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "Node<'Person', data_id=275387048>" + "Node<'Person', data_id=274230066>" ] }, - "execution_count": 66, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -194,7 +229,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -220,7 +255,7 @@ }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -254,20 +289,20 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Node<'Department', data_id=275386733>\n", - "├── Node<'Department', data_id=275386904>\n", - "│ ╰── Node<'Person', data_id=275386868>\n", - "╰── Node<'Person', data_id=274729262>\n", - "Node<'Department', data_id=275386451>\n", - "╰── Node<'Person', data_id=275386628>\n", - "Node<'Person', data_id=275387048>\n" + "Node<'Department', data_id=274229715>\n", + "├── Node<'Department', data_id=274230048>\n", + "│ ╰── Node<'Person', data_id=274230138>\n", + "╰── Node<'Person', data_id=274230120>\n", + "Node<'Department', data_id=274229406>\n", + "╰── Node<'Person', data_id=274229751>\n", + "Node<'Person', data_id=274230066>\n" ] } ], @@ -284,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 26, "metadata": {}, "outputs": [ { @@ -299,7 +334,7 @@ " {'data': 'Person'}]" ] }, - "execution_count": 70, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -310,7 +345,7 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -319,7 +354,7 @@ "[(0, {}), (1, {}), (2, {}), (1, {}), (0, {}), (5, {}), (0, {})]" ] }, - "execution_count": 71, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -330,14 +365,14 @@ }, { "cell_type": "code", - "execution_count": 72, + "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Tree<'4405227840'>\n", + "Tree<'4387670112'>\n", "├── 'A'\n", "│ ╰── 'C'\n", "│ ╰── 'E'\n", @@ -364,14 +399,14 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Tree<'4406189552'>\n", + "Tree<'4387679616'>\n", "├── 'A'\n", "│ ├── 'a1'\n", "│ ╰── 'a2'\n", @@ -397,7 +432,7 @@ }, { "cell_type": "code", - "execution_count": 74, + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -407,9 +442,9 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[74], line 6\u001b[0m\n\u001b[1;32m 4\u001b[0m tree[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39madd(d, data_id\u001b[38;5;241m=\u001b[39md[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mid\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[1;32m 5\u001b[0m tree[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mB\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39madd(d, data_id\u001b[38;5;241m=\u001b[39md[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mid\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[0;32m----> 6\u001b[0m \u001b[43mtree\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfind\u001b[49m\u001b[43m(\u001b[49m\u001b[43md\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# tree.print(repr=\"{node}\")\u001b[39;00m\n", - "File \u001b[0;32m~/prj/git/nutree/nutree/tree.py:486\u001b[0m, in \u001b[0;36mTree.find_first\u001b[0;34m(self, data, match, data_id, node_id)\u001b[0m\n\u001b[1;32m 484\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m data \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 485\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m data_id \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m--> 486\u001b[0m data_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcalc_data_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 488\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m data_id \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 489\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m match \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", - "File \u001b[0;32m~/prj/git/nutree/nutree/tree.py:198\u001b[0m, in \u001b[0;36mTree.calc_data_id\u001b[0;34m(self, data)\u001b[0m\n\u001b[1;32m 196\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_calc_data_id_hook:\n\u001b[1;32m 197\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_calc_data_id_hook(\u001b[38;5;28mself\u001b[39m, data)\n\u001b[0;32m--> 198\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mhash\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n", + "Cell \u001b[0;32mIn[30], line 6\u001b[0m\n\u001b[1;32m 4\u001b[0m tree[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39madd(d, data_id\u001b[38;5;241m=\u001b[39md[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mid\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[1;32m 5\u001b[0m tree[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mB\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39madd(d, data_id\u001b[38;5;241m=\u001b[39md[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mid\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[0;32m----> 6\u001b[0m \u001b[43mtree\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfind\u001b[49m\u001b[43m(\u001b[49m\u001b[43md\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# tree.print(repr=\"{node}\")\u001b[39;00m\n", + "File \u001b[0;32m~/prj/git/nutree/nutree/tree.py:497\u001b[0m, in \u001b[0;36mTree.find_first\u001b[0;34m(self, data, match, data_id, node_id)\u001b[0m\n\u001b[1;32m 495\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m data \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 496\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m data_id \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m--> 497\u001b[0m data_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcalc_data_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 499\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m data_id \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 500\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m match \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n", + "File \u001b[0;32m~/prj/git/nutree/nutree/tree.py:199\u001b[0m, in \u001b[0;36mTree.calc_data_id\u001b[0;34m(self, data)\u001b[0m\n\u001b[1;32m 197\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_calc_data_id_hook:\n\u001b[1;32m 198\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_calc_data_id_hook(\u001b[38;5;28mself\u001b[39m, data)\n\u001b[0;32m--> 199\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mhash\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'dict'" ] } @@ -426,21 +461,9 @@ }, { "cell_type": "code", - "execution_count": 61, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "ename": "ImportError", - "evalue": "cannot import name 'DictWrapper' from 'nutree.common' (/Users/martin/prj/git/nutree/nutree/common.py)", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[61], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnutree\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcommon\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m DictWrapper\n\u001b[1;32m 4\u001b[0m tree \u001b[38;5;241m=\u001b[39m Tree()\n\u001b[1;32m 5\u001b[0m tree\u001b[38;5;241m.\u001b[39madd(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mA\u001b[39m\u001b[38;5;124m\"\u001b[39m)\u001b[38;5;241m.\u001b[39mup()\u001b[38;5;241m.\u001b[39madd(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mB\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", - "\u001b[0;31mImportError\u001b[0m: cannot import name 'DictWrapper' from 'nutree.common' (/Users/martin/prj/git/nutree/nutree/common.py)" - ] - } - ], + "outputs": [], "source": [ "from nutree import DictWrapper\n", "\n", @@ -469,21 +492,9 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Tree<'4405500464'>\n", - "├── 'A'\n", - "│ ├── 'a1'\n", - "│ ╰── 'a2'\n", - "╰── 'B'\n" - ] - } - ], + "outputs": [], "source": [ "Tree().add(\"A\").add(\"a1\").up().add(\"a2\").up(2).add(\"B\").tree.print()" ]