bigtree

🌼 DAGNode

Classes:

DAGNode([name, parents, children])

Base DAGNode extends any Python class to a DAG node, for DAG implementation.

class bigtree.node.dagnode.DAGNode(name: str = '', parents: List[T] | None = None, children: List[T] | None = None, **kwargs: Any)

Bases: object

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.

>>> 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.

>>> from bigtree import DAGNode
>>> a = DAGNode.from_dict({"name": "a", "age": 90})

DAGNode Attributes

These are node attributes that have getter and/or setter methods.

Get and set other DAGNode

  1. parents: Get/set parent nodes

  2. children: Get/set child nodes

Get other DAGNode

  1. ancestors: Get ancestors of node excluding self, iterator

  2. descendants: Get descendants of node excluding self, iterator

  3. siblings: Get siblings of self

Get DAGNode configuration

  1. node_name: Get node name, without accessing name directly

  2. is_root: Get indicator if self is root node

  3. is_leaf: Get indicator if self is leaf node

DAGNode Methods

These are methods available to be performed on DAGNode.

Constructor methods

  1. from_dict(): Create DAGNode from dictionary

DAGNode methods

  1. describe(): Get node information sorted by attributes, return list of tuples

  2. get_attr(attr_name: str): Get value of node attribute

  3. set_attrs(attrs: dict): Set node attribute name(s) and value(s)

  4. go_to(node: Self): Get a path from own node to another node from same DAG

  5. copy(): Deep copy self


Attributes:

ancestors

Get iterator to yield all ancestors of self, does not include self

children

Get child nodes

descendants

Get iterator to yield all descendants of self, does not include self

is_leaf

Get indicator if self is leaf node

is_root

Get indicator if self is root node

node_name

Get node name

parent

Do not allow parent attribute to be accessed

parents

Get parent nodes

siblings

Get siblings of self

Methods:

copy()

Deep copy self; clone DAGNode

describe([exclude_attributes, exclude_prefix])

Get node information sorted by attribute name, returns list of tuples

from_dict(input_dict)

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

get_attr(attr_name[, default_value])

Get value of node attribute Returns default value if attribute name does not exist

go_to(node)

Get list of possible paths from current node to specified node from same tree

set_attrs(attrs)

Set node attributes

property ancestors: Iterable[T]

Get iterator to yield all ancestors of self, does not include self

Returns:

(Iterable[Self])

property children: Iterable[T]

Get child nodes

Returns:

(Iterable[Self])

copy() T

Deep copy self; clone DAGNode

>>> from bigtree.node.dagnode import DAGNode
>>> a = DAGNode('a')
>>> a_copy = a.copy()
Returns:

(Self)

property descendants: Iterable[T]

Get iterator to yield all descendants of self, does not include self

Returns:

(Iterable[Self])

describe(exclude_attributes: List[str] = [], exclude_prefix: str = '') List[Tuple[str, Any]]

Get node information sorted by attribute name, returns list of tuples

Parameters:
  • exclude_attributes (List[str]) – list of attributes to exclude

  • exclude_prefix (str) – prefix of attributes to exclude

Returns:

(List[Tuple[str, Any]])

classmethod from_dict(input_dict: Dict[str, Any]) DAGNode

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

>>> from bigtree import DAGNode
>>> a = DAGNode.from_dict({"name": "a", "age": 90})
Parameters:

input_dict (Dict[str, Any]) – dictionary with node information, key: attribute name, value: attribute value

Returns:

(DAGNode)

get_attr(attr_name: str, default_value: Any | None = None) Any

Get value of node attribute Returns default value if attribute name does not exist

Parameters:
  • attr_name (str) – attribute name

  • default_value (Any) – default value if attribute does not exist, defaults to None

Returns:

(Any)

go_to(node: T) List[List[T]]

Get list of possible paths from current node to specified node from same tree

>>> 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:

node (Self) – node to travel to from current node, inclusive of start and end node

Returns:

(List[List[Self]])

property is_leaf: bool

Get indicator if self is leaf node

Returns:

(bool)

property is_root: bool

Get indicator if self is root node

Returns:

(bool)

property node_name: str

Get node name

Returns:

(str)

property parent: None

Do not allow parent attribute to be accessed

property parents: Iterable[T]

Get parent nodes

Returns:

(Iterable[Self])

set_attrs(attrs: Dict[str, Any]) None

Set node attributes

>>> from bigtree.node.dagnode import DAGNode
>>> a = DAGNode('a')
>>> a.set_attrs({"age": 90})
>>> a
DAGNode(a, age=90)
Parameters:

attrs (Dict[str, Any]) – attribute dictionary, key: attribute name, value: attribute value

property siblings: Iterable[T]

Get siblings of self

Returns:

(Iterable[Self])