Tips and tricks
Accessing child nodes by index
To make child nodes accessible by index, install indexed:
pip install indexed
Then create a subclass of Node that uses indexed.Dict for its internal mapping:
import indexed
class IndexedNode(Node):
__slots__ = ()
dict_class = indexed.Dict
tree = IndexedNode()
tree["Asia"] = IndexedNode()
assert tree.children[0] == tree["Asia"]
This works because dict_class can be set to any mapping type that behaves like dict.
The children property uses the value-view of the mapping.
In the case of indexed.Dict, this view additionally supports indexing.
As a result, tree.children[0] works as expected.
Always-sorted child nodes
To ensure children are always sorted by their identifier, install sortedcontainers:
pip install sortedcontainers
Then subclass Node to use SortedDict:
from sortedcontainers import SortedDict
class SortedNode(Node):
__slots__ = ()
dict_class = SortedDict
def sort_children(self, key=None):
if key is not None:
raise ValueError("Argument key is not supported. "
"Nodes are always sorted by identifier.")
# No-op, since children are already sorted.
tree = SortedNode()
tree["b"] = SortedNode()
tree["c"] = SortedNode()
tree["a"] = SortedNode()
for child in tree.children:
print(child.identifier) # Prints "a", then "b", then "c"
# With a regular Node, children would appear in insertion order (b, c, a).
This works because SortedDict always keeps its items sorted by key.
Since Node uses the identifier as the key, the children are automatically sorted.
By contrast, iteration over a regular dict (and therefore a regular Node) follows insertion order.