umami.tools package#
Submodules#
umami.tools.tools module#
Collection of tools used in different places in the project.
- umami.tools.tools.atoi(text)#
Return string as int, if the given string is a int.
- Parameters:
text (str) – String with int inside.
- Returns:
Int_string – Returning the string if it is not a digit, otherwise return string as int.
- Return type:
int/str
- umami.tools.tools.check_main_class_input(main_class) list #
Checks the given main class for type and returns a list with the main classes inside.
- Parameters:
main_class (str or list) – Main class loaded from the yaml file.
- Returns:
main_class – The main class(es) as a list.
- Return type:
list
- Raises:
TypeError – If the given main_class is neither a string, list or a set.
- umami.tools.tools.check_option_definition(variable_to_check, variable_name: str, needed_type: list, check_for_nan: bool) None #
Check if the given variable is correctly defined.
- Parameters:
variable_to_check – Variable which is to be checked.
variable_name (str) – Name of the variable (for logger).
needed_type (list) – List of allowed types for the variable.
check_for_nan (bool) – Bool, if the variable needs to be set (True) or if a NaN value is also allowed (False).
- Raises:
ValueError – If you havn’t/wronly defined a variable which is needed.
- umami.tools.tools.compare_leading_spaces(ref: str, comp: str)#
Compares if leading spaces of 2 strings are the same.
- Parameters:
ref (str) – reference string
comp (str) – comparison string
- Returns:
difference in leading spaces of ref and comp string
- Return type:
int
- umami.tools.tools.flatten(nested_list: list)#
Flatten an arbitrarily nested list. from https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists
- Parameters:
nested_list (list) – Arbitrarily nested list.
- Yields:
list – Flattened list elements.
- umami.tools.tools.flatten_list(nested_list: list)#
Flatten an arbitrarily nested list.
- Parameters:
nested_list (list) – Arbitrarily nested list.
- Returns:
Flattened list or None if nested_list input was None
- Return type:
list
- umami.tools.tools.natural_keys(text)#
Sorting strings by natural keys.
- Parameters:
text (str) – String with int inside.
- Returns:
sorted_list – List with the sorted strings inside.
- Return type:
list
- umami.tools.tools.replace_line_in_file(file, key, new_line, only_first=False)#
Replace line in file
- Parameters:
file (str) – file name
key (str) – key which triggers the replacement of line
new_line (str) – content of line replacement
only_first (bool, optional) – if True only first line in which key found is replaced, by default False
- Raises:
AttributeError – If no matching line could be found.
AttributeError – If no matching line could be found.
umami.tools.yaml_tools module#
code taken from https://github.com/Tristan-Sweeney-CambridgeConsultants/ccorp_yaml_include and adapted for the needs of the umami framework
- class umami.tools.yaml_tools.CompositingComposer(loader: Any = None)#
Bases:
Composer
Composer class extension of ruamel.
- classmethod add_compositor(tag, compositor, *, node_types=(<class 'ruamel.yaml.nodes.ScalarNode'>, ))#
adding compositor
- Parameters:
tag (object) – tag
compositor (object) – compositor
node_types (tuple, optional) – node type, by default (ScalarNode,)
- compose_mapping_node(anchor)#
Compose mapping node.
- Parameters:
anchor (object) – anchor
- Returns:
__compose_dispatch from super class
- Return type:
compose_dispatch object
- compose_scalar_node(anchor)#
Compose scalar node.
- Parameters:
anchor (object) – anchor
- Returns:
__compose_dispatch
- Return type:
compose_dispatch object
- compose_sequence_node(anchor)#
Compose sequence node.
- Parameters:
anchor (object) – anchor
- Returns:
__compose_dispatch from super class
- Return type:
compose_dispatch object
- compositors = {<class 'ruamel.yaml.nodes.ScalarNode'>: {'!include': <function include_compositor>}, <class 'ruamel.yaml.nodes.MappingNode'>: {}, <class 'ruamel.yaml.nodes.SequenceNode'>: {}}#
- classmethod get_compositor(tag, node_type)#
compositor getter.
- Parameters:
tag (object) – tag
node_type (object) – node type
- Returns:
compositors
- Return type:
compositor object
- class umami.tools.yaml_tools.ExcludingConstructor(preserve_quotes: bool | None = None, loader: Any = None)#
Bases:
Constructor
Constructor class extension of ruamel.
- classmethod add_filter(filter_yaml, *, node_types=(<class 'ruamel.yaml.nodes.MappingNode'>, ))#
Adding filter.
- Parameters:
filter_yaml (object) – filter
node_types (tuple, optional) – node types, by default (MappingNode,)
- construct_mapping(node)#
Construct mapping.
- Parameters:
node (object) – node
- Returns:
construct_mapping from super class
- Return type:
construct_mapping
- construct_sequence(node)#
Construct sequence.
- Parameters:
node (object) – node
- Returns:
construct_sequence from super class
- Return type:
construct_sequence object
- filters = {<class 'ruamel.yaml.nodes.MappingNode'>: [<function exclude_filter>], <class 'ruamel.yaml.nodes.SequenceNode'>: [<function exclude_filter>]}#
- class umami.tools.yaml_tools.YAML(*args, **kwargs)#
Bases:
YAML
Yaml interpreter adding !include option which support anchors.
- compose(stream)#
at this point you either have the non-pure Parser (which has its own reader and scanner) or you have the pure Parser. If the pure Parser is set, then set the Reader and Scanner, if not already set. If either the Scanner or Reader are set, you cannot use the non-pure Parser, so reset it to the pure parser and set the Reader resp. Scanner if necessary
- Parameters:
stream (stream) – stream
- Returns:
node
- Return type:
node
- fork()#
Fork function.
- Returns:
yaml object
- Return type:
yaml
- umami.tools.yaml_tools.exclude_filter(key_node, value_node=None)#
Filder exclusion.
- Parameters:
key_node (object) – key node
value_node (object, optional) – value node, by default None
- Returns:
filter
- Return type:
bool
- umami.tools.yaml_tools.include_compositor(self, anchor)#
Compositor inclusion function
- Parameters:
self (object) – self constructor from class
anchor (object) – anchor
- Returns:
composed object
- Return type:
yml.compose
- umami.tools.yaml_tools.join(loader, node)#
Custom tag handler as suggested in https://stackoverflow.com/a/57327330
- Parameters:
loader (object) – yaml loader
node (object) – yaml node
- Returns:
joint string
- Return type:
str