spmi.utils package#
Subpackages#
Submodules#
spmi.utils.exception module#
Provides SpmiException
class.
- exception spmi.utils.exception.SpmiException#
Bases:
Exception
Base SPMI exception.
spmi.utils.load module#
Provides functions to load modules.
- spmi.utils.load.load_class_from_package(classname, package)#
Loads class from package by name.
Iterates throw
package
modules and returns a class byclassname
if finds it.- Parameters:
classname (
str
) – Classname.package (Python package) – Package.
- Returns:
class
.- Raises:
TypeError –
NotImplementedError –
spmi.utils.logger module#
Provides Logger
.
- class spmi.utils.logger.Logger(name)#
Bases:
object
Provides logging methods.
- Parameters:
name (
str
) – Logger name.
- class DefaultFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)#
Bases:
Formatter
Default logger formatter.
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.
Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting,
str.format()
({}
) formatting orstring.Template
formatting in your format string.Changed in version 3.2: Added the
style
parameter.- FORMATS = {10: '\x1b[38;20m[%(asctime)s - %(name)s - %(levelname)s]\n%(message)s\x1b[0m', 20: '\x1b[32;20m[%(asctime)s - %(name)s - %(levelname)s]\n%(message)s\x1b[0m', 30: '\x1b[33;20m[%(asctime)s - %(name)s - %(levelname)s]\n%(message)s\x1b[0m', 40: '\x1b[31;20m[%(asctime)s - %(name)s - %(levelname)s]\n%(message)s\x1b[0m', 50: '\x1b[31;1m[%(asctime)s - %(name)s - %(levelname)s]\n%(message)s\x1b[0m'}#
- bold_red = '\x1b[31;1m'#
- format(record)#
Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
- format_str = '[%(asctime)s - %(name)s - %(levelname)s]\n%(message)s'#
- green = '\x1b[32;20m'#
- grey = '\x1b[38;20m'#
- red = '\x1b[31;20m'#
- reset = '\x1b[0m'#
- yellow = '\x1b[33;20m'#
- static basic_config(loglevel='INFO')#
Sets up logging basic config.
- Parameters:
loglevel –
logging
log level.
- critical(msg)#
Critical a message.
- Parameters:
msg (
str
) – message to show.
- debug(msg)#
Debug a message.
- Parameters:
msg (
str
) – message to show.
- error(msg)#
Error a message.
- Parameters:
msg (
str
) – message to show.
- info(msg)#
Info a message.
- Parameters:
msg (
str
) – message to show.
- static log_level()#
Returns global log level.
- warning(msg)#
Warning a message.
- Parameters:
msg (
str
) – message to show.
spmi.utils.metadata module#
Provides Metadata
and SubDict
.
- exception spmi.utils.metadata.IncorrectProperty#
Bases:
MetaDataError
- class spmi.utils.metadata.MetaData(data=None, meta=None, mutable=True, metadata=None, copy=True)#
Bases:
MetaDataNode
Provides property and file access to meta and data.
- Parameters:
meta (
Union[dict, pathlib.Path, None]
) – Meta dictionary. Emptydict
ifNone
.data (
Union[dict, pathlib.Path, None]
) – Data dictionary. Emptydict
ifNone
.metadata (
Union[dict, MetaData]
) –MetaData
object.mutable (
bool
) – Mutable flag.copy (
bool
) – Copy flag. IfTrue
, deepcopiesmeta
,data
andmetadata
.
Note
You can set meta and data or metadata flags at once
- Raises:
TypeError –
ValueError –
- blocking_dump()#
Blocks and dumps meta and data.
Note
Should be mutable.
- Raises:
- blocking_load()#
Blocks and loads meta and data from files.
Note
Should be mutable.
- Raises:
- property data_path#
Union[pathlib.Path, None]
. Path to data file.None
if not exist.- Raises:
TypeError –
- dump()#
Dumps meta and data.
Note
Should be mutable.
- Raises:
- classmethod is_correct_meta_data(data, meta=None)#
Returns
True
if meta and data may be meta and data of manageable.- Parameters:
data (
Union[dict, Pathlib.path]
) – Data.meta (
Uinon[dict, Pathlib.path]
) – Meta.
- Returns:
bool
.- Raises:
TypeError –
- load()#
Loads meta and data from files.
Note
Should be mutable.
- Raises:
- property meta_path#
Union[pathlib.Path, None]
. Path to meta file.None
if not exist.- Raises:
TypeError –
- exception spmi.utils.metadata.MetaDataError#
Bases:
SpmiException
- class spmi.utils.metadata.MetaDataNode(meta=None, data=None, metadata=None, mutable=True, copy=False)#
Bases:
object
Provides property access to
meta
anddata
dictionaries.data
is immutable dictionary,meta
is mutable. Ifmutable
flag is set toFalse
,meta
become immutable.- Parameters:
meta (
Union[dict, None]
) – Meta dictionary. Empty ifNone
.data (
Union[dict, None]
) – Data dictionary. Empty ifNone
.metadata (
Union[dict, MetaDataNode]
) –MetaDataNode
object.mutable (
bool
) – Mutable flag.copy (
bool
) – Copy flag. IfTrue
, deepcopiesmeta
,data
andmetadata
.
Note
You can set meta and data or metadata flags at once
- Raises:
TypeError –
ValueError –
- check_properties()#
Raises
IncorrectProperty
exception if properties are incorrect..Iterates throw all properties and tries to get them or set with default value if. Skips properties which are wrapped with
dontcheck()
.- Raises:
- property data#
dict
. Data dictionary.Note
Returns deepcopy.
- property meta#
dict
. Meta dictionary.Note
If immutable, returns deepcopy.
- property mutable#
True if this object is mutable.
- Type:
bool
- spmi.utils.metadata.dontcheck(prop)#
Decorate properties you don’t want to check.
Some properties of
MetaData
may be incorrect to check on creation (e.g.MetaData.state
creates aMetaData
which will call this property again if it is not wrapped by this decorator). Wrap these properties with@dontcheck
to prevent their check onMetaData
creation.- Rasies:
TypeError
AttributeError
Note
dontcheck()
sets_spmi_metadata_dontcheck
attribute offget
function.Example
A get-only property:
@dontcheck @property def example_property1(self): return self.state
A get and set property:
@dontcheck @property def example_property2(self): return self.state @example_property2.setter def example_property2(self, value): pass
spmi.utils.pattern module#
Provides PatternMatcher
.
- class spmi.utils.pattern.PatternMatcher#
Bases:
object
Provides methods to match pattern strings.
- abstract is_pattern(string)#
Returns
True
ifstring
is pattern.- Parameters:
string (
str
) – String to check.- Returns:
bool
.
- abstract match(pattern, string)#
Returns
True
ifstring
matchespattern
.- Parameters:
pattern (
str
) – Pattern.string (
str
) – String.
- Returns:
bool
.- Raises:
TypeError –
ValueError –
- class spmi.utils.pattern.RegexPatternMatcher#
Bases:
PatternMatcher
Regex pattern matcher.
- is_pattern(string)#
Returns
True
ifstring
is pattern.- Parameters:
string (
str
) – String to check.- Returns:
bool
.
- match(pattern, string)#
Returns
True
ifstring
matchespattern
.- Parameters:
pattern (
str
) – Pattern.string (
str
) – String.
- Returns:
bool
.- Raises:
TypeError –
ValueError –
- class spmi.utils.pattern.SimplePatternMatcher#
Bases:
PatternMatcher
Simple pattern matcher.
- is_pattern(string)#
Returns
True
ifstring
is pattern.- Parameters:
string (
str
) – String to check.- Returns:
bool
.
- match(pattern, string)#
Returns
True
ifstring
matchespattern
.- Parameters:
pattern (
str
) – Pattern.string (
str
) – String.
- Returns:
bool
.- Raises:
TypeError –
ValueError –