spmi.utils package#
Subpackages#
Submodules#
spmi.utils.exception module#
Provides SpmiException class.
- exception spmi.utils.exception.SpmiException#
Bases:
ExceptionBase 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
packagemodules and returns a class byclassnameif 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:
objectProvides logging methods.
- Parameters:
name (
str) – Logger name.
- class DefaultFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)#
Bases:
FormatterDefault 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.Templateformatting in your format string.Changed in version 3.2: Added the
styleparameter.- 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 –
logginglog 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:
MetaDataNodeProvides property and file access to meta and data.
- Parameters:
meta (
Union[dict, pathlib.Path, None]) – Meta dictionary. EmptydictifNone.data (
Union[dict, pathlib.Path, None]) – Data dictionary. EmptydictifNone.metadata (
Union[dict, MetaData]) –MetaDataobject.mutable (
bool) – Mutable flag.copy (
bool) – Copy flag. IfTrue, deepcopiesmeta,dataandmetadata.
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.Noneif not exist.- Raises:
TypeError –
- dump()#
Dumps meta and data.
Note
Should be mutable.
- Raises:
- classmethod is_correct_meta_data(data, meta=None)#
Returns
Trueif 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.Noneif 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:
objectProvides property access to
metaanddatadictionaries.datais immutable dictionary,metais mutable. Ifmutableflag is set toFalse,metabecome immutable.- Parameters:
meta (
Union[dict, None]) – Meta dictionary. Empty ifNone.data (
Union[dict, None]) – Data dictionary. Empty ifNone.metadata (
Union[dict, MetaDataNode]) –MetaDataNodeobject.mutable (
bool) – Mutable flag.copy (
bool) – Copy flag. IfTrue, deepcopiesmeta,dataandmetadata.
Note
You can set meta and data or metadata flags at once
- Raises:
TypeError –
ValueError –
- check_properties()#
Raises
IncorrectPropertyexception 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
MetaDatamay be incorrect to check on creation (e.g.MetaData.statecreates aMetaDatawhich will call this property again if it is not wrapped by this decorator). Wrap these properties with@dontcheckto prevent their check onMetaDatacreation.- Rasies:
TypeErrorAttributeError
Note
dontcheck()sets_spmi_metadata_dontcheckattribute offgetfunction.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:
objectProvides methods to match pattern strings.
- abstract is_pattern(string)#
Returns
Trueifstringis pattern.- Parameters:
string (
str) – String to check.- Returns:
bool.
- abstract match(pattern, string)#
Returns
Trueifstringmatchespattern.- Parameters:
pattern (
str) – Pattern.string (
str) – String.
- Returns:
bool.- Raises:
TypeError –
ValueError –
- class spmi.utils.pattern.RegexPatternMatcher#
Bases:
PatternMatcherRegex pattern matcher.
- is_pattern(string)#
Returns
Trueifstringis pattern.- Parameters:
string (
str) – String to check.- Returns:
bool.
- match(pattern, string)#
Returns
Trueifstringmatchespattern.- Parameters:
pattern (
str) – Pattern.string (
str) – String.
- Returns:
bool.- Raises:
TypeError –
ValueError –
- class spmi.utils.pattern.SimplePatternMatcher#
Bases:
PatternMatcherSimple pattern matcher.
- is_pattern(string)#
Returns
Trueifstringis pattern.- Parameters:
string (
str) – String to check.- Returns:
bool.
- match(pattern, string)#
Returns
Trueifstringmatchespattern.- Parameters:
pattern (
str) – Pattern.string (
str) – String.
- Returns:
bool.- Raises:
TypeError –
ValueError –