PLAMS icon indicating copy to clipboard operation
PLAMS copied to clipboard

Change `init` from a function into a context manager

Open BvB93 opened this issue 4 years ago • 2 comments

This PR changes plams.init from a function into a context manager.

In addition to calling it as a normal function, as could be done previously, plams.init can now be used on conjunction with the with statement. In the latter case plams.finish will be automatically called upon exiting the context manager.

Examples

>>> from scm.plams import Molecule, Settings, AMSJob, init, finish

>>> mol: Molecule = ...
>>> settings: Settings = ...

>>> with init():
...     job1 = AMSJob(molecule=mol, settings=settings)
...     result1 = job1.run()

# Equivalently:
>>> init()
>>> job2 = AMSJob(molecule=mol, settings=settings)
>>> result2 = job2.run()
>>> finish()

BvB93 avatar Dec 01 '21 12:12 BvB93

If possible it would be nice to have a unit test proving that both the context manager syntax and the normal syntax work.

miron-scm avatar Jun 08 '23 12:06 miron-scm

If possible it would be nice to have a unit test proving that both the context manager syntax and the normal syntax work.

Sorry, but considering the PLAMS test suite (at least on github) currently lacks any sort of infrastructure for testing the likes of init, job managers or running actual jobs I'm going to have to decline here, as this would involve this would effectively require setting up a whole new suite of testing infrastructure.

BvB93 avatar Jun 16 '23 12:06 BvB93