[ase-users] Weird behaviour in "Parameters" class defined in calculator.py
Joakim Löfgren
joalof at chalmers.se
Wed Mar 1 23:20:40 CET 2017
Hi,
While playing around with a custom calculator I encountered some weirdness when dealing with the Parameters class that is defined in calculator.py. The root of my issue seems to be that you can't update a Parameters object with a dictionary where one of the values is another dictionary. To my understanding Parameters is supposed to behave like a regular dict only with some extra features like shorthand access to keys.
Here is a MWE demonstrating the issue:
from ase.calculators.calculator import Parameters
p = Parameters()
p.update({'foo': 1}) # works
p.update({'bar': {'baz': 2}}) # KeyError
The code responsible (with some context) in the update method (line 189) of Parameters is:
for key, value in other:
if isinstance(value, dict) and isinstance(self[key], dict):
self[key].update(value)
else:
self[key] = value
Here, the 2nd part of the if statement fails unless the key already exists. I think this could be fixed without breaking any of the existing calculators using something along the lines of:
for key, value in other:
if isinstance(value, dict) and key in self:
self[key].update(value)
else:
self[key] = value
What do you think?
Cheers,
Joakim Löfgren
___________________
Joakim Löfgren
Ph.D. Student
Department of Physics
Division of Materials and Surface Theory
Chalmers University of Technology
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listserv.fysik.dtu.dk/pipermail/ase-users/attachments/20170301/96d2c2f2/attachment.html>
More information about the ase-users
mailing list