evariste.utils

A rag-bag of utility functions that did not fit anywhere else.

class evariste.utils.DeepDict(depth, dictionary=None)[source]

Dictionary of dictionary of … of dictionaries.

All the dictionaries (expeted the last one) are collections.defaultdict objects.

copy()[source]

Return a copy of self.

fill_blanks(other: dict)[source]

Recursively copy values of other into self.

The values are copied only if those of self are not defined.

classmethod from_configparser(config: ConfigParser) DeepDict[source]

Create a DeepDict object from a configparser.ConfigParser object.

get_subkey(subkey)[source]

Return the first self[ANY][subkey], when ANY is any dictionary key.

evariste.utils.smart_open(filename, mode='w', encoding='utf8')[source]

Open filename, standard output, or nothing.

Parameters:
  • filename (str) –

    If filename is:

    • "" (the empty string): return a fake file object,

      which is empty if file is open for reading, and can be written in if file is open for writing (but content is then discarded);

    • "-" (a dash):

      read from standard input, or write to standard output (depending on mode);

    • any other: open the given file.

  • mode (str) – Same as the mode parameter of open().

  • encoding (str) – Same as the encoding parameter of open().

evariste.utils.yesno(arg: bool | str | int | None) bool[source]

Interpret some (mostly str) variable as a boolean.

>>> yesno("y")
True
>>> yesno("0")
False
>>> yesno("1")
True
>>> yesno("Yes")
True
>>> yesno("True")
True
>>> yesno("something senseless")
False
>>> yesno(None)
False
evariste.utils.expand_path(path)[source]

Return path where environment variables and user directory have been expanded.

class evariste.utils.ChangeDir(directory)[source]

Context manager to change and restore current directory.

evariste.utils.cached_iterator(func)[source]

Like functools.cache(), but for iterators.

That is, the first time the function is run, the returned iterable is stored (as a tuple), and next calls to the function return this tuple.