datasafe.utils module
General purpose functions and classes used in other modules.
To avoid circular dependencies, this module does not depend on any other modules of the datasafe package, but it can be imported into every other module.
Todo
Probably, this module should eventually be moved to the utils subpackage of the LabInform package, as it may contain utils not only useful for the datasafe component.
- datasafe.utils.object_from_name(module_name='', class_name='')[source]
Create object from module and class name
- datasafe.utils.change_working_dir(path='')[source]
Context manager for temporarily changing the working directory.
Sometimes it is necessary to temporarily change the working directory, but one would like to ensure that the directory is reverted even in case an exception is raised.
Due to its nature as a context manager, this function can be used with a
with
statement. See below for an example.- Parameters:
path (
str
) – Path the current working directory should be changed to.
Examples
To temporarily change the working directory:
with change_working_dir(os.path.join('some', 'path')): # Do something that may raise an exception
This can come in quite handy in case of tests.