mt_metadata.base.helpers
Created on Wed Dec 23 20:37:52 2020
- copyright:
Jared Peacock (jpeacock@usgs.gov)
- license:
MIT
Attributes
Classes
Need to encode numpy ints and floats for json to work |
Functions
|
split a description into separate lines |
|
Validate column 1 width based on attribute dictionary |
|
Takes a dictionary of field names to FieldInfo objects and parses it into a table |
|
|
|
|
|
Flatten a nested list |
|
recursively split a dictionary |
|
|
|
|
|
Recursively split a name and set the value of the last key. Recursion splits on the separator present in the name. |
|
|
|
|
|
|
|
|
|
Assumes dictionary is structured {class:{attribute_dict}} |
|
|
|
|
|
Validate name |
|
Check if a string contains any numeric characters. |
|
Check if a string represents a valid number (int or float). |
|
Extract all numeric values from a string. |
|
Decorate a function with optional dependencies. |
|
Convert a value to a numpy array. |
Module Contents
- mt_metadata.base.helpers.filter_descriptions
- mt_metadata.base.helpers.wrap_description(description, column_width)
split a description into separate lines
- mt_metadata.base.helpers.validate_c1(attr_dict, c1)
Validate column 1 width based on attribute dictionary
- Parameters:
attr_dict (dict) – DESCRIPTION
c1 (int) – DESCRIPTION
- Returns:
DESCRIPTION
- Return type:
int
- mt_metadata.base.helpers.write_lines(field_dict, c1=45, c2=45, c3=15)
Takes a dictionary of field names to FieldInfo objects and parses it into a table Returns a string representation of this table. This overwrites the doc.
- Parameters:
field_dict (dict) – dictionary mapping field names to FieldInfo objects
c1 (int, optional) – column 1 width, by default 45
c2 (int, optional) – column 2 width, by default 45
c3 (int, optional) – column 3 width, by default 15
- Returns:
doc string
- Return type:
str
- mt_metadata.base.helpers.write_block(key, field_info, c1=45, c2=45, c3=15)
- Parameters:
key (string) – key to write from attr dict
field_info (dict) – field information dictionary
c1 (int, optional) – column 1 width, defaults to 45
c2 (int, optional) – column 2 width, defaults to 45
c3 (int, optional) – column 3 width, defaults to 15
- Returns:
list of lines
- Return type:
list
- mt_metadata.base.helpers.flatten_dict(meta_dict, parent_key=None, sep='.')
- Parameters:
meta_dict (TYPE) – DESCRIPTION
parent_key (TYPE, optional) – DESCRIPTION, defaults to None
sep (TYPE, optional) – DESCRIPTION, defaults to ‘.’
- Returns:
DESCRIPTION
- Return type:
TYPE
- mt_metadata.base.helpers.flatten_list(x_list)
Flatten a nested list flatten = lambda l: [item for sublist in l for item in sublist]
- Return type:
None.
- mt_metadata.base.helpers.recursive_split_dict(key, value, remainder, sep='.')
recursively split a dictionary
- Parameters:
key (TYPE) – DESCRIPTION
value (TYPE) – DESCRIPTION
remainder (TYPE) – DESCRIPTION
- Returns:
DESCRIPTION
- Return type:
TYPE
- mt_metadata.base.helpers.get_by_alias(model, alias_name)
- mt_metadata.base.helpers.recursive_split_getattr(base_object, name, sep='.')
- mt_metadata.base.helpers.recursive_split_setattr(base_object, name, value, sep='.', skip_validation=False)
Recursively split a name and set the value of the last key. Recursion splits on the separator present in the name.
- Parameters:
base_object (object) – The object having its attribute set, or a “parent” object in the recursive/nested scenario
name (str) – The name of the attribute to set
value (any) – The value to set the attribute to
sep (str, optional) – The separator to split the name on, defaults to “.”
skip_validation (Optional[bool]) – Whether to skip validation/parse of the attribute, defaults to False
- Returns:
None
- Return type:
NoneType
- mt_metadata.base.helpers.structure_dict(meta_dict, sep='.')
- Parameters:
meta_dict (TYPE) – DESCRIPTION
sep (TYPE, optional) – DESCRIPTION, defaults to ‘.’
- Returns:
DESCRIPTION
- Return type:
TYPE
- mt_metadata.base.helpers.get_units(name, attr_dict)
- mt_metadata.base.helpers.get_type(name, attr_dict)
- mt_metadata.base.helpers.recursive_split_xml(element, item, base, name, attr_dict=None)
- mt_metadata.base.helpers.dict_to_xml(meta_dict, attr_dict=None)
Assumes dictionary is structured {class:{attribute_dict}}
- Parameters:
meta_dict (TYPE) – DESCRIPTION
- Returns:
DESCRIPTION
- Return type:
TYPE
- mt_metadata.base.helpers.element_to_dict(element)
- Parameters:
element (TYPE) – DESCRIPTION
- Returns:
DESCRIPTION
- Return type:
TYPE
- mt_metadata.base.helpers.element_to_string(element)
- class mt_metadata.base.helpers.NumpyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
Bases:
json.JSONEncoderNeed to encode numpy ints and floats for json to work
- default(obj)
- Parameters:
obj
- Returns:
- mt_metadata.base.helpers.validate_name(name, pattern=None)
Validate name
- Parameters:
name (TYPE) – DESCRIPTION
pattern (TYPE, optional) – DESCRIPTION, defaults to None
- Returns:
DESCRIPTION
- Return type:
TYPE
- mt_metadata.base.helpers.has_numbers(text)
Check if a string contains any numeric characters.
- Parameters:
text (str) – The string to check for numeric characters.
- Returns:
True if the string contains any digits (0-9), False otherwise.
- Return type:
bool
Examples
>>> has_numbers("abc123") True >>> has_numbers("hello") False >>> has_numbers("test1") True >>> has_numbers("") False
- mt_metadata.base.helpers.is_numeric_string(text)
Check if a string represents a valid number (int or float).
- Parameters:
text (str) – The string to check if it represents a number.
- Returns:
True if the string can be converted to a number, False otherwise.
- Return type:
bool
Examples
>>> is_numeric_string("123") True >>> is_numeric_string("12.34") True >>> is_numeric_string("-45.6") True >>> is_numeric_string("1.23e-4") True >>> is_numeric_string("abc") False >>> is_numeric_string("12abc") False
- mt_metadata.base.helpers.extract_numbers(text)
Extract all numeric values from a string.
- Parameters:
text (str) – The string to extract numbers from.
- Returns:
List of float values found in the string.
- Return type:
list
Examples
>>> extract_numbers("abc123def45.6") [123.0, 45.6] >>> extract_numbers("no numbers here") [] >>> extract_numbers("1.5 and -2.3e4") [1.5, -23000.0]
- mt_metadata.base.helpers.requires(**requirements)
Decorate a function with optional dependencies.
- Parameters:
**requirements (obj) – keywords of package name and the required object for a function.
- Returns:
decorated_function – Original function if all soft dependencies are met, otherwise it returns an empty function which prints why it is not running.
- Return type:
function
Examples
``` try:
import obspy
- except ImportError:
obspy = None
@requires(obspy=obspy) def obspy_function():
… # does something using obspy
- mt_metadata.base.helpers.object_to_array(value, dtype=float)
Convert a value to a numpy array.
- Parameters:
value (any) – The value to convert.
- Returns:
The converted numpy array.
- Return type:
np.ndarray