utilities#

Note

See the Glossary for the meaning of the acronyms used in this guide.

contexts#

plugin_dirs(dirs: Iterable[str] = ('.',)) Iterator[None][source]#
sys_path_dirs(dirs: Iterable[str] = ('.',)) Iterator[None][source]#
env_vars(env_updates: dict[str, str]) Iterator[None][source]#

Create a context for temporarily updating environment variables.

Parameters

env_updates – A dictionary that maps the environment variables (keys) to a new value. Both the keys and values must be strings.

Examples

The example below shows how to use env_vars to set/update the value of the environment variable __A within a context block. First, import the os module and check the current value of __A (we provide a a default value because this environment variable should not exist).

>>> import os
>>> print(os.getenv("__A", "default value of __A"))
default value of __A

Next, set up a with statement using env_vars to set a new value for __A confirm that the update worked.

>>> with env_vars({"__A": "new value of __A"}):
...     print(os.getenv("__A", "default value of A"))
new value of __A

Finally, confirm that __A has been restored to its default value after exiting the with statement.

>>> print(os.getenv("__A", "default value of __A"))
default value of __A

Note

This code is adapted from https://stackoverflow.com/a/34333710.

import_temp(package_name: str, path: pathlib.Path)[source]#

Context manager to temporarily add a module

decorators#

require_package(name: str, exc_message: typing.Optional[str] = None, exc_type: typing.Type[dioptra.sdk.exceptions.base.BaseOptionalDependencyError] = <class 'dioptra.sdk.exceptions.base.BaseOptionalDependencyError'>) Callable[[dioptra.sdk.utilities.decorators._require_package.T], dioptra.sdk.utilities.decorators._require_package.T][source]#

logging#

paths#

set_path_ext(filepath: Union[str, pathlib.Path], ext: str) pathlib.Path[source]#
set_cwd(temp_cwd: Union[str, pathlib.Path]) Iterator[None][source]#

A convenient way to temporarily set the current working directory to a particular directory, while executing a particular block of code. Just place the code into a with-statement which uses this function as a contextmanager.

Parameters

temp_cwd – The directory to temporarily set as current