PLAMS icon indicating copy to clipboard operation
PLAMS copied to clipboard

Allow `Molecule.as_array` to return a context manager

Open BvB93 opened this issue 3 years ago • 0 comments

This PR adds the context parameter to Molecule.as_array, allowing the latter to return a context manager.

The purpose of this addition is to make interconverion between PLAMS molecules and NumPy arrays more convenient, removing the need to explicitly call both Molecule.as_array and Molecule.from_array every single time (which is now done automatically upon exiting the context manager).

Examples

>>> from scm.plams import Molecule

>>> mol = Molecule(...)

>>> with mol.as_array(context=True) as xyz_array:
>>>     xyz_array += 5.0
>>>     xyz_array[0] = [0, 0, 0]

# Or equivalently
>>> xyz_array = mol.as_array()
>>> xyz_array += 5.0
>>> xyz_array[0] = [0, 0, 0]
>>> mol.from_array(xyz_array)

BvB93 avatar Feb 14 '22 14:02 BvB93