Coverage for stems/utils.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" """
# ============================================================================ # STANDARD DATATYPE HELPERS """ Convert string to most appropriate number """
""" Return True if ``obj`` is list-like
List-like includes lists, tuples, and numpy.ndarrays, but not other sequences like str or Mapping.
Parameters ---------- obj : object An object
Returns ------- bool True if ``obj`` is a list (sequence but not str) """ and not isinstance(obj, collections.abc.Mapping) and not isinstance(obj, str))
""" Squeeze Sequences with 1 item into a scalar
Parameters ---------- l : Sequence List, tuple, etc
Returns ------- object Either original object if ``len(l) != 1`` else ``l[0]`` """
""" Concatenate all list-like items in ``l``
Parameters ---------- l : list or tuple Sequence (but not a str)
Yields ------ list Concatenated list items
See Also -------- toolz.concat Similar function, but concatenates all iterables flatten_lists Recursive version of this function """ else:
""" Flatten all list-like items in ``l``
Parameters ---------- l : list or tuple Sequence (but not a str)
Yields ------ list Flattened list items """ else:
""" Update a potentially nested dict with another
Parameters ---------- d : dict Dictionary to update other : dict Other dict with replacement values
Returns ------- dict Updated dict """ isinstance(d_v, collections.abc.Mapping)): else:
""" A dict that doesn't allow new keys """
# ============================================================================ # STANDARD LIBRARY HELPERS # ============================================================================ """ Cache a class property (e.g., that requires a lookup) """
def wrapper(self):
""" Register multiple types for singledispatch
Parameters ---------- func : callable Function types : tuple Multiple types to register
Returns ------- func : callable Decorated function """ "`func.registry` from wrapping with `singledispatch`)")
# ============================================================================ # MODULE HELPERS # ============================================================================ """ Find subclasses of an object
Parameters ---------- cls_ : class A Python class
Returns ------- set[class] Classes that inherit from ``cls_`` """
""" Import a Python module/class/function from its string
Parameters ---------- string : str A Python object path
Returns ------- object The imported object (class/module/function) """
# ============================================================================ # NUMPY HELPERS # ============================================================================ """ Return the largest NumPy datatype required to hold all types
Parameters ---------- dtypes : iterable NumPy datatypes to promote
Returns ------- np.dtype Smallest NumPy datatype required to store all input datatypes
See Also -------- np.promote_types """ 'source bands')
"""Return integer or float dtype info
Parameters ---------- arr : np.ndarray Array
Returns ------- numpy.core.getlimits.finfo or numpy.core.getlimits.iinfo NumPy type information
Raises ------ TypeError Raised if ``arr`` is not a int or float type """ else: f'(got "{arr.dtype.kind}")')
# ============================================================================ # FILE HELPERS # ============================================================================ """ Return a sorted list of files matching pattern
Parameters ---------- location : str or pathlib.Path Directory location to search pattern : str Search pattern for files regex : bool True if ``pattern`` is a regular expression
Returns -------- list List of file paths for files found
"""
""" Return the relative path of a file compared to another
Parameters ---------- one : str or Path File to return relative path for two : str or Path File ``one`` will be relative to
Returns ------- Path Relative path of ``one`` """
# Ensure could be a file (is_file or doesn't exist yet)
""" Help save/write to file and move upon completion
Parameters ---------- destination : str or Path The final intended location of the file tmpdir : str, optional By default, this function will yield a temporary filename in the same directory as ``destination``, but you may specify another location using ``tmpdir``. prefix : str, optional Characters to prefix the temporary file with suffix : str, optional Characters to add at the end of the temporary filename. By default appends ".tmp." and the process ID
Yields ------ str A temporary filename to use during writing/saving/etc """
else:
# Yield tmpfile name for user to use for saving/etc
# We're back -- rename/move the tmpfile to ``destination`` # `shutil.move` supports move, or copy if on different device/partition |