Skip to content

🌼 DAGNode

bigtree.node.dagnode

DAGNode

DAGNode(name='', parents=None, children=None, **kwargs)

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.

>>> 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. append(node: Self): Add child to node
  6. extend(nodes: List[Self]): Add multiple children to node
  7. copy(): Deep copy self

parent property writable

parent

Do not allow parent attribute to be accessed.

Raises:

Type Description
AttributeError

No such attribute

parents property writable

parents

Get parent nodes.

Returns:

Type Description
Iterable[T]

Parent node(s)

children deletable property writable

children

Get child nodes.

Returns:

Type Description
Iterable[T]

Child node(s)

ancestors property

ancestors

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

Returns:

Type Description
Iterable[T]

Ancestor(s) of node excluding itself

descendants property

descendants

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

Returns:

Type Description
Iterable[T]

Descendant(s) of node excluding itself

siblings property

siblings

Get siblings of self.

Returns:

Type Description
Iterable[T]

Sibling(s) of node

node_name property

node_name

Get node name.

Returns:

Type Description
str

Node name

is_root property

is_root

Get indicator if self is root node.

Returns:

Type Description
bool

Indicator if node is root node

is_leaf property

is_leaf

Get indicator if self is leaf node.

Returns:

Type Description
bool

Indicator if node is leaf node

from_dict classmethod

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.

Examples:

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

Parameters:

Name Type Description Default
input_dict Mapping[str, Any]

node information, key: attribute name, value: attribute value

required

Returns:

Type Description
DAGNode

DAG node

describe

describe(exclude_attributes=(), exclude_prefix='')

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

Parameters:

Name Type Description Default
exclude_attributes Iterable[str]

attributes to exclude

()
exclude_prefix str

prefix of attributes to exclude

''

Returns:

Type Description
List[Tuple[str, Any]]

List of attribute name and attribute value pairs

get_attr

get_attr(attr_name, default_value=None)

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

None

Returns:

Type Description
Any

Attribute value of node

set_attrs

set_attrs(attrs)

Set node attributes.

Examples:

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

Parameters:

Name Type Description Default
attrs Mapping[str, Any]

attribute information, key: attribute name, value: attribute value

required

go_to

go_to(node)

Get list of possible paths from current node to specified node from same tree, uses get_path_dag function.

Parameters:

Name Type Description Default
node T

node to travel to from current node, inclusive of start and end node

required

Returns:

Type Description
List[List[T]]

Possible paths from current node to destination node

append

append(other)

Add other as child of self. Can be chained.

Parameters:

Name Type Description Default
other T

other node, child to be added

required

extend

extend(others)

Add others as children of self. Can be chained.

Parameters:

Name Type Description Default
others List[T]

other nodes, children to be added

required

copy

copy()

Deep copy self; clone DAGNode.

Examples:

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

Returns:

Type Description
T

Cloned copy of node