Skip to content

Commit

Permalink
Update walkthrough.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
mar10 committed Oct 13, 2024
1 parent eee7b0d commit 66d6388
Showing 1 changed file with 77 additions and 66 deletions.
143 changes: 77 additions & 66 deletions docs/jupyter/walkthrough.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -95,21 +130,21 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"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"
]
}
],
Expand Down Expand Up @@ -142,7 +177,7 @@
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": 21,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -174,16 +209,16 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Node<'Person<Alice (25)>', data_id=275387048>"
"Node<'Person<Alice (25)>', data_id=274230066>"
]
},
"execution_count": 66,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -194,7 +229,7 @@
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -220,7 +255,7 @@
},
{
"cell_type": "code",
"execution_count": 68,
"execution_count": 24,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -254,20 +289,20 @@
},
{
"cell_type": "code",
"execution_count": 69,
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Node<'Department<Development>', data_id=275386733>\n",
"├── Node<'Department<Test>', data_id=275386904>\n",
"│ ╰── Node<'Person<Claire (45)>', data_id=275386868>\n",
"╰── Node<'Person<Bob (35)>', data_id=274729262>\n",
"Node<'Department<Marketing>', data_id=275386451>\n",
"╰── Node<'Person<Dave (55)>', data_id=275386628>\n",
"Node<'Person<Alice (25)>', data_id=275387048>\n"
"Node<'Department<Development>', data_id=274229715>\n",
"├── Node<'Department<Test>', data_id=274230048>\n",
"│ ╰── Node<'Person<Claire (45)>', data_id=274230138>\n",
"╰── Node<'Person<Bob (35)>', data_id=274230120>\n",
"Node<'Department<Marketing>', data_id=274229406>\n",
"╰── Node<'Person<Dave (55)>', data_id=274229751>\n",
"Node<'Person<Alice (25)>', data_id=274230066>\n"
]
}
],
Expand All @@ -284,7 +319,7 @@
},
{
"cell_type": "code",
"execution_count": 70,
"execution_count": 26,
"metadata": {},
"outputs": [
{
Expand All @@ -299,7 +334,7 @@
" {'data': 'Person<Alice (25)>'}]"
]
},
"execution_count": 70,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -310,7 +345,7 @@
},
{
"cell_type": "code",
"execution_count": 71,
"execution_count": 27,
"metadata": {},
"outputs": [
{
Expand All @@ -319,7 +354,7 @@
"[(0, {}), (1, {}), (2, {}), (1, {}), (0, {}), (5, {}), (0, {})]"
]
},
"execution_count": 71,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -397,7 +432,7 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": 30,
"metadata": {},
"outputs": [
{
Expand All @@ -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'"
]
}
Expand All @@ -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",
Expand Down Expand Up @@ -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()"
]
Expand Down

0 comments on commit 66d6388

Please sign in to comment.