Module osbot_utils.helpers.sqlite.domains.Sqlite__DB__Requests

Expand source code
from osbot_utils.decorators.methods.cache_on_self import cache_on_self
from osbot_utils.helpers.sqlite.domains.Sqlite__DB__Local               import Sqlite__DB__Local
from osbot_utils.helpers.sqlite.domains.schemas.Schema__Table__Requests import Schema__Table__Requests
from osbot_utils.utils.Misc import random_text

SQLITE_TABLE__REQUESTS = 'requests'

class Sqlite__DB__Requests(Sqlite__DB__Local):
    table_name  : str
    table_schema: type

    def __init__(self,db_path=None, db_name=None, table_name=None):
        self.table_name   = table_name or SQLITE_TABLE__REQUESTS
        self.table_schema = Schema__Table__Requests
        super().__init__(db_path=db_path, db_name=db_name)
        # if not self.table_name:
        #     self.table_name = 'temp_table'
        self.setup()

    @cache_on_self
    def table_requests(self):
        return self.table(self.table_name)

    def table_requests__create(self):
        with self.table_requests() as _:
            _.row_schema = self.table_schema                    # set the table_class
            if _.exists() is False:
                _.create()                                          # create if it doesn't exist
                _.index_create('request_hash')                      # add index to the request_hash field
                return True
        return False

    def table_requests__reset(self):
        self.table_requests().delete()
        return self.table_requests__create()

    def setup(self):
        self.table_requests__create()
        return self

Classes

class Sqlite__DB__Requests (db_path=None, db_name=None, table_name=None)

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__DB__Requests(Sqlite__DB__Local):
    table_name  : str
    table_schema: type

    def __init__(self,db_path=None, db_name=None, table_name=None):
        self.table_name   = table_name or SQLITE_TABLE__REQUESTS
        self.table_schema = Schema__Table__Requests
        super().__init__(db_path=db_path, db_name=db_name)
        # if not self.table_name:
        #     self.table_name = 'temp_table'
        self.setup()

    @cache_on_self
    def table_requests(self):
        return self.table(self.table_name)

    def table_requests__create(self):
        with self.table_requests() as _:
            _.row_schema = self.table_schema                    # set the table_class
            if _.exists() is False:
                _.create()                                          # create if it doesn't exist
                _.index_create('request_hash')                      # add index to the request_hash field
                return True
        return False

    def table_requests__reset(self):
        self.table_requests().delete()
        return self.table_requests__create()

    def setup(self):
        self.table_requests__create()
        return self

Ancestors

Class variables

var table_name : str
var table_schema : type

Methods

def setup(self)
Expand source code
def setup(self):
    self.table_requests__create()
    return self
def table_requests(self)
Expand source code
@cache_on_self
def table_requests(self):
    return self.table(self.table_name)
def table_requests__create(self)
Expand source code
def table_requests__create(self):
    with self.table_requests() as _:
        _.row_schema = self.table_schema                    # set the table_class
        if _.exists() is False:
            _.create()                                          # create if it doesn't exist
            _.index_create('request_hash')                      # add index to the request_hash field
            return True
    return False
def table_requests__reset(self)
Expand source code
def table_requests__reset(self):
    self.table_requests().delete()
    return self.table_requests__create()

Inherited members