Tree node list utilities
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tnode-1.1.1.tar.gz
.
File metadata
- Download URL: tnode-1.1.1.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7ea99ae82dd2d9e6f61eca6b2765dfad3250d6c40ade54f9d2c9f0abff093c4 |
|
MD5 | 605be2037f1411483c3acd31c8648168 |
|
BLAKE2b-256 | 760ecf023cdada9030309ed556b91445679a5d3ba6df6dfc22b79ba95b9c13b3 |
File details
Details for the file tnode-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: tnode-1.1.1-py3-none-any.whl
- Upload date:
- Size: 43.0 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 074f78499223614ca6e0d91bca7c1b51438a3f04b0d19b7243546865a372549a |
|
MD5 | 315efef6681d1f6af53eb49ec48bd8ec |
|
BLAKE2b-256 | c257f292d45cb26f960a1a4fe35bc1c133a6a9dc908563912e343e3b205d4a69 |