juham.database

Description

Classes implementing MQTT pub-sub networks used for data transmission between IoT nodes. These classes must be derived trom base.MQtt base class.

class juham.database.JConsole(name='jconsole')[source]

Bases: JDatabase

Database interface that simply dumps the written records to stdout solely for testing and debugging purposes.

classmethod classattrs_from_dict(attributes)

Set only the class’s own attributes from a dictionary.

Return type:

None

classmethod classattrs_to_dict()

Convert the class’s own attributes to a dictionary, excluding inherited and private ones.

Return type:

Dict[str, Any]

copy()

Create and return a copy of the current object.

This method serializes the current object to a dictionary using the to_dict method, creates a new instance of the object’s class, and populates it with the serialized data using the from_dict method.

This method uses class identifier based instantiation (see factory method pattern) to create a new instance of the object, and ‘to_dict’ and ‘from_dict’ methods to initialize object’s state.

Return type:

MasterPiece

Returns:

A new instance of the object’s class with the same state as the original object.

Example:

clone_of_john = john.copy()
database = 'home'
debug(msg, details='')

Logs the given debug message to the application log.

Parameters:
  • msg (str) – The information message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

do(action, context)

Execute the given action to the object, by calling the provided action.

Parameters:
  • action (Callable[["MasterPiece", Dict[str, Any]], bool]) – A callable that takes

  • (node

  • boolean. (context) and returns a)

  • context (Dict[str, Any]) – Any context data that the action may use.

Return type:

bool

Returns:

The return value from the executed action.

error(msg, details='')

Logs the given error message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

classmethod factory()

Fetch the dictionary holding class names and associated classes.

Returns:

with class names and associated classes

Return type:

factory

classmethod find_class(class_id)

Create an instance of the class corresponding to the given class identifier.

Parameters:
  • class_id (str) – Identifier of the class to instantiate.

  • *args – Optional arguments to pass to the class constructor.

Returns:

An instance of the class corresponding to the given class identifier.

Return type:

MasterPiece

from_dict(data_dict)

Update instance attributes from a dictionary.

Return type:

None

classmethod get_class_id()

Return the class id of the class. Each class has an unique name that can be used for instantiating the class via Object.instantiate() method.

Parameters:

cls (class) – class

Returns:

unique class identifier through which the class can be instantiated by factory method pattern.

Return type:

id (str)

classmethod has_class_method_directly(method_name)

Check if the method is defined directly in the class (not inherited).

Return type:

bool

host: str = ''
info(msg, details='')

Logs the given information message to the application log.

Parameters:
  • msg (str) – The information message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

classmethod init_class(clazz)

Initialize class. Registers the class into the class factory .

Parameters:

clazz (class) – class to be initialized

Return type:

None

classmethod instantiate(class_id, *args)

Create an instance of the class corresponding to the given class identifier.

Parameters:
  • class_id (str) – Identifier of the class to instantiate.

  • *args (Any) – Optional arguments to pass to the class constructor.

Returns:

An instance of the class corresponding to the given class identifier.

Return type:

MasterPiece

classmethod log_debug(msg, details='')

Logs the given debug message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

classmethod log_error(msg, details='')

Logs the given message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

classmethod log_info(msg, details='')

Logs the given message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

classmethod log_warning(msg, details='')

Logs the given debug message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

make_url()

Generate the URL for the composite, including all children.

Return type:

URL

org: str = 'juham'
property parent: MasterPiece | None
read(point)
Return type:

None

classmethod register()

Register the class.

Called immediately upon class initialization, right before the class attributes are loaded from the class specific configuration files.

Subclasses can extend this with custom register functionality:

class MyMasterPiece(MasterPiece):

    @classmethod
    def register(cls):
        super().register()  # Don't forget
        cls._custom_field = True
Return type:

None

resolve_url(url)

Find a MasterPiece in the hierarchy matching the URL.

Return type:

Optional[MasterPiece]

root()

Fetch the root object

Returns:

root object

Return type:

MasterPiece

run()

Run the masterpiece. Dispatches the call to payload object and returns the control to the caller.

Return type:

None

run_forever()

Run the payload forever. This method will return only when violently terminated. If the object does not have playload object, or it is not instance of ‘MasterPiece’ class then returns immediately and this method has no effect.

Return type:

None

classmethod set_log(l)

Set logger.

Parameters:

l (logger) – logger object

Return type:

None

shutdown()

Shutdown the payload object. If the payload object is None, or is not instance of MasterPiece, then the call has no effect.

Return type:

None

to_dict()

Convert instance attributes to a dictionary.

Return type:

Dict[str, Any]

token: str = ''
warning(msg, details='')

Logs the given warning message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

write(point)[source]

Write record to database table.

@param point point to be written

Return type:

None

write_point(name, tags, fields, ts)

Write record to the database table.

Parameters:
  • name (str) – name of the measurement

  • tags (dict[str, Any]) – tags (indexed keys)

  • fields (dict[str, Any])

  • ts (str)

