BF-VARIOUS - Various Functions

../../../../../../../_images/MLPro-BF-Various_class_diagram.drawio.png

Ver. 2.1.2 (2023-06-01)

This module provides various classes with elementry functionalities for reuse in higher level classes. For example: logging, persistence, timer…

class mlpro.bf.various.Id(p_id=None)

Bases: object

Property class that inherits a unique id and related get/set-methods to a child class.

Parameters:
  • p_id – Optional external id

  • Attributes

  • _id – Unique id of the object.

get_id()
set_id(p_id=None)

Sets/generates a new id.

Parameters:

p_id – Optional external id. If None, a unique id is generated.

class mlpro.bf.various.Log(p_logging=True)

Bases: object

This class adds elementry log functionality to inherited classes.

Parameters:

p_logging – Log level (see constants C_LOG_*). Default: Log.C_LOG_ALL

C_TYPE = '????'
C_NAME = '????'
C_LOG_TYPE_I = 'I'
C_LOG_TYPE_W = 'W'
C_LOG_TYPE_E = 'E'
C_LOG_TYPE_S = 'S'
C_LOG_TYPES = ['I', 'W', 'E', 'S']
C_COL_WARNING = '\x1b[93m'
C_COL_ERROR = '\x1b[91m'
C_COL_SUCCESS = '\x1b[32m'
C_COL_RESET = '\x1b[0m'
C_LOG_ALL = True
C_LOG_NOTHING = False
C_LOG_WE = 'W'
C_LOG_E = 'E'
C_LOG_LEVELS = [True, False, 'W', 'E']
C_INST_MSG = True
get_name() str
set_name(p_name: str)
switch_logging(p_logging)

Sets new log level.

Parameters:

p_logging – Log level (constant C_LOG_LEVELS contains valid values)

get_log_level()
log(p_type, *p_args)

Writes log line to standard output in format: yyyy-mm-dd hh:mm:ss.mmmmmm [p_type C_TYPE C_NAME]: [p_args]

Parameters:
  • entry (p_type type of log)

  • informations (p_args log)

Returns:

Nothing

class mlpro.bf.various.Persistent(p_id=None, p_logging=True)

Bases: Id, Log

Property class that inherits persistence to its child classes.

Parameters:
  • p_id – Optional external id

  • p_logging – Log level (see constants C_LOG_*). Default: Log.C_LOG_ALL

C_PERSISTENCE_VERSION

Version of the implementation of the persistence. Shall be raised in child classes whenever an incompatible change has been done.

Type:

str

C_SUFFIX

Default suffix for pickled result files.

Type:

str = ‘.pkl’

C_PERSISTENCE_VERSION: str = '1.0.0'
C_SUFFIX: str = '.pkl'
get_filename_stub() str

Returns the unique filename of the object without a suffix.

Returns:

filename_stub – Filename stub.

Return type:

str

get_filename() str

Returns the full unique filename of the object including the suffix.

Returns:

filename – Full filename.

Return type:

str

set_filename(p_filename_stub: str, p_suffix: str = None)
_get_path() str

Internal helper method to determine the current path for loading/saving external data.

classmethod load(p_path: str, p_filename: str)

Static method to load an object of the current class from a file using pickle/dill. During unpickling the given file, standard method __setstate__() is called. This in turn is implemented specifically and calls the MLPro custom method _complete_state(). This method allows the completion of the unpickled object from further externally stored data.

Parameters:
  • p_path (str) – Path where file will be saved

  • p_filename (str = None) – File name (if None an internal filename will be used)

Returns:

Object of the given class that was unpickled from the given file.

Return type:

Object

_complete_state(p_path: str, p_os_sep: str, p_filename_stub: str)

Custom method to complete the object state (=self) from external data sources. This method is called by standard method __setstate__() during unpickling the object from an external file.

Parameters:
  • p_path (str) – Path of the object pickle file (and further optional related files)

  • p_os_sep (str) – OS-specific path separator.

  • p_filename_stub (str) – Filename stub to be used for further optional custom data files

save(p_path: str, p_filename: str = None) bool

Saves the object to the given path and file name using pickle/dill. If file name is None, a unique inernal file name is used (recommended). During pickling the Python standard method __getstate() is called. This in turn is implemented specifically and calls the MLPro custom method _reduce_state(). This method allows to reduce unpickleable components from the object state before pickling. These components can optionally be stored in separate files of a suitable format.

Parameters:
  • p_path (str) – Path where file will be saved

  • p_filename (str = None) – File name (if None an internal filename will be used)

