mt_metadata.base.helpers ======================== .. py:module:: mt_metadata.base.helpers .. autoapi-nested-parse:: Created on Wed Dec 23 20:37:52 2020 :copyright: Jared Peacock (jpeacock@usgs.gov) :license: MIT Attributes ---------- .. autoapisummary:: mt_metadata.base.helpers.filter_descriptions Classes ------- .. autoapisummary:: mt_metadata.base.helpers.NumpyEncoder Functions --------- .. autoapisummary:: mt_metadata.base.helpers.wrap_description mt_metadata.base.helpers.validate_c1 mt_metadata.base.helpers.write_lines mt_metadata.base.helpers.write_block mt_metadata.base.helpers.flatten_dict mt_metadata.base.helpers.flatten_list mt_metadata.base.helpers.recursive_split_dict mt_metadata.base.helpers.get_by_alias mt_metadata.base.helpers.recursive_split_getattr mt_metadata.base.helpers.recursive_split_setattr mt_metadata.base.helpers.structure_dict mt_metadata.base.helpers.get_units mt_metadata.base.helpers.get_type mt_metadata.base.helpers.recursive_split_xml mt_metadata.base.helpers.dict_to_xml mt_metadata.base.helpers.element_to_dict mt_metadata.base.helpers.element_to_string mt_metadata.base.helpers.validate_name mt_metadata.base.helpers.has_numbers mt_metadata.base.helpers.is_numeric_string mt_metadata.base.helpers.extract_numbers mt_metadata.base.helpers.requires mt_metadata.base.helpers.object_to_array Module Contents --------------- .. py:data:: filter_descriptions .. py:function:: wrap_description(description, column_width) split a description into separate lines .. py:function:: validate_c1(attr_dict, c1) Validate column 1 width based on attribute dictionary :param attr_dict: DESCRIPTION :type attr_dict: dict :param c1: DESCRIPTION :type c1: int :returns: DESCRIPTION :rtype: int .. py:function:: 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. :param field_dict: dictionary mapping field names to FieldInfo objects :type field_dict: dict :param c1: column 1 width, by default 45 :type c1: int, optional :param c2: column 2 width, by default 45 :type c2: int, optional :param c3: column 3 width, by default 15 :type c3: int, optional :returns: doc string :rtype: str .. py:function:: write_block(key, field_info, c1=45, c2=45, c3=15) :param key: key to write from attr dict :type key: string :param field_info: field information dictionary :type field_info: dict :param c1: column 1 width, defaults to 45 :type c1: int, optional :param c2: column 2 width, defaults to 45 :type c2: int, optional :param c3: column 3 width, defaults to 15 :type c3: int, optional :return: list of lines :rtype: list .. py:function:: flatten_dict(meta_dict, parent_key=None, sep='.') :param meta_dict: DESCRIPTION :type meta_dict: TYPE :param parent_key: DESCRIPTION, defaults to None :type parent_key: TYPE, optional :param sep: DESCRIPTION, defaults to '.' :type sep: TYPE, optional :return: DESCRIPTION :rtype: TYPE .. py:function:: flatten_list(x_list) Flatten a nested list flatten = lambda l: [item for sublist in l for item in sublist] :rtype: None. .. py:function:: recursive_split_dict(key, value, remainder, sep='.') recursively split a dictionary :param key: DESCRIPTION :type key: TYPE :param value: DESCRIPTION :type value: TYPE :param remainder: DESCRIPTION :type remainder: TYPE :return: DESCRIPTION :rtype: TYPE .. py:function:: get_by_alias(model, alias_name) .. py:function:: recursive_split_getattr(base_object, name, sep='.') .. py:function:: 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. :param base_object: The object having its attribute set, or a "parent" object in the recursive/nested scenario :type base_object: object :param name: The name of the attribute to set :type name: str :param value: The value to set the attribute to :type value: any :param sep: The separator to split the name on, defaults to "." :type sep: str, optional :param skip_validation: Whether to skip validation/parse of the attribute, defaults to False :type skip_validation: Optional[bool] :return: None :rtype: NoneType .. py:function:: structure_dict(meta_dict, sep='.') :param meta_dict: DESCRIPTION :type meta_dict: TYPE :param sep: DESCRIPTION, defaults to '.' :type sep: TYPE, optional :return: DESCRIPTION :rtype: TYPE .. py:function:: get_units(name, attr_dict) .. py:function:: get_type(name, attr_dict) .. py:function:: recursive_split_xml(element, item, base, name, attr_dict=None) .. py:function:: dict_to_xml(meta_dict, attr_dict=None) Assumes dictionary is structured {class:{attribute_dict}} :param meta_dict: DESCRIPTION :type meta_dict: TYPE :return: DESCRIPTION :rtype: TYPE .. py:function:: element_to_dict(element) .. todo:: Add way to read in attritues like units and validate them. :param element: DESCRIPTION :type element: TYPE :return: DESCRIPTION :rtype: TYPE .. py:function:: element_to_string(element) .. py:class:: NumpyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None) Bases: :py:obj:`json.JSONEncoder` Need to encode numpy ints and floats for json to work .. py:method:: default(obj) :param obj: :type obj: :return: .. py:function:: validate_name(name, pattern=None) Validate name :param name: DESCRIPTION :type name: TYPE :param pattern: DESCRIPTION, defaults to None :type pattern: TYPE, optional :return: DESCRIPTION :rtype: TYPE .. py:function:: has_numbers(text) Check if a string contains any numeric characters. :param text: The string to check for numeric characters. :type text: str :returns: True if the string contains any digits (0-9), False otherwise. :rtype: bool .. rubric:: Examples >>> has_numbers("abc123") True >>> has_numbers("hello") False >>> has_numbers("test1") True >>> has_numbers("") False .. py:function:: is_numeric_string(text) Check if a string represents a valid number (int or float). :param text: The string to check if it represents a number. :type text: str :returns: True if the string can be converted to a number, False otherwise. :rtype: bool .. rubric:: 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 .. py:function:: extract_numbers(text) Extract all numeric values from a string. :param text: The string to extract numbers from. :type text: str :returns: List of float values found in the string. :rtype: list .. rubric:: 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] .. py:function:: requires(**requirements) Decorate a function with optional dependencies. :param \*\*requirements: keywords of package name and the required object for a function. :type \*\*requirements: obj :returns: **decorated_function** -- Original function if all soft dependencies are met, otherwise it returns an empty function which prints why it is not running. :rtype: function .. rubric:: Examples ``` try: import obspy except ImportError: obspy = None @requires(obspy=obspy) def obspy_function(): ... # does something using obspy .. py:function:: object_to_array(value, dtype=float) Convert a value to a numpy array. :param value: The value to convert. :type value: any :returns: The converted numpy array. :rtype: np.ndarray