Module osbot_utils.helpers.sqlite.domains.Sqlite__DB__Graph
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.tables.Sqlite__Table__Edges import Sqlite__Table__Edges
from osbot_utils.helpers.sqlite.tables.Sqlite__Table__Nodes import Sqlite__Table__Nodes
class Sqlite__DB__Graph(Sqlite__DB__Local):
def __init__(self, db_path=None, db_name=None):
super().__init__(db_path=db_path, db_name=db_name)
def add_edge(self, source_key, target_key, value=None, properties=None):
new_edge = self.table_edges().add_edge(source_key, target_key, value, properties)
self.add_node(source_key) # assuming node_table.allow_duplicate_keys is set to False
self.add_node(target_key) # make sure there is a node for each part of the edge added
return new_edge
def add_node(self, key, value=None, properties=None):
return self.table_nodes().add_node(key, value, properties)
def clear(self):
self.table_edges().clear()
self.table_nodes().clear()
def edges(self):
return self.table_edges().edges()
def nodes(self):
return self.table_nodes().nodes()
def nodes_keys(self):
return self.table_nodes().keys()
@cache_on_self
def table_edges(self):
return Sqlite__Table__Edges(database=self).setup()
@cache_on_self
def table_nodes(self):
return Sqlite__Table__Nodes(database=self).setup()
def setup(self):
#self.table_config() # wire this up when I have a use case for it
self.table_nodes()
self.table_edges()
return self
Classes
class Sqlite__DB__Graph (db_path=None, db_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__Graph(Sqlite__DB__Local): def __init__(self, db_path=None, db_name=None): super().__init__(db_path=db_path, db_name=db_name) def add_edge(self, source_key, target_key, value=None, properties=None): new_edge = self.table_edges().add_edge(source_key, target_key, value, properties) self.add_node(source_key) # assuming node_table.allow_duplicate_keys is set to False self.add_node(target_key) # make sure there is a node for each part of the edge added return new_edge def add_node(self, key, value=None, properties=None): return self.table_nodes().add_node(key, value, properties) def clear(self): self.table_edges().clear() self.table_nodes().clear() def edges(self): return self.table_edges().edges() def nodes(self): return self.table_nodes().nodes() def nodes_keys(self): return self.table_nodes().keys() @cache_on_self def table_edges(self): return Sqlite__Table__Edges(database=self).setup() @cache_on_self def table_nodes(self): return Sqlite__Table__Nodes(database=self).setup() def setup(self): #self.table_config() # wire this up when I have a use case for it self.table_nodes() self.table_edges() return self
Ancestors
Methods
def add_edge(self, source_key, target_key, value=None, properties=None)
-
Expand source code
def add_edge(self, source_key, target_key, value=None, properties=None): new_edge = self.table_edges().add_edge(source_key, target_key, value, properties) self.add_node(source_key) # assuming node_table.allow_duplicate_keys is set to False self.add_node(target_key) # make sure there is a node for each part of the edge added return new_edge
def add_node(self, key, value=None, properties=None)
-
Expand source code
def add_node(self, key, value=None, properties=None): return self.table_nodes().add_node(key, value, properties)
def clear(self)
-
Expand source code
def clear(self): self.table_edges().clear() self.table_nodes().clear()
def edges(self)
-
Expand source code
def edges(self): return self.table_edges().edges()
def nodes(self)
-
Expand source code
def nodes(self): return self.table_nodes().nodes()
def nodes_keys(self)
-
Expand source code
def nodes_keys(self): return self.table_nodes().keys()
def setup(self)
-
Expand source code
def setup(self): #self.table_config() # wire this up when I have a use case for it self.table_nodes() self.table_edges() return self
def table_edges(self)
-
Expand source code
@cache_on_self def table_edges(self): return Sqlite__Table__Edges(database=self).setup()
def table_nodes(self)
-
Expand source code
@cache_on_self def table_nodes(self): return Sqlite__Table__Nodes(database=self).setup()
Inherited members