Tree node
- Classes:
TNode - Single node approach that can have any number of children
Parent - Parent/Child nodes. Parent can have children of specific types.
Child - Parent/Child nodes. Child cannot have children, but can have a parent of specific types.
- Attributes
parent - parent object or None
title - String title for this node (Can be ‘’)
children - List of child objects
full_title - Parent titles and this title separeted by the set delimiter
- Methods
get_parents(require_title=False) - Iterate through the parent objects
add_child(child) - Add a child object
remove_child(child) - Remove a child object
clear() - Clear all direct children
find_parent(full_title) - Return the parent and title from the given full title.
find(full_title) - Return the child object with the given full title.
iter_children() - Iterate through the direct children.
__iter__() - Iterate through the direct children.
iter() - Iterate though all children and children’s children.
iter_nearest() - Iterate through direct children then their children’s children.
__getitem__(full_title) - Return the child object with the given full title.
__setitem__(full_title, child) - Add the child to the proper parent with the full title.
__len__() - Return the length of the direct children.
Example
Create the tree nodes.
from tnode import TNode
t = TNode()
parent1 = TNode('parent1', parent=t)
child1 = TNode('child1', parent=parent1)
child2 = TNode('child2', parent=parent1)
parent2 = TNode('parent2', parent=parent1)
child3 = TNode('child3', parent=parent2)
child4 = TNode('child4', parent=parent2)
assert list(t.iter()) == \
[t,
parent1,
child1,
child2,
parent2,
child3,
child4]
Parent Child Example
Create custom Parent and Child classes.
from tnode import ParentNode, ChildNode
class Parent(ParentNode):
pass
class Child(ChildNode):
def __init__(self, title='', data=None, parent=None, **kwargs):
self.data = data
super().__init__(title=title, parent=parent, **kwargs)
def has_data(self):
"""Helper to return if this function has data."""
return True
def get_data(self):
"""Return the data stored."""
return self.data
Parent.register_parent_type(Parent)
Parent.register_child_type(Parent)
Parent.register_child_type(Child)
Child.register_parent_type(Parent)
# Create tree
top = Parent('')
parent1 = top.add_parent('parent1')
parent2 = top.add_parent('parent2')
subparent1 = top.add_parent('parent1 > subparent1')
child1 = top.add('child1', data=1)
child2 = top.add('parent1 > child2', data=2)
child3 = top.add('parent1 > subparent1 > child3', data=3)
filename = 'test_json_parent_child.json'
try:
t2 = top.from_json(top.to_json(filename))
for v1, v2 in zip(top.iter(), t2.iter()):
assert v1.full_title == v2.full_title
assert v1.get_data() == v2.get_data()
finally:
try:
os.remove(filename)
except (OSError, Exception):
pass
Metadata
Release files for tnode 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tnode-1.1.1.tar.gz | 12.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tnode-1.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 55.8 kB
Release files / tnode-1.1.1.tar.gz
| Download URL | tnode-1.1.1.tar.gz |
|---|---|
| Size | 12.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b7ea99ae82dd2d9e6f61eca6b2765dfad3250d6c40ade54f9d2c9f0abff093c4
|
|
BLAKE2b-256 checksum How to use checksums |
760ecf023cdada9030309ed556b91445679a5d3ba6df6dfc22b79ba95b9c13b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.1
|
Release files / tnode-1.1.1-py3-none-any.whl
| Download URL | tnode-1.1.1-py3-none-any.whl |
|---|---|
| Size | 43.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
074f78499223614ca6e0d91bca7c1b51438a3f04b0d19b7243546865a372549a
|
|
BLAKE2b-256 checksum How to use checksums |
c257f292d45cb26f960a1a4fe35bc1c133a6a9dc908563912e343e3b205d4a69
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.1
|