Returns:

successful – True, if file content was saved successfully. False otherwise.

Return type:

bool

_reduce_state(p_state: dict, p_path: str, p_os_sep: str, p_filename_stub: str)

Custom method to reduce the given object state by components that can not be pickled. Further data files can be created in the given path and should use the given filename stub.

Parameters:
  • p_state (dict) – Object state dictionary to be reduced by components that can not be pickled.

  • p_path (str) – Path to store further optional custom data files

  • p_os_sep (str) – OS-specific path separator.

  • p_filename_stub (str) – Filename stub to be used for further optional custom data files

class mlpro.bf.various.Timer(p_mode: int, p_lap_duration: timedelta = None, p_lap_limit: int = 999999)

Bases: object

Timer class in two time modes (real/virtual) and with simple lap management.

Parameters:
  • p_mode (int) – C_MODE_REAL for real time mode or C_MODE_VIRTUAL for virtual time mode

  • p_lap_duration (timedelta = None) – Optional duration of a single lap.

  • p_lap_limit (int = C_LAP_LIMIT) – Maximum number of laps until the lap counter restarts with 0

C_MODE_REAL = 0
C_MODE_VIRTUAL = 1
C_LAP_LIMIT = 999999
reset() None

Resets timer.

Returns:

Nothing

get_time() timedelta
get_lap_time() timedelta
get_lap_id()
add_time(p_delta: timedelta)
finish_lap() bool

Finishes the current lap. In timer mode C_MODE_REAL the remaining time until the end of the lap will be paused.

Returns:

True, if the remaining time to the next lap was positive. False, if the timer timed out.

class mlpro.bf.various.TStamp(p_tstamp: timedelta = None)

Bases: object

This class provides elementry time stamp functionality for inherited classes.

get_tstamp() timedelta
set_tstamp(p_tstamp: timedelta)
class mlpro.bf.various.ScientificObject

Bases: object

This class provides elementary functionality for storing a scientific reference.

C_SCIREF_TYPE_NONE = None
C_SCIREF_TYPE_ARTICLE = 'Article'
C_SCIREF_TYPE_BOOK = 'Book'
C_SCIREF_TYPE_BOOKLET = 'Booklet'
C_SCIREF_TYPE_INBOOK = 'Inbook'
C_SCIREF_TYPE_ONLINE = 'Online'
C_SCIREF_TYPE_PROCEEDINGS = 'Proceedings'
C_SCIREF_TYPE_INPROCEEDINGS = 'Inproceedings'
C_SCIREF_TYPE_TECHREPORT = 'Technical Report'
C_SCIREF_TYPE_UNPUBLISHED = 'Unpublished'
C_SCIREF_TYPE = None
C_SCIREF_AUTHOR = None
C_SCIREF_TITLE = None
C_SCIREF_JOURNAL = None
C_SCIREF_ABSTRACT = None
C_SCIREF_VOLUME = None
C_SCIREF_NUMBER = None
C_SCIREF_PAGES = None
C_SCIREF_YEAR = None
C_SCIREF_MONTH = None
C_SCIREF_DAY = None
C_SCIREF_DOI = None
C_SCIREF_KEYWORDS = None
C_SCIREF_ISBN = None
C_SCIREF_SERIES = None
C_SCIREF_PUBLISHER = None
C_SCIREF_CITY = None
C_SCIREF_COUNTRY = None
C_SCIREF_URL = None
C_SCIREF_CHAPTER = None
C_SCIREF_BOOKTITLE = None
C_SCIREF_INSTITUTION = None
C_SCIREF_CONFERENCE = None
C_SCIREF_NOTES = None
C_SCIREF_EDITOR = None
C_SCIREF_ADDRESS = None
C_SCIREF_HOWPUBLISHED = None
C_SCIREF_NUMPAGES = None
C_SCIREF_ISSN = None
C_SCIREF_VERSION = None
get_bibtex()
class mlpro.bf.various.PersonalisedStamp(p_name: str, p_id: int = None)

Bases: Id

This class serves as a base class of label to set up a name and id for another class.

Parameters:
  • p_name (str) – name of the created class.

  • p_id (int) – unique id of the created class. Default: None.

C_NAME

name of the created class. Default: ‘’.

Type:

str

C_NAME = ''
set_name(p_name: str)

This method provides a functionality to set an unique name.

Parameters:

p_name (str) – An unique name.

get_name() str

This method provides a functionality to get the unique name.

Returns:

The unique name of the related component.

Return type:

str