Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mar10 committed Nov 1, 2024
1 parent 88beac7 commit 2252690
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 96 deletions.
152 changes: 101 additions & 51 deletions docs/jupyter/take_the_tour.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@
"output_type": "stream",
"text": [
"Tree<'Organization'>\n",
"├── <__main__.Department object at 0x10da423f0>\n",
"│ ├── <__main__.Department object at 0x107ca8860>\n",
"│ │ ╰── <__main__.Person object at 0x10dbcea20>\n",
"│ ╰── <__main__.Person object at 0x10dbce2a0>\n",
"├── <__main__.Department object at 0x10da419d0>\n",
"│ ╰── <__main__.Person object at 0x10dbce390>\n",
"╰── <__main__.Person object at 0x10dbce1b0>\n"
"├── <__main__.Department object at 0x107bd3cb0>\n",
"│ ├── <__main__.Department object at 0x1078ea0f0>\n",
"│ │ ╰── <__main__.Person object at 0x107902180>\n",
"│ ╰── <__main__.Person object at 0x1079015b0>\n",
"├── <__main__.Department object at 0x1078eb560>\n",
"│ ╰── <__main__.Person object at 0x109ab25a0>\n",
"╰── <__main__.Person object at 0x107900980>\n"
]
}
],
Expand Down Expand Up @@ -221,7 +221,7 @@
{
"data": {
"text/plain": [
"Node<'Person<Alice (25)>', data_id=282840603>"
"Node<'Person<Alice (25)>', data_id=276365464>"
]
},
"execution_count": 6,
Expand Down Expand Up @@ -267,7 +267,9 @@
{
"data": {
"text/plain": [
"[]"
"[Node<'Person<Claire (45)>', data_id=276365848>,\n",
" Node<'Department<Marketing>', data_id=276360022>,\n",
" Node<'Person<Alice (25)>', data_id=276365464>]"
]
},
"execution_count": 8,
Expand All @@ -276,7 +278,7 @@
}
],
"source": [
"tree.find_all(lambda node: \"i\" in str(node.data))"
"tree.find_all(match=lambda node: \"i\" in node.data.name)"
]
},
{
Expand All @@ -300,8 +302,8 @@
"output_type": "stream",
"text": [
"Tree<'Organization'>\n",
"╰── Node<'Department<Development>', data_id=a8a94901-3e87-4c71-832a-95a3678c406f>\n",
" ╰── Node<'Person<Bob (35)>', data_id=1055ff94-ac20-4642-95ef-a92a650a3978>\n"
"╰── Node<'Department<Development>', data_id=571ace0b-1a49-44d4-ab52-97320e945d69>\n",
" ╰── Node<'Person<Bob (35)>', data_id=b4cbb694-a18d-420b-ad03-cda9fd0bf7f9>\n"
]
}
],
Expand All @@ -327,7 +329,7 @@
{
"data": {
"text/plain": [
"Node<'Person<Bob (35)>', data_id=1055ff94-ac20-4642-95ef-a92a650a3978>"
"Node<'Person<Bob (35)>', data_id=b4cbb694-a18d-420b-ad03-cda9fd0bf7f9>"
]
},
"execution_count": 10,
Expand Down Expand Up @@ -399,18 +401,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Development\n",
"Marketing\n",
"Alice\n",
"Test\n",
"Bob\n",
"Dave\n",
"Claire\n"
"Development, Marketing, Alice, Test, Bob, Dave, Claire, "
]
}
],
"source": [
"tree.visit(lambda node, memo: print(node.data.name), method=IterMethod.LEVEL_ORDER)"
"def callback(node, memo):\n",
" print(node.data.name, end=\", \")\n",
"\n",
"\n",
"tree.visit(callback, method=IterMethod.LEVEL_ORDER)"
]
},
{
Expand Down Expand Up @@ -442,7 +442,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Filter"
"## Filter\n",
"\n",
"We can create a filtered copy like so:"
]
},
{
Expand All @@ -455,14 +457,41 @@
"output_type": "stream",
"text": [
"Tree<\"Copy of Tree<'Organization'>\">\n",
"├── Node<'Department<Development>', data_id=282739263>\n",
"│ ╰── Node<'Department<Test>', data_id=276605062>\n",
"╰── Node<'Department<Marketing>', data_id=282739101>\n"
"├── Node<'Department<Development>', data_id=276550603>\n",
"│ ╰── Node<'Department<Test>', data_id=276359695>\n",
"╰── Node<'Department<Marketing>', data_id=276360022>\n"
]
}
],
"source": [
"tree_copy = tree.filtered(lambda node: isinstance(node.data, Department))\n",
"tree_copy.print(repr=\"{node}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In-place filtering is also available:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tree<\"Copy of Tree<'Organization'>\">\n",
"├── Node<'Department<Development>', data_id=276550603>\n",
"╰── Node<'Department<Marketing>', data_id=276360022>\n"
]
}
],
"source": [
"tree_copy = tree.copy(predicate=lambda node: isinstance(node.data, Department))\n",
"tree_copy.filter(lambda node: \"m\" in node.data.name.lower())\n",
"tree_copy.print(repr=\"{node}\")"
]
},
Expand All @@ -479,7 +508,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -508,6 +537,13 @@
"tree.print(repr=\"{node.data}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read the user guide for more."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -521,21 +557,21 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Node<'Department<Development>', data_id=282739263>\n",
"├── Node<'Department<Test>', data_id=276605062>\n",
"│ ╰── Node<'Person<Claire (45)>', data_id=282840738>\n",
"╰── Node<'Person<Alice (25)>', data_id=282840603>\n",
"Node<'Department<Marketing>', data_id=282739101>\n",
"├── Node<'Person<Dave (55)>', data_id=282840633>\n",
"╰── Node<'Person<Bob (35)>', data_id=282840618>\n",
"Node<'Person<Alice (25)>', data_id=282840603>\n"
"Node<'Department<Development>', data_id=276550603>\n",
"├── Node<'Department<Test>', data_id=276359695>\n",
"│ ╰── Node<'Person<Claire (45)>', data_id=276365848>\n",
"╰── Node<'Person<Alice (25)>', data_id=276365464>\n",
"Node<'Department<Marketing>', data_id=276360022>\n",
"├── Node<'Person<Dave (55)>', data_id=278573658>\n",
"╰── Node<'Person<Bob (35)>', data_id=276365659>\n",
"Node<'Person<Alice (25)>', data_id=276365464>\n"
]
}
],
Expand All @@ -545,15 +581,15 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Node<'Person<Alice (25)>', data_id=282840603>, parent=None\n",
"Node<'Person<Alice (25)>', data_id=282840603>, parent=Node<'Department<Development>', data_id=282739263>\n"
"Node<'Person<Alice (25)>', data_id=276365464>, parent=None\n",
"Node<'Person<Alice (25)>', data_id=276365464>, parent=Node<'Department<Development>', data_id=276550603>\n"
]
}
],
Expand All @@ -569,19 +605,20 @@
"## Special Data Types\n",
"### Plain Strings\n",
"\n",
"We can add simple string objects the same way as any other object"
"Plain strings are hashable objects, so we can handle them the same way as any \n",
"other object:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tree<'4525455792'>\n",
"Tree<'4456612640'>\n",
"├── 'A'\n",
"│ ├── 'a1'\n",
"│ ╰── 'a2'\n",
Expand All @@ -606,23 +643,26 @@
"\n",
"We cannot add Python `dict` objects to a tree, because nutree cannot derive\n",
"a *data_id* for unhashable types. <br>\n",
"A a workaround, we can wrap it inside `DictWrapper` objects:"
"We can handle this by passing a *data_id* explicitly to `add()` and similar \n",
"methods, or implement a `calc_data_id` callback as shown before.\n",
"\n",
"As another workaround, we can wrap it inside `DictWrapper` objects:"
]
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tree<'4525453728'>\n",
"├── Node<'A', data_id=6789757684237002091>\n",
"│ ╰── Node<\"DictWrapper<{'title': 'foo', 'id': 1}>\", data_id=4425631040>\n",
"╰── Node<'B', data_id=-9085030021599920704>\n",
" ╰── Node<\"DictWrapper<{'title': 'foo', 'id': 1}>\", data_id=4425631040>\n"
"Tree<'4457449184'>\n",
"├── Node<'A', data_id=-2140708064199008729>\n",
"│ ╰── Node<\"DictWrapper<{'title': 'foo', 'id': 1}>\", data_id=4421803904>\n",
"╰── Node<'B', data_id=-6374143811137841581>\n",
" ╰── Node<\"DictWrapper<{'title': 'foo', 'id': 1}>\", data_id=4421803904>\n"
]
}
],
Expand Down Expand Up @@ -662,14 +702,14 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TypedTree<'4425583216'>\n",
"TypedTree<'4415355264'>\n",
"╰── friend → Mia\n",
" ├── brother → Noah\n",
" ╰── sister → Olivia\n"
Expand All @@ -685,6 +725,16 @@
")\n",
"typed_tree.print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Typing\n",
"\n",
"Nutree comes fully typed (passing [pyright](https://microsoft.github.io/pyright/#/) \n",
"standard checks). This improves type-safety and auto-complete features in your IDE."
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 2252690

Please sign in to comment.