stems.utils module

class stems.utils.FrozenKeyDict(*args, **kwds)[source]

Bases: collections.abc.MutableMapping

A dict that doesn’t allow new keys

copy(self)[source]
stems.utils.cached_property(prop)[source]

Cache a class property (e.g., that requires a lookup)

stems.utils.concat_lists(l)[source]

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

stems.utils.dtype_info(arr)[source]

Return integer or float dtype info

Parameters

arr (np.ndarray) – Array

Returns

NumPy type information

Return type

numpy.core.getlimits.finfo or numpy.core.getlimits.iinfo

Raises

TypeError – Raised if arr is not a int or float type

stems.utils.find(location, pattern, regex=False)[source]

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 of file paths for files found

Return type

list

stems.utils.find_subclasses(cls_)[source]

Find subclasses of an object

Parameters

cls_ (class) – A Python class

Returns

Classes that inherit from cls_

Return type

set[class]

stems.utils.flatten_lists(l)[source]

Flatten all list-like items in l

Parameters

l (list or tuple) – Sequence (but not a str)

Yields

list – Flattened list items

stems.utils.import_object(string)[source]

Import a Python module/class/function from its string

Parameters

string (str) – A Python object path

Returns

The imported object (class/module/function)

Return type

object

stems.utils.list_like(obj)[source]

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

True if obj is a list (sequence but not str)

Return type

bool

stems.utils.np_promote_all_types(*dtypes)[source]

Return the largest NumPy datatype required to hold all types

Parameters

dtypes (iterable) – NumPy datatypes to promote

Returns

Smallest NumPy datatype required to store all input datatypes

Return type

np.dtype

See also

np.promote_types()

stems.utils.register_multi_singledispatch(func, types)[source]

Register multiple types for singledispatch

Parameters
  • func (callable) – Function

  • types (tuple) – Multiple types to register

Returns

func – Decorated function

Return type

callable

stems.utils.relative_to(one, two)[source]

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

Relative path of one

Return type

Path

stems.utils.renamed_upon_completion(destination, tmpdir=None, prefix='', suffix=None)[source]

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

stems.utils.squeeze(l)[source]

Squeeze Sequences with 1 item into a scalar

Parameters

l (Sequence) – List, tuple, etc

Returns

Either original object if len(l) != 1 else l[0]

Return type

object

stems.utils.to_number(string)[source]

Convert string to most appropriate number

stems.utils.update_nested(d, other)[source]

Update a potentially nested dict with another

Parameters
  • d (dict) – Dictionary to update

  • other (dict) – Other dict with replacement values

Returns

Updated dict

Return type

dict