[ase-users] Weird behaviour in "Parameters" class defined in calculator.py

Jens Jørgen Mortensen jensj at fysik.dtu.dk
Thu Mar 2 09:53:52 CET 2017


On 03/01/2017 11:20 PM, Joakim Löfgren via ase-users wrote:
>
> 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.
>

Thanks you for pointing this out.  I've fixed this in master by simply 
removing the Parameters.update() method so now it'll behave like a 
normal dict.

Jens Jørgen

>
> 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
>
>
>
>
> _______________________________________________
> ase-users mailing list
> ase-users at listserv.fysik.dtu.dk
> https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users



More information about the ase-users mailing list