Skip to content

Commit

Permalink
Also inherit init from contextlib.ContextDecorator so it can be u…
Browse files Browse the repository at this point in the history
…sed as a function decorator
  • Loading branch information
BvB93 committed May 27, 2022
1 parent 0393e96 commit e93e37f
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions core/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 = ...
Expand All @@ -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)
Expand Down

0 comments on commit e93e37f

Please sign in to comment.