cmsml.util#

Helpful functions and utilities.

Functions:

is_lazy_iterable(obj)

Returns whether obj is iterable lazily, such as generators, range objects, maps, etc.

make_list(obj[, cast])

Converts an object obj to a list and returns it.

tmp_file([create, delete])

Prepares a temporary file and opens a context yielding its path.

tmp_dir([create, delete])

Prepares a temporary directory and opens a context yielding its path.

Classes:

MockModule(name)

Mockup object that resembles a module with arbitrarily deep structure such that, e.g.,

is_lazy_iterable(obj: Any) bool[source]#

Returns whether obj is iterable lazily, such as generators, range objects, maps, etc.

make_list(obj: Any, cast: bool = True) list[Any][source]#

Converts an object obj to a list and returns it. Objects of types tuple and set are converted if cast is True. Otherwise, and for all other types, obj is put in a new list.

tmp_file(create=False, delete=True, **kwargs)[source]#

Prepares a temporary file and opens a context yielding its path. When create is True, the file is created before the context is opened, and deleted upon closing if delete is True. All kwargs are forwarded to tempfile.mkstemp().

tmp_dir(create=True, delete=True, **kwargs)[source]#

Prepares a temporary directory and opens a context yielding its path. When create is True, the directory is created before the context is opened, and deleted upon closing if delete is True. All kwargs are forwarded to tempfile.mkdtemp().

class MockModule(name: str)[source]#

Mockup object that resembles a module with arbitrarily deep structure such that, e.g.,

tf = MockModule("tensorflow")
print(tf.Graph)
# -> "<MockupModule 'tf' at 0x981jald1>"

will always succeed at declaration time.

_name#

type: str

The name of the mock module.