pds4_tools.utils.helpers module¶
Functions¶
cast_int_float_string (value) |
Cast given string value, if possible, to an int, float or returns unchanged. |
is_array_like (value) |
Array-like values are defined as those that implement __len__ (such as list , tuple , array.array , np.ndarray , etc) but are not str , unicode or bytes . |
finite_min_max (array_like) |
Obtain finite (non-NaN, non-Inf) minimum and maximum of an array. |
dict_extract (nested_dict, key) |
Recursively searches nested dictionaries. |
xml_to_dict (xml_element[, skip_attributes, ...]) |
Transforms XML to an OrderedDict . |
Details¶
-
cast_int_float_string
(value)[source]¶ Cast given string value, if possible, to an int, float or returns unchanged.
Parameters: value : str or unicode
Value to try casting to an int and float.
Returns: int, float, str or unicode
Cast value.
-
is_array_like
(value)[source]¶ Array-like values are defined as those that implement __len__ (such as
list
,tuple
,array.array
,np.ndarray
, etc) but are notstr
,unicode
orbytes
.Parameters: value
Any kind of value.
Returns: bool
True if value is array-like, false otherwise.
-
finite_min_max
(array_like)[source]¶ Obtain finite (non-NaN, non-Inf) minimum and maximum of an array.
Parameters: array_like : array_like
A numeric array of some kind, possibly containing NaN or Inf values.
Returns: tuple
Two-valued tuple containing the finite minimum and maximum of array_like.
-
dict_extract
(nested_dict, key)[source]¶ Recursively searches nested dictionaries.
nested_dict may contain other dictionaries, or other array-like’s that have dictionaries inside: all dictionaries anywhere will be searched.
Adapted from http://stackoverflow.com/a/29652561.
Parameters: nested_dict : dict or OrderedDict
A dictionary potentially containing an arbitrary number of other dictionaries.
key : str or unicode
The key to search for in nested_dict.
Returns: generator
Found values for key in any dictionary inside nested_dict.
Notes
This code is generally efficient. However, if you pass it a dictionary that has a huge array nested within it, it will not be performant because it will try to search each value in the array for a dictionary (this is by design; the intent if non-dict array-like’s are present is to search them on the assumption they will be small where using this function makes sense).
Examples
>>> d = { "id" : "abcde", "key1" : "blah", "key2" : "blah blah", "nestedlist" : [ "blah blah", { "id" : "qwerty", "key1": "blah"} ] } >>> result = dict_extract(d, 'id')
>>> print(list(result)) ['abcde', 'qwerty']
-
xml_to_dict
(xml_element, skip_attributes=False, cast_values=False, cast_ignore=(), tag_modify=())[source]¶ Transforms XML to an
OrderedDict
.Takes an XML
ElementTree
Element or aLabel
and creates an equivalentOrderedDict
. Keys of the dictionary represent tag names and values represent the text values of the elements. In case of a (sub)element having child elements, values will be anotherOrderedDict
, inside which the text of the element has key ‘_text’. In case of (sub)elements having child elements with the same key, the value for the key will be alist
. In case of (sub)elements with attributes, the value will be anOrderedDict
, inside which the key for each attribute starts with ‘@’ and the text of the element has key ‘_text’. For text elements, the text value is not preserved (and a ‘_text’ key is not created) if it contains only whitespace (including spaces, tabs and newlines); otherwise whitespaces are preserved.Preserves order of elements in most cases. The exception is when an element has 2 or more sets of children, where each set has the same key names (i.e., there are at least 4 children, and 2 of those children have one key, and 2 have another key) and the order of the children with the non-matching keys is intertwined, in such a case the order of the intertwined keys will not be preserved.
Adapted from http://stackoverflow.com/a/10076823.
Parameters: xml_element :
ElementTree
Element or LabelXML representation which will be turned into a dictionary.
skip_attributes : bool, optional
If True, skips adding attributes from XML. Defaults to False.
cast_values : bool, optional
If True, float and int compatible values of element text and attribute values will be cast as such in the output dictionary. Defaults to False.
cast_ignore : tuple[str or unicode], optional
If given, then a tuple of element tags and/or attribute names. If cast_values is True, then for elements and attributes matching exactly the values in this tuple, values will not be cast. Attribute names must be prepended by an ‘@’. If tag_modify is set, then tags and attribute names specified by cast_ignore should be the already tag modified versions. Empty by default.
tag_modify : tuple, optional
If given, then a 2-valued tuple with str or unicode values, or a tuple of 2-valued tuples. Any match, including partial, in element tag names and/or attributes names for each tag_modify[0] is replaced with tag_modify[1]. Empty by default.
Returns: OrderedDict
Dictionary representation of the XML input.