diff --git a/core/functions.py b/core/functions.py index a7ddbb468..7b890b614 100644 --- a/core/functions.py +++ b/core/functions.py @@ -24,7 +24,7 @@ #=========================================================================== -class init(contextlib.AbstractContextManager): +class init(contextlib.AbstractContextManager, contextlib.ContextDecorator): """Initialize PLAMS environment. Create global ``config`` and the default |JobManager|. An empty |Settings| instance is created and populated with default settings by executing ``plams_defaults``. The following locations are used to search for the defaults file, in order of precedence: @@ -37,11 +37,11 @@ class init(contextlib.AbstractContextManager): Optionally, an additional `dict` (or |Settings| instance) can be provided to the `config_settings` argument which will be used to update the values from the ``plams_defaults``. - |init| can be either used as a standalone function or in conjunction with the ``with`` statement, the latter option automatically calling |finish| upon exiting the context manager: + |init| can be either used as a standalone function, in conjunction with the ``with`` statement or as a decorator, the latter two options automatically calling |finish| upon exiting the context manager: .. code-block:: python - >>> from scm.plams import Molecule, Settings, AMSJob, init, finish + >>> from scm.plams import Molecule, Settings, AMSJob, AMSResult, init, finish >>> mol: Molecule = ... >>> settings: Settings = ... @@ -50,6 +50,11 @@ class init(contextlib.AbstractContextManager): ... job1 = AMSJob(molecule=mol, settings=settings) ... result1 = job1.run() + >>> @init() + ... def run_job(mol: Molecule, settings: Settings) -> AMSResult + ... job1 = AMSJob(molecule=mol, settings=settings) + ... return job1.run() + # Equivalently: >>> init() >>> job2 = AMSJob(molecule=mol, settings=settings)