Module osbot_utils.helpers.sqlite.domains.Sqlite__Cache__Requests__Patch

Expand source code
import types

from osbot_utils.helpers.sqlite.domains.Sqlite__Cache__Requests import Sqlite__Cache__Requests
from osbot_utils.utils.Dev import pprint
from osbot_utils.utils.Misc import random_text


class Sqlite__Cache__Requests__Patch(Sqlite__Cache__Requests):
    db_name             : str                = random_text('requests_cache_')
    table_name          : str                = random_text('requests_table_')
    pickle_response     : bool               = True
    target_function     : types.FunctionType
    target_class        : object
    target_function_name: str

    def __init__(self, db_path=None):
        super().__init__(db_path=db_path, db_name=self.db_name, table_name=self.table_name)

    def __enter__(self):
        self.patch_apply()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.patch_restore()
        return

    def delete(self):
        return self.sqlite_requests.delete()

    def proxy_method(self, *args, **kwargs):
        request_data  = self.request_data (*args, **kwargs)
        target_kwargs = self.target_kwargs(*args, **kwargs)
        target_args   = self.target_args  (*args, **kwargs)

        invoke_kwargs = dict(target       = self.target_function,
                             target_args   = target_args        ,
                             target_kwargs = target_kwargs      ,
                             request_data  = request_data       )

        return self.invoke_with_cache(**invoke_kwargs)

    def patch_apply(self):
        if (type(self.target_class)   is object or
            self.target_function      is None   or
            self.target_function_name  == ''     ):
                raise ValueError('target_function, target_object and target_function_name must be set')
        def proxy(*args, **kwargs):
            return self.proxy_method(*args, **kwargs)
        setattr(self.target_class, self.target_function_name, proxy)
        return self

    def patch_restore(self):
        setattr(self.target_class, self.target_function_name, self.target_function)

    def request_data(self, *args, **kwargs):
        return {'args'  : args   ,
                'kwargs': kwargs }

    def target_args(self, *args, **kwargs):
        return args

    def target_kwargs(self, *args, **kwargs):
        return kwargs

Classes

class Sqlite__Cache__Requests__Patch (db_path=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__Cache__Requests__Patch(Sqlite__Cache__Requests):
    db_name             : str                = random_text('requests_cache_')
    table_name          : str                = random_text('requests_table_')
    pickle_response     : bool               = True
    target_function     : types.FunctionType
    target_class        : object
    target_function_name: str

    def __init__(self, db_path=None):
        super().__init__(db_path=db_path, db_name=self.db_name, table_name=self.table_name)

    def __enter__(self):
        self.patch_apply()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.patch_restore()
        return

    def delete(self):
        return self.sqlite_requests.delete()

    def proxy_method(self, *args, **kwargs):
        request_data  = self.request_data (*args, **kwargs)
        target_kwargs = self.target_kwargs(*args, **kwargs)
        target_args   = self.target_args  (*args, **kwargs)

        invoke_kwargs = dict(target       = self.target_function,
                             target_args   = target_args        ,
                             target_kwargs = target_kwargs      ,
                             request_data  = request_data       )

        return self.invoke_with_cache(**invoke_kwargs)

    def patch_apply(self):
        if (type(self.target_class)   is object or
            self.target_function      is None   or
            self.target_function_name  == ''     ):
                raise ValueError('target_function, target_object and target_function_name must be set')
        def proxy(*args, **kwargs):
            return self.proxy_method(*args, **kwargs)
        setattr(self.target_class, self.target_function_name, proxy)
        return self

    def patch_restore(self):
        setattr(self.target_class, self.target_function_name, self.target_function)

    def request_data(self, *args, **kwargs):
        return {'args'  : args   ,
                'kwargs': kwargs }

    def target_args(self, *args, **kwargs):
        return args

    def target_kwargs(self, *args, **kwargs):
        return kwargs

Ancestors

Class variables

var db_name : str
var pickle_response : bool
var table_name : str
var target_class : object
var target_function : function
var target_function_name : str

Methods

def delete(self)
Expand source code
def delete(self):
    return self.sqlite_requests.delete()
def patch_apply(self)
Expand source code
def patch_apply(self):
    if (type(self.target_class)   is object or
        self.target_function      is None   or
        self.target_function_name  == ''     ):
            raise ValueError('target_function, target_object and target_function_name must be set')
    def proxy(*args, **kwargs):
        return self.proxy_method(*args, **kwargs)
    setattr(self.target_class, self.target_function_name, proxy)
    return self
def patch_restore(self)
Expand source code
def patch_restore(self):
    setattr(self.target_class, self.target_function_name, self.target_function)
def proxy_method(self, *args, **kwargs)
Expand source code
def proxy_method(self, *args, **kwargs):
    request_data  = self.request_data (*args, **kwargs)
    target_kwargs = self.target_kwargs(*args, **kwargs)
    target_args   = self.target_args  (*args, **kwargs)

    invoke_kwargs = dict(target       = self.target_function,
                         target_args   = target_args        ,
                         target_kwargs = target_kwargs      ,
                         request_data  = request_data       )

    return self.invoke_with_cache(**invoke_kwargs)
def request_data(self, *args, **kwargs)
Expand source code
def request_data(self, *args, **kwargs):
    return {'args'  : args   ,
            'kwargs': kwargs }
def target_args(self, *args, **kwargs)
Expand source code
def target_args(self, *args, **kwargs):
    return args
def target_kwargs(self, *args, **kwargs)
Expand source code
def target_kwargs(self, *args, **kwargs):
    return kwargs

Inherited members