πΊ Node
bigtree.node.node
Node
Bases: BaseNode
Node is an extension of BaseNode, and is able to extend to any Python class.
Nodes can have attributes if they are initialized from Node
, dictionary, or pandas DataFrame.
Nodes can be linked to each other with parent
and children
setter methods.
Examples:
>>> from bigtree import Node
>>> a = Node("a")
>>> b = Node("b")
>>> c = Node("c")
>>> d = Node("d")
>>> b.parent = a
>>> b.children = [c, d]
Directly passing parent
argument.
>>> from bigtree import Node
>>> a = Node("a")
>>> b = Node("b", parent=a)
>>> c = Node("c", parent=b)
>>> d = Node("d", parent=b)
Directly passing children
argument.
>>> from bigtree import Node
>>> d = Node("d")
>>> c = Node("c")
>>> b = Node("b", children=[c, d])
>>> a = Node("a", children=[b])
Node Creation
Node can be created by instantiating a Node
class or by using a dictionary.
If node is created with dictionary, all keys of dictionary will be stored as class attributes.
Node Attributes
These are node attributes that have getter and/or setter methods.
Get and set Node
configuration
sep
: Get/set separator for path name
Get Node
configuration
node_name
: Get node name, without accessingname
directlypath_name
: Get path name from root, separated bysep
Node Methods
These are methods available to be performed on Node
.
Node
methods
show()
: Print tree to consolehshow()
: Print tree in horizontal orientation to console
parent
property
writable
Get parent node
Returns:
Type | Description |
---|---|
Optional[T]
|
(Optional[Self]) |
children
deletable
property
writable
Get child nodes
Returns:
Type | Description |
---|---|
Tuple[T, ...]
|
(Tuple[Self, ...]) |
parents
property
writable
Do not allow parents
attribute to be accessed
Raises:
Type | Description |
---|---|
AttributeError
|
No such attribute |
ancestors
property
Get iterator to yield all ancestors of self, does not include self
Returns:
Type | Description |
---|---|
Iterable[T]
|
(Iterable[Self]) |
descendants
property
Get iterator to yield all descendants of self, does not include self
Returns:
Type | Description |
---|---|
Iterable[T]
|
(Iterable[Self]) |
leaves
property
Get iterator to yield all leaf nodes from self
Returns:
Type | Description |
---|---|
Iterable[T]
|
(Iterable[Self]) |
siblings
property
Get siblings of self
Returns:
Type | Description |
---|---|
Iterable[T]
|
(Iterable[Self]) |
node_path
property
Get tuple of nodes starting from root
Returns:
Type | Description |
---|---|
Iterable[T]
|
(Iterable[Self]) |
diameter
property
Get diameter of tree or subtree, the length of longest path between any two nodes
Returns:
Type | Description |
---|---|
int
|
(int) |
max_depth
property
Get maximum depth from root to leaf node
Returns:
Type | Description |
---|---|
int
|
(int) |
path_name
property
Get path name, separated by self.sep
Returns:
Type | Description |
---|---|
str
|
(str) |
from_dict
classmethod
Construct node from dictionary, all keys of dictionary will be stored as class attributes
Input dictionary must have key name
if not Node
will not have any name
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dict |
Dict[str, Any]
|
dictionary with node information, key: attribute name, value: attribute value |
required |
Returns:
Type | Description |
---|---|
BaseNode
|
(BaseNode) |
describe
Get node information sorted by attribute name, returns list of tuples
Examples:
>>> from bigtree.node.node import Node
>>> a = Node('a', age=90)
>>> a.describe()
[('_BaseNode__children', []), ('_BaseNode__parent', None), ('_sep', '/'), ('age', 90), ('name', 'a')]
>>> a.describe(exclude_prefix="_")
[('age', 90), ('name', 'a')]
>>> a.describe(exclude_prefix="_", exclude_attributes=["name"])
[('age', 90)]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exclude_attributes |
List[str]
|
list of attributes to exclude |
[]
|
exclude_prefix |
str
|
prefix of attributes to exclude |
''
|
Returns:
Type | Description |
---|---|
List[Tuple[str, Any]]
|
(List[Tuple[str, Any]]) |
get_attr
Get value of node attribute Returns default value if attribute name does not exist
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attr_name |
str
|
attribute name |
required |
default_value |
Any
|
default value if attribute does not exist, defaults to None |
None
|
Returns:
Type | Description |
---|---|
Any
|
(Any) |
set_attrs
go_to
Get path from current node to specified node from same tree
Examples:
>>> from bigtree import Node, print_tree
>>> a = Node(name="a")
>>> b = Node(name="b", parent=a)
>>> c = Node(name="c", parent=a)
>>> d = Node(name="d", parent=b)
>>> e = Node(name="e", parent=b)
>>> f = Node(name="f", parent=c)
>>> g = Node(name="g", parent=e)
>>> h = Node(name="h", parent=e)
>>> print_tree(a)
a
βββ b
β βββ d
β βββ e
β βββ g
β βββ h
βββ c
βββ f
>>> d.go_to(d)
[Node(/a/b/d, )]
>>> d.go_to(g)
[Node(/a/b/d, ), Node(/a/b, ), Node(/a/b/e, ), Node(/a/b/e/g, )]
>>> d.go_to(f)
[Node(/a/b/d, ), Node(/a/b, ), Node(/a, ), Node(/a/c, ), Node(/a/c/f, )]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
Self
|
node to travel to from current node, inclusive of start and end node |
required |
Returns:
Type | Description |
---|---|
Iterable[T]
|
(Iterable[Self]) |
append
Add other as child of self
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Self
|
other node, child to be added |
required |
extend
Add others as children of self
Parameters:
Name | Type | Description | Default |
---|---|---|---|
others |
Self
|
other nodes, children to be added |
required |
copy
sort
Sort children, possible keyword arguments include key=lambda node: node.name
, reverse=True
Examples: