Module osbot_utils.graphs.mgraph.MGraph__Serializer

Expand source code
from enum import Enum, auto
from osbot_utils.utils.Str import safe_str
from osbot_utils.helpers.Local_Cache import Local_Cache

from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self
from osbot_utils.graphs.mgraph.MGraph import MGraph


class Serialization_Mode(Enum):
    JSON    = auto()
    PICKLE  = auto()

class MGraph__Serializer(Kwargs_To_Self):

    caches_name : str                = 'mgraph_tests'
    mode        : Serialization_Mode = Serialization_Mode.PICKLE

    local_cache : Local_Cache                                           # todo, refactor this into an MGraph__Storage__Disk class
    key         : str
    mgraph      : MGraph

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.key         = safe_str(f'serialiser_for__{self.mgraph.key}')

        self.local_cache = Local_Cache(cache_name=self.key, caches_name=self.caches_name)


    def save(self):
        if self.mode == Serialization_Mode.JSON:
            return self.save_to_json()
        if self.mode == Serialization_Mode.PICKLE:
            return self.save_to_pickle()

    def save_to_json(self):
        graph_data = self.mgraph.data().graph_data()
        #pprint(graph_data)
        self.local_cache.set('graph_data', graph_data)
        return True

    def save_to_pickle(self):
        #obj_info(self.local_cache)
        return '...pickle save - to be implemented...'

Classes

class MGraph__Serializer (**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 MGraph__Serializer(Kwargs_To_Self):

    caches_name : str                = 'mgraph_tests'
    mode        : Serialization_Mode = Serialization_Mode.PICKLE

    local_cache : Local_Cache                                           # todo, refactor this into an MGraph__Storage__Disk class
    key         : str
    mgraph      : MGraph

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.key         = safe_str(f'serialiser_for__{self.mgraph.key}')

        self.local_cache = Local_Cache(cache_name=self.key, caches_name=self.caches_name)


    def save(self):
        if self.mode == Serialization_Mode.JSON:
            return self.save_to_json()
        if self.mode == Serialization_Mode.PICKLE:
            return self.save_to_pickle()

    def save_to_json(self):
        graph_data = self.mgraph.data().graph_data()
        #pprint(graph_data)
        self.local_cache.set('graph_data', graph_data)
        return True

    def save_to_pickle(self):
        #obj_info(self.local_cache)
        return '...pickle save - to be implemented...'

Ancestors

Class variables

var caches_name : str
var key : str
var local_cacheLocal_Cache
var mgraphMGraph
var modeSerialization_Mode

Methods

def save(self)
Expand source code
def save(self):
    if self.mode == Serialization_Mode.JSON:
        return self.save_to_json()
    if self.mode == Serialization_Mode.PICKLE:
        return self.save_to_pickle()
def save_to_json(self)
Expand source code
def save_to_json(self):
    graph_data = self.mgraph.data().graph_data()
    #pprint(graph_data)
    self.local_cache.set('graph_data', graph_data)
    return True
def save_to_pickle(self)
Expand source code
def save_to_pickle(self):
    #obj_info(self.local_cache)
    return '...pickle save - to be implemented...'

Inherited members

class Serialization_Mode (*args, **kwds)

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access:

Color.RED

  • value lookup:

Color(1)

  • name lookup:

Color['RED']

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.

Expand source code
class Serialization_Mode(Enum):
    JSON    = auto()
    PICKLE  = auto()

Ancestors

  • enum.Enum

Class variables

var JSON
var PICKLE