🌼 DAGNode
bigtree.node.dagnode
DAGNode
Base DAGNode extends any Python class to a DAG node, for DAG implementation. In DAG implementation, a node can have multiple parents.
Parents and children cannot be reassigned once assigned, as Nodes are allowed to have multiple parents and children.
If each node only has one parent, use Node
class.
DAGNodes can have attributes if they are initialized from DAGNode
or dictionary.
DAGNode can be linked to each other with parents
and children
setter methods,
or using bitshift operator with the convention parent_node >> child_node
or child_node << parent_node
.
Examples:
>>> from bigtree import DAGNode
>>> a = DAGNode("a")
>>> b = DAGNode("b")
>>> c = DAGNode("c")
>>> d = DAGNode("d")
>>> c.parents = [a, b]
>>> c.children = [d]
>>> from bigtree import DAGNode
>>> a = DAGNode("a")
>>> b = DAGNode("b")
>>> c = DAGNode("c")
>>> d = DAGNode("d")
>>> a >> c
>>> b >> c
>>> d << c
Directly passing parents
argument.
>>> from bigtree import DAGNode
>>> a = DAGNode("a")
>>> b = DAGNode("b")
>>> c = DAGNode("c", parents=[a, b])
>>> d = DAGNode("d", parents=[c])
Directly passing children
argument.
>>> from bigtree import DAGNode
>>> d = DAGNode("d")
>>> c = DAGNode("c", children=[d])
>>> b = DAGNode("b", children=[c])
>>> a = DAGNode("a", children=[c])
DAGNode Creation
Node can be created by instantiating a DAGNode
class or by using a dictionary.
If node is created with dictionary, all keys of dictionary will be stored as class attributes.
DAGNode Attributes
These are node attributes that have getter and/or setter methods.
Get and set other DAGNode
parents
: Get/set parent nodeschildren
: Get/set child nodes
Get other DAGNode
ancestors
: Get ancestors of node excluding self, iteratordescendants
: Get descendants of node excluding self, iteratorsiblings
: Get siblings of self
Get DAGNode
configuration
node_name
: Get node name, without accessingname
directlyis_root
: Get indicator if self is root nodeis_leaf
: Get indicator if self is leaf node
DAGNode Methods
These are methods available to be performed on DAGNode
.
Constructor methods
from_dict()
: Create DAGNode from dictionary
DAGNode
methods
describe()
: Get node information sorted by attributes, return list of tuplesget_attr(attr_name: str)
: Get value of node attributeset_attrs(attrs: dict)
: Set node attribute name(s) and value(s)go_to(node: Self)
: Get a path from own node to another node from same DAGcopy()
: Deep copy self
parent
property
writable
Do not allow parent
attribute to be accessed
Raises:
Type | Description |
---|---|
AttributeError
|
No such attribute |
parents
property
writable
Get parent nodes
Returns:
Type | Description |
---|---|
Iterable[T]
|
(Iterable[Self]) |
children
deletable
property
writable
Get child nodes
Returns:
Type | Description |
---|---|
Iterable[T]
|
(Iterable[Self]) |
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]) |
siblings
property
Get siblings of self
Returns:
Type | Description |
---|---|
Iterable[T]
|
(Iterable[Self]) |
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 |
---|---|
DAGNode
|
(DAGNode) |
describe
Get node information sorted by attribute name, returns list of tuples
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
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 list of possible paths from current node to specified node from same tree
Examples:
>>> from bigtree import DAGNode
>>> a = DAGNode("a")
>>> b = DAGNode("b")
>>> c = DAGNode("c")
>>> d = DAGNode("d")
>>> a >> c
>>> b >> c
>>> c >> d
>>> a >> d
>>> a.go_to(c)
[[DAGNode(a, ), DAGNode(c, )]]
>>> a.go_to(d)
[[DAGNode(a, ), DAGNode(c, ), DAGNode(d, )], [DAGNode(a, ), DAGNode(d, )]]
>>> a.go_to(b)
Traceback (most recent call last):
...
bigtree.utils.exceptions.TreeError: It is not possible to go to DAGNode(b, )
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node |
Self
|
node to travel to from current node, inclusive of start and end node |
required |
Returns:
Type | Description |
---|---|
List[List[T]]
|
(List[List[Self]]) |