Module osbot_utils.graphs.mermaid.Mermaid__Graph

Expand source code
from typing import List

from osbot_utils.graphs.mgraph.MGraph__Edge import MGraph__Edge
from osbot_utils.graphs.mgraph.MGraph__Node import MGraph__Node
from osbot_utils.graphs.mermaid.Mermaid__Edge import Mermaid__Edge
from osbot_utils.graphs.mermaid.Mermaid__Node import Mermaid__Node
from osbot_utils.graphs.mgraph.MGraph import MGraph


class Mermaid__Graph(MGraph):
    edges        : List[Mermaid__Edge]
    mermaid_code : List
    nodes        : List[Mermaid__Node]

    # def __init__(self, mgraph=None):
    #     super().__init__()
    #     if mgraph is None:
    #         mgraph = MGraph()
    #     self.__dict__ = mgraph.__dict__
    #     self.convert_nodes().convert_edges()

    # def cast(self, source):
    #     self.__dict__ = source.__dict__
    #     return self

    # def add_edge(self, **kwargs):
    #     #new_edge = super().add_edge(*args, **kwargs)
    #     mermaid_edge = Mermaid__Edge(**kwargs)
    #     self.edges.append(mermaid_edge)
    #     return mermaid_edge
    #
    def add_node(self, **kwargs):
        new_node    = MGraph__Node(**kwargs)
        mermaid_node = Mermaid__Node().merge_with(new_node)
        self.nodes.append(mermaid_node)
        return mermaid_node

    #
    #
    # def add_line(self, line):
    #     self.mermaid_code.append(line)
    #     return line
    #
    # def code(self):
    #     self.code_create()
    #     return '\n'.join(self.mermaid_code)
    #
    # def code_create(self):
    #     with self as _:
    #         _.reset_code()
    #         _.add_line('graph LR')
    #         for node in _.nodes:
    #             _.add_line(node.code())
    #         _.add_line('')
    #         for edge in _.edges:
    #             _.add_line(edge.code())
    #     return self
    #
    # def code_markdown(self):
    #     self.code_create()
    #     markdown = ['# Mermaid Graph',
    #                 "```mermaid" ,
    #                 *self.mermaid_code,
    #                 "```"]
    #
    #     return '\n'.join(markdown)
    #
    # def convert_edges(self):
    #     new_edges = []
    #     for edge in self.edges:
    #         new_edges.append(Mermaid__Edge().cast(edge))
    #     self.edges = new_edges
    #     return self
    #
    # def convert_nodes(self):
    #     new_nodes = []
    #     for node in self.nodes:
    #         mermaid_node = Mermaid__Node().cast(node)
    #         new_nodes.append(mermaid_node)
    #     self.nodes = new_nodes
    #     return self
    #
    # def reset_code(self):
    #     self.mermaid_code = []
    #     return self
    #
    # def save(self, target_file=None):
    #     file_path = target_file or '/tmp/mermaid.md'
    #
    #     with open(file_path, 'w') as file:
    #         file.write(self.code_markdown())
    #     return file_path
    #
    # def print_code(self):
    #     print(self.code())

Classes

class Mermaid__Graph (**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__Graph(MGraph):
    edges        : List[Mermaid__Edge]
    mermaid_code : List
    nodes        : List[Mermaid__Node]

    # def __init__(self, mgraph=None):
    #     super().__init__()
    #     if mgraph is None:
    #         mgraph = MGraph()
    #     self.__dict__ = mgraph.__dict__
    #     self.convert_nodes().convert_edges()

    # def cast(self, source):
    #     self.__dict__ = source.__dict__
    #     return self

    # def add_edge(self, **kwargs):
    #     #new_edge = super().add_edge(*args, **kwargs)
    #     mermaid_edge = Mermaid__Edge(**kwargs)
    #     self.edges.append(mermaid_edge)
    #     return mermaid_edge
    #
    def add_node(self, **kwargs):
        new_node    = MGraph__Node(**kwargs)
        mermaid_node = Mermaid__Node().merge_with(new_node)
        self.nodes.append(mermaid_node)
        return mermaid_node

    #
    #
    # def add_line(self, line):
    #     self.mermaid_code.append(line)
    #     return line
    #
    # def code(self):
    #     self.code_create()
    #     return '\n'.join(self.mermaid_code)
    #
    # def code_create(self):
    #     with self as _:
    #         _.reset_code()
    #         _.add_line('graph LR')
    #         for node in _.nodes:
    #             _.add_line(node.code())
    #         _.add_line('')
    #         for edge in _.edges:
    #             _.add_line(edge.code())
    #     return self
    #
    # def code_markdown(self):
    #     self.code_create()
    #     markdown = ['# Mermaid Graph',
    #                 "```mermaid" ,
    #                 *self.mermaid_code,
    #                 "```"]
    #
    #     return '\n'.join(markdown)
    #
    # def convert_edges(self):
    #     new_edges = []
    #     for edge in self.edges:
    #         new_edges.append(Mermaid__Edge().cast(edge))
    #     self.edges = new_edges
    #     return self
    #
    # def convert_nodes(self):
    #     new_nodes = []
    #     for node in self.nodes:
    #         mermaid_node = Mermaid__Node().cast(node)
    #         new_nodes.append(mermaid_node)
    #     self.nodes = new_nodes
    #     return self
    #
    # def reset_code(self):
    #     self.mermaid_code = []
    #     return self
    #
    # def save(self, target_file=None):
    #     file_path = target_file or '/tmp/mermaid.md'
    #
    #     with open(file_path, 'w') as file:
    #         file.write(self.code_markdown())
    #     return file_path
    #
    # def print_code(self):
    #     print(self.code())

Ancestors

Class variables

var edges : List[Mermaid__Edge]
var mermaid_code : List
var nodes : List[Mermaid__Node]

Methods

def add_node(self, **kwargs)
Expand source code
def add_node(self, **kwargs):
    new_node    = MGraph__Node(**kwargs)
    mermaid_node = Mermaid__Node().merge_with(new_node)
    self.nodes.append(mermaid_node)
    return mermaid_node

Inherited members