Skip to content

Commit

Permalink
Generic data (#13)
Browse files Browse the repository at this point in the history
* Update fixture.py

* Define a concept

* Initial refactoring

* Fix warnings
  • Loading branch information
mar10 authored Nov 3, 2024
1 parent 8f9b701 commit 3682ae4
Show file tree
Hide file tree
Showing 15 changed files with 699 additions and 423 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
- tree.to_rdf() is now available for Tree (not only TypedTree).
- New method `node.up()` allows method chaining when adding nodes.
- Pass pyright 'typeCheckingMode = "standard"'.
- Use generic typing for improved type checking.
- Use generic typing for improved type checking, e.g. use `tree = Tree[Animals]()`
to create a type-aware container.

## 0.9.0 (2024-09-12)

Expand Down
141 changes: 100 additions & 41 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 0x109bbf6b0>\n",
"│ ├── <__main__.Department object at 0x1078baa50>\n",
"│ │ ╰── <__main__.Person object at 0x109cb23c0>\n",
"│ ╰── <__main__.Person object at 0x109cb2300>\n",
"├── <__main__.Department object at 0x1078b8f20>\n",
"│ ╰── <__main__.Person object at 0x109cb2060>\n",
"╰── <__main__.Person object at 0x109cb1f70>\n"
"├── <__main__.Department object at 0x114bc4140>\n",
"│ ├── <__main__.Department object at 0x114bc4440>\n",
"│ │ ╰── <__main__.Person object at 0x114bc4740>\n",
"│ ╰── <__main__.Person object at 0x114bc46b0>\n",
"├── <__main__.Department object at 0x114bc45f0>\n",
"│ ╰── <__main__.Person object at 0x114bc47a0>\n",
"╰── <__main__.Person object at 0x114bc4590>\n"
]
}
],
Expand Down Expand Up @@ -221,7 +221,7 @@
{
"data": {
"text/plain": [
"Node<'Person<Alice (25)>', data_id=278704631>"
"Node<'Person<Alice (25)>', data_id=290178137>"
]
},
"execution_count": 6,
Expand Down Expand Up @@ -267,9 +267,9 @@
{
"data": {
"text/plain": [
"[Node<'Person<Claire (45)>', data_id=278704700>,\n",
" Node<'Department<Marketing>', data_id=276347122>,\n",
" Node<'Person<Alice (25)>', data_id=278704631>]"
"[Node<'Person<Claire (45)>', data_id=290178164>,\n",
" Node<'Department<Marketing>', data_id=290178143>,\n",
" Node<'Person<Alice (25)>', data_id=290178137>]"
]
},
"execution_count": 8,
Expand All @@ -294,16 +294,16 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tree<'Organization'>\n",
"╰── Node<'Department<Development>', data_id=8e6ae0d7-b268-4c9d-8da8-bdc5cdcb4f8d>\n",
" ╰── Node<'Person<Bob (35)>', data_id=ad20de1f-34c4-40cc-a102-51c28fb6ec5b>\n"
"╰── Node<'Department<Development>', data_id=1145f7c6-9a12-4110-a9f7-f84c4d53df77>\n",
" ╰── Node<'Person<Bob (35)>', data_id=a5adfa10-ebbb-4949-9638-6a99dab6165b>\n"
]
}
],
Expand Down Expand Up @@ -331,7 +331,7 @@
{
"data": {
"text/plain": [
"Node<'Person<Bob (35)>', data_id=ad20de1f-34c4-40cc-a102-51c28fb6ec5b>"
"Node<'Person<Bob (35)>', data_id=a5adfa10-ebbb-4949-9638-6a99dab6165b>"
]
},
"execution_count": 10,
Expand All @@ -354,7 +354,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -396,7 +396,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -459,9 +459,9 @@
"output_type": "stream",
"text": [
"Tree<\"Copy of Tree<'Organization'>\">\n",
"├── Node<'Department<Development>', data_id=278642539>\n",
"│ ╰── Node<'Department<Test>', data_id=276347557>\n",
"╰── Node<'Department<Marketing>', data_id=276347122>\n"
"├── Node<'Department<Development>', data_id=290178068>\n",
"│ ╰── Node<'Department<Test>', data_id=290178116>\n",
"╰── Node<'Department<Marketing>', data_id=290178143>\n"
]
}
],
Expand All @@ -487,8 +487,8 @@
"output_type": "stream",
"text": [
"Tree<\"Copy of Tree<'Organization'>\">\n",
"├── Node<'Department<Development>', data_id=278642539>\n",
"╰── Node<'Department<Marketing>', data_id=276347122>\n"
"├── Node<'Department<Development>', data_id=290178068>\n",
"╰── Node<'Department<Marketing>', data_id=290178143>\n"
]
}
],
Expand Down Expand Up @@ -567,14 +567,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Node<'Department<Development>', data_id=278642539>\n",
"├── Node<'Department<Test>', data_id=276347557>\n",
"│ ╰── Node<'Person<Claire (45)>', data_id=278704700>\n",
"╰── Node<'Person<Alice (25)>', data_id=278704631>\n",
"Node<'Department<Marketing>', data_id=276347122>\n",
"├── Node<'Person<Dave (55)>', data_id=278704646>\n",
"╰── Node<'Person<Bob (35)>', data_id=278704688>\n",
"Node<'Person<Alice (25)>', data_id=278704631>\n"
"Node<'Department<Development>', data_id=290178068>\n",
"├── Node<'Department<Test>', data_id=290178116>\n",
"│ ╰── Node<'Person<Claire (45)>', data_id=290178164>\n",
"╰── Node<'Person<Alice (25)>', data_id=290178137>\n",
"Node<'Department<Marketing>', data_id=290178143>\n",
"├── Node<'Person<Dave (55)>', data_id=290178170>\n",
"╰── Node<'Person<Bob (35)>', data_id=290178155>\n",
"Node<'Person<Alice (25)>', data_id=290178137>\n"
]
}
],
Expand All @@ -591,8 +591,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Node<'Person<Alice (25)>', data_id=278704631>, parent=None\n",
"Node<'Person<Alice (25)>', data_id=278704631>, parent=Node<'Department<Development>', data_id=278642539>\n"
"Node<'Person<Alice (25)>', data_id=290178137>, parent=None\n",
"Node<'Person<Alice (25)>', data_id=290178137>, parent=Node<'Department<Development>', data_id=290178068>\n"
]
}
],
Expand Down Expand Up @@ -621,7 +621,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Tree<'4459280720'>\n",
"Tree<'4642856144'>\n",
"├── 'A'\n",
"│ ├── 'a1'\n",
"│ ╰── 'a2'\n",
Expand Down Expand Up @@ -661,11 +661,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Tree<'4459281584'>\n",
"├── Node<'A', data_id=646631547239422124>\n",
"│ ╰── Node<\"DictWrapper<{'title': 'foo', 'id': 1}>\", data_id=4459448768>\n",
"╰── Node<'B', data_id=-8358617541791855429>\n",
" ╰── Node<\"DictWrapper<{'title': 'foo', 'id': 1}>\", data_id=4459448768>\n"
"Tree<'4626598512'>\n",
"├── Node<'A', data_id=5473560910289120105>\n",
"│ ╰── Node<\"DictWrapper<{'title': 'foo', 'id': 1}>\", data_id=4628284480>\n",
"╰── Node<'B', data_id=5825221258629577999>\n",
" ╰── Node<\"DictWrapper<{'title': 'foo', 'id': 1}>\", data_id=4628284480>\n"
]
}
],
Expand Down Expand Up @@ -702,7 +702,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"TypedTree<'4459281248'>\n",
"TypedTree<'4626500208'>\n",
"╰── friend → Mia\n",
" ├── brother → Noah\n",
" ╰── sister → Olivia\n"
Expand Down Expand Up @@ -739,10 +739,69 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Typing\n",
"# Type Hints\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."
"standard checks). This improves type-safety and auto-complete features inside \n",
"IDEs.\n",
"\n",
"Assuming we have a hierrchy of objects:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"class OrgaEntry:\n",
" def __init__(self, name: str):\n",
" self.name: str = name\n",
" self.guid = uuid.uuid4()\n",
"\n",
"\n",
"class Person(OrgaEntry):\n",
" def __init__(self, name: str, age: int):\n",
" super().__init__(name)\n",
" self.age: int = age\n",
"\n",
"\n",
"class Department(OrgaEntry):\n",
" def __init__(self, name: str):\n",
" super().__init__(name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now create a type-safe tree to store these objects:"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b871d538-8505-464c-992f-a1bf6a781711\n"
]
}
],
"source": [
"tree = Tree[OrgaEntry]()\n",
"\n",
"dev = tree.add(Department(\"Development\"))\n",
"alice = dev.add(Person(\"Alice\", 25))\n",
"\n",
"tree.add(42) # Flags a typing error in the IDE (but not at runtime)\n",
"\n",
"# IDE infers that the type `OrgaEntry` is stored in the `data` attribute\n",
"# and provides code completion\n",
"print(alice.data.guid)"
]
}
],
Expand Down
Loading

0 comments on commit 3682ae4

Please sign in to comment.