Module osbot_utils.helpers.sqlite.tables.Sqlite__Table__Config

Expand source code
from osbot_utils.base_classes.Kwargs_To_Self    import Kwargs_To_Self
from osbot_utils.decorators.methods.cache_on_self import cache_on_self
from osbot_utils.helpers.sqlite.Sqlite__Table   import Sqlite__Table
from osbot_utils.utils.Objects                  import pickle_save_to_bytes, pickle_load_from_bytes

SQLITE__TABLE_NAME__CONFIG = 'config'

class Schema__Table__Config(Kwargs_To_Self):
    key  : str
    value: bytes

class Sqlite__Table__Config(Sqlite__Table):
    def __init__(self, **kwargs):
        self.table_name = SQLITE__TABLE_NAME__CONFIG
        self.row_schema  = Schema__Table__Config
        super().__init__(**kwargs)

    def config_data(self):
        config_data = {}
        for row in self.rows():
            key, value = self.deserialize_row(row)
            config_data[key] = value
        return config_data

    @cache_on_self
    def data(self):
        return self.config_data()

    def deserialize_row(self, row):
        if row:
            key           = row.get('key')
            pickled_value = row.get('value')
            value         = pickle_load_from_bytes(pickled_value)
            return key, value
        return None, None

    def set_config_data(self, config_data: dict):
        self.clear()
        for key,value in config_data.items():
            self.set_value(key=key, value=value)
        self.commit()

    def set_value(self, key, value):
        if self.not_contains(key=key):
            pickled_value = pickle_save_to_bytes(value)
            return self.add_row_and_commit(key=key, value=pickled_value)
        return self.update_value(key,value)

    def update_value(self, key, value):
        pickled_value = pickle_save_to_bytes(value)
        self.row_update(dict(key=key, value=pickled_value), dict(key=key))

    def setup(self):
        if self.exists() is False:
            self.create()
            self.index_create('key')
        return self

    def value(self, key):
        row = self.where_one(key=key)
        key, value = self.deserialize_row(row)
        return value

Classes

class Schema__Table__Config (**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 Schema__Table__Config(Kwargs_To_Self):
    key  : str
    value: bytes

Ancestors

Class variables

var key : str
var value : bytes

Inherited members

class Sqlite__Table__Config (**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 Sqlite__Table__Config(Sqlite__Table):
    def __init__(self, **kwargs):
        self.table_name = SQLITE__TABLE_NAME__CONFIG
        self.row_schema  = Schema__Table__Config
        super().__init__(**kwargs)

    def config_data(self):
        config_data = {}
        for row in self.rows():
            key, value = self.deserialize_row(row)
            config_data[key] = value
        return config_data

    @cache_on_self
    def data(self):
        return self.config_data()

    def deserialize_row(self, row):
        if row:
            key           = row.get('key')
            pickled_value = row.get('value')
            value         = pickle_load_from_bytes(pickled_value)
            return key, value
        return None, None

    def set_config_data(self, config_data: dict):
        self.clear()
        for key,value in config_data.items():
            self.set_value(key=key, value=value)
        self.commit()

    def set_value(self, key, value):
        if self.not_contains(key=key):
            pickled_value = pickle_save_to_bytes(value)
            return self.add_row_and_commit(key=key, value=pickled_value)
        return self.update_value(key,value)

    def update_value(self, key, value):
        pickled_value = pickle_save_to_bytes(value)
        self.row_update(dict(key=key, value=pickled_value), dict(key=key))

    def setup(self):
        if self.exists() is False:
            self.create()
            self.index_create('key')
        return self

    def value(self, key):
        row = self.where_one(key=key)
        key, value = self.deserialize_row(row)
        return value

Ancestors

Methods

def config_data(self)
Expand source code
def config_data(self):
    config_data = {}
    for row in self.rows():
        key, value = self.deserialize_row(row)
        config_data[key] = value
    return config_data
def data(self)
Expand source code
@cache_on_self
def data(self):
    return self.config_data()
def deserialize_row(self, row)
Expand source code
def deserialize_row(self, row):
    if row:
        key           = row.get('key')
        pickled_value = row.get('value')
        value         = pickle_load_from_bytes(pickled_value)
        return key, value
    return None, None
def set_config_data(self, config_data: dict)
Expand source code
def set_config_data(self, config_data: dict):
    self.clear()
    for key,value in config_data.items():
        self.set_value(key=key, value=value)
    self.commit()
def set_value(self, key, value)
Expand source code
def set_value(self, key, value):
    if self.not_contains(key=key):
        pickled_value = pickle_save_to_bytes(value)
        return self.add_row_and_commit(key=key, value=pickled_value)
    return self.update_value(key,value)
def setup(self)
Expand source code
def setup(self):
    if self.exists() is False:
        self.create()
        self.index_create('key')
    return self
def update_value(self, key, value)
Expand source code
def update_value(self, key, value):
    pickled_value = pickle_save_to_bytes(value)
    self.row_update(dict(key=key, value=pickled_value), dict(key=key))
def value(self, key)
Expand source code
def value(self, key):
    row = self.where_one(key=key)
    key, value = self.deserialize_row(row)
    return value

Inherited members