From e93e37ffc5708fa758e876b8bc9265f09452a89a Mon Sep 17 00:00:00 2001 From: Bas van Beek <43369155+BvB93@users.noreply.github.com> Date: Fri, 27 May 2022 15:13:35 +0200 Subject: [PATCH] Also inherit `init` from `contextlib.ContextDecorator` so it can be used as a function decorator --- core/functions.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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)