Skip to content

ragraph.node

Node class module

Node

Node(
    name: Optional[str] = None,
    parent: Optional[Node] = None,
    children: Optional[Iterable[Node]] = None,
    is_bus: bool = False,
    kind: str = "node",
    labels: Optional[List[str]] = None,
    weights: Optional[Dict[str, Union[int, float]]] = None,
    annotations: Union[
        Annotations, Dict[str, Any], None
    ] = None,
    uuid: Optional[Union[str, UUID]] = None,
)

Bases: Metadata

Generic node class.

Parameters:

Name Type Description Default
name Optional[str]

Instance name. Given a UUID if none provided.

None
parent Optional[Node]

Parent node.

None
children Optional[Iterable[Node]]

List of child nodes.

None
kind str

Kind or main category of this instance.

'node'
labels Optional[List[str]]

Labels categorizing this instance.

None
weights Optional[Dict[str, Union[int, float]]]

Dictionary of weights attached to this instance.

None
annotations Union[Annotations, Dict[str, Any], None]

Miscellaneous properties of this instance.

None
uuid Optional[Union[str, UUID]]

Fixed UUID if desired, generated when left set to None.

None
Source code in ragraph/node.py
def __init__(
    self,
    name: Optional[str] = None,
    parent: Optional["Node"] = None,
    children: Optional[Iterable["Node"]] = None,
    is_bus: bool = False,
    kind: str = "node",
    labels: Optional[List[str]] = None,
    weights: Optional[Dict[str, Union[int, float]]] = None,
    annotations: Union[Annotations, Dict[str, Any], None] = None,
    uuid: Optional[Union[str, UUID]] = None,
):
    Metadata.__init__(
        self,
        name=name,
        kind=kind,
        labels=labels,
        weights=weights,
        annotations=annotations,
        uuid=uuid,
    )
    self._parent: Optional[Node] = None
    self._children: List[Node] = []
    self._is_bus: bool = False
    setattr(self, "parent", parent)
    setattr(self, "children", children)
    setattr(self, "is_bus", is_bus)

ancestor_gen property

ancestor_gen: Generator[Node, None, None]

Yield all ancestors of this node.

ancestors property

ancestors: List[Node]

Get all ancestors of this node.

children property writable

children: List[Node]

Child nodes. Default is empty.

depth property

depth: int

Depth of this node, e.g. the number of levels from the node to the root.

descendant_gen property

descendant_gen: Generator[Node, None, None]

Yield all descendants of this node.

descendants property

descendants: List[Node]

Get all descendants of this node.

height property

height: int

Height of this node, e.g. the longest simple path to a leaf node.

is_bus property writable

is_bus

Whether this node is a bus node for its parent.

is_leaf property

is_leaf: bool

Whether this node is a leaf node (has no children).

is_root property

is_root: bool

Whether this node is a root node (has no parent).

json_dict property

json_dict: Dict[str, Any]

JSON dictionary representation.

Returns:

Name Type Description
name Dict[str, Any]

Name as str.

parent Dict[str, Any]

Parent name (not Node) as str.

children Dict[str, Any]

Children names (not Nodes) as str.

is_bus Dict[str, Any]

is_bus as bool.

kind Dict[str, Any]

Kind as str.

labels Dict[str, Any]

Labels as a list of strings.

weights Dict[str, Any]

Weights as dict.

annotations Dict[str, Any]

Annotations as per ragraph.generic.Mapping.as_dict.

parent property writable

parent: Optional[Node]

Parent node. Defaults to None.

width property

width: int

Width of this node, e.g. the number of leaf nodes in this node's branch.

as_dict

as_dict(use_uuid: bool = False) -> Dict[str, Any]

Return a copy as a (serializable) dictionary.

Returns:

Name Type Description
name Dict[str, Any]

Name as str.

parent Dict[str, Any]

Parent name or UUID (not Node) as str.

children Dict[str, Any]

Children names (not Nodes) as str.

is_bus Dict[str, Any]

is_bus as bool.

kind Dict[str, Any]

Kind as str.

labels Dict[str, Any]

Labels as a list of strings.

weights Dict[str, Any]

Weights as dict.

annotations Dict[str, Any]

Annotations as per ragraph.generic.Mapping.as_dict.

uuid Dict[str, Any]

UUID as str if toggled.

Source code in ragraph/node.py
def as_dict(self, use_uuid: bool = False) -> Dict[str, Any]:
    """Return a copy as a (serializable) dictionary.

    Returns:
        name: Name as str.
        parent: Parent name or UUID (not Node) as str.
        children: Children names (not Nodes) as str.
        is_bus: is_bus as bool.
        kind: Kind as str.
        labels: Labels as a list of strings.
        weights: Weights as dict.
        annotations: Annotations as per
            [`ragraph.generic.Mapping.as_dict`][ragraph.generic.Mapping.as_dict].
        uuid: UUID as str if toggled.
    """
    if use_uuid:
        return dict(
            name=self.name,
            parent=None if self.parent is None else str(self.parent.uuid),
            children=[str(n.uuid) for n in self.children],
            is_bus=self.is_bus,
            kind=self.kind,
            labels=self.labels,
            weights=self.weights,
            annotations=self.annotations.as_dict(),
            uuid=str(self.uuid),
        )
    else:
        return dict(
            name=self.name,
            parent=getattr(self.parent, "name", None),
            children=[n.name for n in self.children],
            is_bus=self.is_bus,
            kind=self.kind,
            labels=self.labels,
            weights=self.weights,
            annotations=self.annotations.as_dict(),
        )