Module osbot_utils.helpers.sqlite.tables.Sqlite__Table__Nodes

Expand source code
from osbot_utils.decorators.lists.index_by import index_by
from osbot_utils.helpers.sqlite.Sqlite__Table import Sqlite__Table

from osbot_utils.base_classes.Kwargs_To_Self import Kwargs_To_Self
from osbot_utils.utils.Lists import unique
from osbot_utils.utils.Misc import timestamp_utc_now

SQLITE__TABLE_NAME__NODES = 'nodes'

class Schema__Table__Nodes(Kwargs_To_Self):
    key       : str
    value     : bytes
    properties: bytes
    timestamp : int

class Sqlite__Table__Nodes(Sqlite__Table):
    allow_duplicate_keys: bool = False
    auto_pickle_blob    : bool = True
    set_timestamp       : bool = True

    def __init__(self, **kwargs):
        self.table_name = SQLITE__TABLE_NAME__NODES
        self.row_schema  = Schema__Table__Nodes
        super().__init__(**kwargs)

    def add_node(self, key, value=None, properties=None):
        if self.allow_duplicate_keys is False:
            if self.contains(key=key):
                return None
        row_data = self.create_node_data(key,value, properties)
        return self.add_row_and_commit(**row_data)

    def create_node_data(self, key, value=None, properties=None):
        node_data =  {'key'        : key                     ,
                      'value'      : value                   ,
                      'properties' : properties              }
        if self.set_timestamp:
            node_data['timestamp'] = timestamp_utc_now()
        return node_data

    @index_by
    def nodes(self):
        return self.rows()

    def keys(self):
        return unique(self.select_field_values('key'))

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

Classes

class Schema__Table__Nodes (**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__Nodes(Kwargs_To_Self):
    key       : str
    value     : bytes
    properties: bytes
    timestamp : int

Ancestors

Class variables

var key : str
var properties : bytes
var timestamp : int
var value : bytes

Inherited members

class Sqlite__Table__Nodes (**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__Nodes(Sqlite__Table):
    allow_duplicate_keys: bool = False
    auto_pickle_blob    : bool = True
    set_timestamp       : bool = True

    def __init__(self, **kwargs):
        self.table_name = SQLITE__TABLE_NAME__NODES
        self.row_schema  = Schema__Table__Nodes
        super().__init__(**kwargs)

    def add_node(self, key, value=None, properties=None):
        if self.allow_duplicate_keys is False:
            if self.contains(key=key):
                return None
        row_data = self.create_node_data(key,value, properties)
        return self.add_row_and_commit(**row_data)

    def create_node_data(self, key, value=None, properties=None):
        node_data =  {'key'        : key                     ,
                      'value'      : value                   ,
                      'properties' : properties              }
        if self.set_timestamp:
            node_data['timestamp'] = timestamp_utc_now()
        return node_data

    @index_by
    def nodes(self):
        return self.rows()

    def keys(self):
        return unique(self.select_field_values('key'))

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

Ancestors

Class variables

var allow_duplicate_keys : bool
var auto_pickle_blob : bool
var set_timestamp : bool

Methods

def add_node(self, key, value=None, properties=None)
Expand source code
def add_node(self, key, value=None, properties=None):
    if self.allow_duplicate_keys is False:
        if self.contains(key=key):
            return None
    row_data = self.create_node_data(key,value, properties)
    return self.add_row_and_commit(**row_data)
def create_node_data(self, key, value=None, properties=None)
Expand source code
def create_node_data(self, key, value=None, properties=None):
    node_data =  {'key'        : key                     ,
                  'value'      : value                   ,
                  'properties' : properties              }
    if self.set_timestamp:
        node_data['timestamp'] = timestamp_utc_now()
    return node_data
def keys(self)
Expand source code
def keys(self):
    return unique(self.select_field_values('key'))
def nodes(self)
Expand source code
@index_by
def nodes(self):
    return self.rows()
def setup(self)
Expand source code
def setup(self):
    if self.exists() is False:
        self.create()
        self.index_create('key')
    return self

Inherited members