Skip to content

Commit 32fe3b4

Browse files
authored
Remove optional checks, add benchmark timings (#411)
* feat: nested_key_dict to tree to support child_key=None * feat: tree to nested_key_dict to support child_key=None * docs: update CHANGELOG * docs: add benchmark timing ipynb * style: shift method
1 parent 9c2af2c commit 32fe3b4

File tree

6 files changed

+381
-68
lines changed

6 files changed

+381
-68
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
## [1.0.1] - 2025-10-18
10+
### Added:
11+
- Documentation: Benchmark timings against other libraries.
12+
### Changed:
13+
- Optimisation: Empty node name and duplicated path as optional checks if ASSERTIONS=false. Default does not change.
814
### Fixed:
915
- Tree Export: Print tree to display correctly for subtrees / non-root nodes.
1016

@@ -839,7 +845,8 @@ ignore null attribute columns.
839845
- Utility Iterator: Tree traversal methods.
840846
- Workflow To Do App: Tree use case with to-do list implementation.
841847

842-
[Unreleased]: https://github.com/kayjan/bigtree/compare/1.0.0...HEAD
848+
[Unreleased]: https://github.com/kayjan/bigtree/compare/1.0.1...HEAD
849+
[1.0.1]: https://github.com/kayjan/bigtree/compare/1.0.0...1.0.1
843850
[1.0.0]: https://github.com/kayjan/bigtree/compare/0.31.2...1.0.0
844851
[0.31.2]: https://github.com/kayjan/bigtree/compare/0.31.1...0.31.2
845852
[0.31.1]: https://github.com/kayjan/bigtree/compare/0.31.0...0.31.1

bigtree/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.0.0"
1+
__version__ = "1.0.1"
22

33
from bigtree.binarytree.binarytree import BinaryTree
44
from bigtree.binarytree.construct import list_to_binarytree

bigtree/node/node.py

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections import Counter
44
from typing import Any, TypeVar
55

6+
from bigtree.globals import ASSERTIONS
67
from bigtree.node import basenode
78
from bigtree.utils import exceptions
89

@@ -83,7 +84,7 @@ def __init__(self, name: str, sep: str = "/", **kwargs: Any):
8384
self.name = name
8485
self._sep = sep
8586
super().__init__(**kwargs)
86-
if not self.node_name:
87+
if ASSERTIONS and not self.node_name:
8788
raise exceptions.TreeError("Node must have a `name` attribute")
8889

8990
@property
@@ -126,47 +127,13 @@ def path_name(self) -> str:
126127
sep = ancestors[-1].sep
127128
return sep + sep.join([str(node.node_name) for node in reversed(ancestors)])
128129

129-
def __pre_assign_children(self: T, new_children: list[T]) -> None:
130-
"""Custom method to check before attaching children
131-
Can be overridden with `_Node__pre_assign_children()`
132-
133-
Args:
134-
new_children (list[Self]): new children to be added
135-
"""
136-
pass
137-
138-
def __post_assign_children(self: T, new_children: list[T]) -> None:
139-
"""Custom method to check after attaching children. Can be overridden with `_Node__post_assign_children()`.
140-
141-
Args:
142-
new_children: new children to be added
143-
"""
144-
pass
145-
146-
def __pre_assign_parent(self: T, new_parent: T) -> None:
147-
"""Custom method to check before attaching parent. Can be overridden with `_Node__pre_assign_parent()`.
148-
149-
Args:
150-
new_parent: new parent to be added
151-
"""
152-
pass
153-
154-
def __post_assign_parent(self: T, new_parent: T) -> None:
155-
"""Custom method to check after attaching parent. Can be overridden with `_Node__post_assign_parent()`.
156-
157-
Args:
158-
new_parent: new parent to be added
159-
"""
160-
pass
161-
162130
def _BaseNode__pre_assign_parent(self: T, new_parent: T) -> None:
163131
"""Do not allow duplicate nodes of same path.
164132
165133
Args:
166134
new_parent: new parent to be added
167135
"""
168-
self.__pre_assign_parent(new_parent)
169-
if new_parent is not None:
136+
if ASSERTIONS and new_parent is not None:
170137
if any(
171138
child.node_name == self.node_name and child is not self
172139
for child in new_parent.children
@@ -176,41 +143,25 @@ def _BaseNode__pre_assign_parent(self: T, new_parent: T) -> None:
176143
f"There exist a node with same path {new_parent.path_name}{new_parent.sep}{self.node_name}"
177144
)
178145

179-
def _BaseNode__post_assign_parent(self: T, new_parent: T) -> None:
180-
"""No rules.
181-
182-
Args:
183-
new_parent: new parent to be added
184-
"""
185-
self.__post_assign_parent(new_parent)
186-
187146
def _BaseNode__pre_assign_children(self: T, new_children: list[T]) -> None:
188147
"""Do not allow duplicate nodes of same path.
189148
190149
Args:
191150
new_children: new children to be added
192151
"""
193-
self.__pre_assign_children(new_children)
194-
children_names = [node.node_name for node in new_children]
195-
duplicate_names = [
196-
item[0] for item in Counter(children_names).items() if item[1] > 1
197-
]
198-
if len(duplicate_names):
199-
duplicate_names_str = " and ".join(
200-
[f"{self.path_name}{self.sep}{name}" for name in duplicate_names]
201-
)
202-
raise exceptions.TreeError(
203-
f"Duplicate node with same path\n"
204-
f"Attempting to add nodes with same path {duplicate_names_str}"
205-
)
206-
207-
def _BaseNode__post_assign_children(self: T, new_children: list[T]) -> None:
208-
"""No rules.
209-
210-
Args:
211-
new_children: new children to be added
212-
"""
213-
self.__post_assign_children(new_children)
152+
if ASSERTIONS:
153+
children_names = [node.node_name for node in new_children]
154+
duplicate_names = [
155+
item[0] for item in Counter(children_names).items() if item[1] > 1
156+
]
157+
if len(duplicate_names):
158+
duplicate_names_str = " and ".join(
159+
[f"{self.path_name}{self.sep}{name}" for name in duplicate_names]
160+
)
161+
raise exceptions.TreeError(
162+
f"Duplicate node with same path\n"
163+
f"Attempting to add nodes with same path {duplicate_names_str}"
164+
)
214165

215166
def show(self, **kwargs: Any) -> None:
216167
"""Print tree to console, takes in same keyword arguments as `print_tree` function."""

0 commit comments

Comments
 (0)