dantro.tools module

This module implements tools that are generally useful in dantro

dantro.tools.recursive_update(d: dict, u: dict) → dict[source]

Recursively updates the Mapping-like object d with the Mapping-like object u and returns it. Note that this does not create a copy of d!

Based on: http://stackoverflow.com/a/32357112/1827608

Parameters
  • d (dict) – The mapping to update

  • u (dict) – The mapping whose values are used to update d

Returns

The updated dict d

Return type

dict

dantro.tools.clear_line(only_in_tty=True, break_if_not_tty=True)[source]

Clears the current terminal line and resets the cursor to the first position using a POSIX command.

Based on: https://stackoverflow.com/a/25105111/1827608

Parameters
  • only_in_tty (bool, optional) – If True (default) will only clear the line if the script is executed in a TTY

  • break_if_not_tty (bool, optional) – If True (default), will insert a line break if the script is not executed in a TTY

dantro.tools.fill_line(s: str, *, num_cols: int = 79, fill_char: str = ' ', align: str = 'left') → str[source]

Extends the given string such that it fills a whole line of num_cols columns.

Parameters
  • s (str) – The string to extend to a whole line

  • num_cols (int, optional) – The number of colums of the line; defaults to the number of TTY columns or – if those are not available – 79

  • fill_char (str, optional) – The fill character

  • align (str, optional) – The alignment. Can be: ‘left’, ‘right’, ‘center’ or the one-letter equivalents.

Returns

The string of length num_cols

Return type

str

Raises

ValueError – For invalid align or fill_char argument

dantro.tools.center_in_line(s: str, *, num_cols: int = 79, fill_char: str = '·', spacing: int = 1) → str[source]

Shortcut for a common fill_line use case.

Parameters
  • s (str) – The string to center in the line

  • num_cols (int, optional) – The number of columns in the line

  • fill_char (str, optional) – The fill character

  • spacing (int, optional) – The spacing around the string s

Returns

The string centered in the line

Return type

str

dantro.tools.apply_along_axis(func, axis: int, arr: numpy.ndarray, *args, **kwargs) → numpy.ndarray[source]

This is like numpy’s function of the same name, but does not try to cast the results of func to an np.ndarray but tries to keep them as dtype object. Thus, the return value of this function will always have one fewer dimension then the input array.

This goes along the equivalent formulation of np.apply_along_axis, outlined in their documentation of the function.

dantro.tools.decode_bytestrings(obj) → str[source]

Checks whether the given attribute value is or contains byte strings and if so, decodes it to a python string.

Parameters

obj – The object to try to decode into holding python strings

Returns

Either the unchanged object or the decoded one

Return type

str

dantro.tools.is_iterable(obj) → bool[source]

Tries whether the given object is iterable.

dantro.tools.is_hashable(obj) → bool[source]

Tries whether the given object is hashable.

class dantro.tools.DoNothingContext[source]

Bases: object

A context manager that … does nothing.

__enter__()[source]

Called upon entering the context using the with statement

__exit__(*args)[source]

Called upon exiting context, with *args being exceptions etc.