Module osbot_utils.graphs.mermaid.Mermaid__Edge

Expand source code
from osbot_utils.graphs.mermaid.Mermaid__Node import LINE_PADDING, Mermaid__Node
from osbot_utils.graphs.mermaid.configs.Mermaid__Edge__Config import Mermaid__Edge__Config
from osbot_utils.graphs.mgraph.MGraph__Edge import MGraph__Edge
#from osbot_utils.graphs.mgraph.views.mermaid.Mermaid__Node import Mermaid__Node
from osbot_utils.utils.Str import safe_str


class Mermaid__Edge(MGraph__Edge):
    config    : Mermaid__Edge__Config
    from_node : Mermaid__Node
    to_node   : Mermaid__Node

    # def __init__(self, **kwargs):
    #     super().__init__(**kwargs)

    # def __init__(self, **kwargs):
    #     super().__init__(**kwargs)
    #     self.convert_nodes()
    #
    def edge_mode(self, edge_mode):
        self.config.edge_mode = edge_mode
        return self

    def edge_mode__lr_using_pipe(self):
        return self.edge_mode('lr_using_pipe')

    def output_node_from(self, value=True):
        self.config.output_node_from = value
        return self

    def output_node_to(self, value=True):
        self.config.output_node_to = value
        return self

    def render_edge(self):
        from_node_key = safe_str(self.from_node.key)
        to_node_key   = safe_str(self.to_node  .key)
        if self.config.output_node_from:
            from_node_key =  self.from_node.render_node(include_padding=False) #f'{edge.from_node.key}["{edge.from_node.label}"]'
        if self.config.output_node_to:
            to_node_key   = self.to_node.render_node(include_padding=False   ) #f'{edge.to_node  .key}["{edge.to_node  .label}"]'
        if self.config.edge_mode == 'lr_using_pipe':
            link_code      = f'-->|{self.label}|'
        elif self.label:
            link_code      = f'--"{self.label}"-->'
        else:
            link_code      = '-->'
        edge_code      = f'{LINE_PADDING}{from_node_key} {link_code} {to_node_key}'
        return edge_code

Classes

class Mermaid__Edge (**kwargs)

A mixin class to strictly assign keyword arguments to pre-defined instance attributes during initialization.

This base class provides an init method that assigns values from keyword arguments to instance attributes. If an attribute with the same name as a key from the kwargs is defined in the class, it will be set to the value from kwargs. If the key does not match any predefined attribute names, an exception is raised.

This behavior enforces strict control over the attributes of instances, ensuring that only predefined attributes can be set at the time of instantiation and avoids silent attribute creation which can lead to bugs in the code.

Usage

class MyConfigurableClass(Kwargs_To_Self): attribute1 = 'default_value' attribute2 = True attribute3 : str attribute4 : list attribute4 : int = 42

# Other methods can be added here

Correctly override default values by passing keyword arguments

instance = MyConfigurableClass(attribute1='new_value', attribute2=False)

This will raise an exception as 'attribute3' is not predefined

instance = MyConfigurableClass(attribute3='invalid_attribute')

this will also assign the default value to any variable that has a type defined. In the example above the default values (mapped by default__kwargs and locals) will be: attribute1 = 'default_value' attribute2 = True attribute3 = '' # default value of str attribute4 = [] # default value of list attribute4 = 42 # defined value in the class

Note

It is important that all attributes which may be set at instantiation are predefined in the class. Failure to do so will result in an exception being raised.

Methods

init(**kwargs): The initializer that handles the assignment of keyword arguments to instance attributes. It enforces strict attribute assignment rules, only allowing attributes that are already defined in the class to be set.

Initialize an instance of the derived class, strictly assigning provided keyword arguments to corresponding instance attributes.

Parameters

**kwargs: Variable length keyword arguments.

Raises

Exception
If a key from kwargs does not correspond to any attribute pre-defined in the class, an exception is raised to prevent setting an undefined attribute.
Expand source code
class Mermaid__Edge(MGraph__Edge):
    config    : Mermaid__Edge__Config
    from_node : Mermaid__Node
    to_node   : Mermaid__Node

    # def __init__(self, **kwargs):
    #     super().__init__(**kwargs)

    # def __init__(self, **kwargs):
    #     super().__init__(**kwargs)
    #     self.convert_nodes()
    #
    def edge_mode(self, edge_mode):
        self.config.edge_mode = edge_mode
        return self

    def edge_mode__lr_using_pipe(self):
        return self.edge_mode('lr_using_pipe')

    def output_node_from(self, value=True):
        self.config.output_node_from = value
        return self

    def output_node_to(self, value=True):
        self.config.output_node_to = value
        return self

    def render_edge(self):
        from_node_key = safe_str(self.from_node.key)
        to_node_key   = safe_str(self.to_node  .key)
        if self.config.output_node_from:
            from_node_key =  self.from_node.render_node(include_padding=False) #f'{edge.from_node.key}["{edge.from_node.label}"]'
        if self.config.output_node_to:
            to_node_key   = self.to_node.render_node(include_padding=False   ) #f'{edge.to_node  .key}["{edge.to_node  .label}"]'
        if self.config.edge_mode == 'lr_using_pipe':
            link_code      = f'-->|{self.label}|'
        elif self.label:
            link_code      = f'--"{self.label}"-->'
        else:
            link_code      = '-->'
        edge_code      = f'{LINE_PADDING}{from_node_key} {link_code} {to_node_key}'
        return edge_code

Ancestors

Class variables

var configMermaid__Edge__Config
var from_nodeMermaid__Node
var to_nodeMermaid__Node

Methods

def edge_mode(self, edge_mode)
Expand source code
def edge_mode(self, edge_mode):
    self.config.edge_mode = edge_mode
    return self
def edge_mode__lr_using_pipe(self)
Expand source code
def edge_mode__lr_using_pipe(self):
    return self.edge_mode('lr_using_pipe')
def output_node_from(self, value=True)
Expand source code
def output_node_from(self, value=True):
    self.config.output_node_from = value
    return self
def output_node_to(self, value=True)
Expand source code
def output_node_to(self, value=True):
    self.config.output_node_to = value
    return self
def render_edge(self)
Expand source code
def render_edge(self):
    from_node_key = safe_str(self.from_node.key)
    to_node_key   = safe_str(self.to_node  .key)
    if self.config.output_node_from:
        from_node_key =  self.from_node.render_node(include_padding=False) #f'{edge.from_node.key}["{edge.from_node.label}"]'
    if self.config.output_node_to:
        to_node_key   = self.to_node.render_node(include_padding=False   ) #f'{edge.to_node  .key}["{edge.to_node  .label}"]'
    if self.config.edge_mode == 'lr_using_pipe':
        link_code      = f'-->|{self.label}|'
    elif self.label:
        link_code      = f'--"{self.label}"-->'
    else:
        link_code      = '-->'
    edge_code      = f'{LINE_PADDING}{from_node_key} {link_code} {to_node_key}'
    return edge_code

Inherited members