Return type:

None

Returns:

None

class juham.database.JInflux(name='influx')[source]

Bases: JDatabase

Influx time series database version 3.

classmethod classattrs_from_dict(attributes)

Set only the class’s own attributes from a dictionary.

Return type:

None

classmethod classattrs_to_dict()

Convert the class’s own attributes to a dictionary, excluding inherited and private ones.

Return type:

Dict[str, Any]

copy()

Create and return a copy of the current object.

This method serializes the current object to a dictionary using the to_dict method, creates a new instance of the object’s class, and populates it with the serialized data using the from_dict method.

This method uses class identifier based instantiation (see factory method pattern) to create a new instance of the object, and ‘to_dict’ and ‘from_dict’ methods to initialize object’s state.

Return type:

MasterPiece

Returns:

A new instance of the object’s class with the same state as the original object.

Example:

clone_of_john = john.copy()
database = 'home'
debug(msg, details='')

Logs the given debug message to the application log.

Parameters:
  • msg (str) – The information message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

do(action, context)

Execute the given action to the object, by calling the provided action.

Parameters:
  • action (Callable[["MasterPiece", Dict[str, Any]], bool]) – A callable that takes

  • (node

  • boolean. (context) and returns a)

  • context (Dict[str, Any]) – Any context data that the action may use.

Return type:

bool

Returns:

The return value from the executed action.

error(msg, details='')

Logs the given error message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

classmethod factory()

Fetch the dictionary holding class names and associated classes.

Returns:

with class names and associated classes

Return type:

factory

classmethod find_class(class_id)

Create an instance of the class corresponding to the given class identifier.

Parameters:
  • class_id (str) – Identifier of the class to instantiate.

  • *args – Optional arguments to pass to the class constructor.

Returns:

An instance of the class corresponding to the given class identifier.

Return type:

MasterPiece

from_dict(data_dict)

Update instance attributes from a dictionary.

Return type:

None

classmethod get_class_id()

Return the class id of the class. Each class has an unique name that can be used for instantiating the class via Object.instantiate() method.

Parameters:

cls (class) – class

Returns:

unique class identifier through which the class can be instantiated by factory method pattern.

Return type:

id (str)

classmethod has_class_method_directly(method_name)

Check if the method is defined directly in the class (not inherited).

Return type:

bool

host: str = ''
info(msg, details='')

Logs the given information message to the application log.

Parameters:
  • msg (str) – The information message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

classmethod init_class(clazz)

Initialize class. Registers the class into the class factory .

Parameters:

clazz (class) – class to be initialized

Return type:

None

classmethod instantiate(class_id, *args)

Create an instance of the class corresponding to the given class identifier.

Parameters:
  • class_id (str) – Identifier of the class to instantiate.

  • *args (Any) – Optional arguments to pass to the class constructor.

Returns:

An instance of the class corresponding to the given class identifier.

Return type:

MasterPiece

classmethod log_debug(msg, details='')

Logs the given debug message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

classmethod log_error(msg, details='')

Logs the given message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

classmethod log_info(msg, details='')

Logs the given message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

classmethod log_warning(msg, details='')

Logs the given debug message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

make_url()

Generate the URL for the composite, including all children.

Return type:

URL

org: str = 'juham'
property parent: MasterPiece | None
read(point)
Return type:

None

classmethod register()

Register the class.

Called immediately upon class initialization, right before the class attributes are loaded from the class specific configuration files.

Subclasses can extend this with custom register functionality:

class MyMasterPiece(MasterPiece):

    @classmethod
    def register(cls):
        super().register()  # Don't forget
        cls._custom_field = True
Return type:

None

resolve_url(url)

Find a MasterPiece in the hierarchy matching the URL.

Return type:

Optional[MasterPiece]

root()

Fetch the root object

Returns:

root object

Return type:

MasterPiece

run()

Run the masterpiece. Dispatches the call to payload object and returns the control to the caller.

Return type:

None

run_forever()

Run the payload forever. This method will return only when violently terminated. If the object does not have playload object, or it is not instance of ‘MasterPiece’ class then returns immediately and this method has no effect.

Return type:

None

classmethod set_log(l)

Set logger.

Parameters:

l (logger) – logger object

Return type:

None

shutdown()

Shutdown the payload object. If the payload object is None, or is not instance of MasterPiece, then the call has no effect.

Return type:

None

to_dict()

Convert instance attributes to a dictionary.

Return type:

Dict[str, Any]

token: str = ''
warning(msg, details='')

Logs the given warning message to the application log.

Parameters:
  • msg (str) – The message to be logged.

  • details (str) – Additional detailed information for the message to be logged

Return type:

None

write(point)[source]

Write record to database table.

@param point point to be written

Return type:

None

write_point(name, tags, fields, ts)[source]

Write record to the database table.

Parameters:
  • name (str) – name of the measurement

  • tags (dict[str, Any]) – tags (indexed keys)

  • fields (dict[str, Any])

  • ts (str)

Return type:

None

Returns:

None