# -*- coding: utf-8 -*-
from aiida.orm import Data
__copyright__ = u"Copyright (c), 2015, ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE (Theory and Simulation of Materials (THEOS) and National Centre for Computational Design and Discovery of Novel Materials (NCCR MARVEL)), Switzerland and ROBERT BOSCH LLC, USA. All rights reserved."
__license__ = "MIT license, see LICENSE.txt file"
__version__ = "0.5.0"
__contributors__ = "Andrea Cepellotti, Giovanni Pizzi, Martin Uhrin"
[docs]class ParameterData(Data):
"""
Pass as input in the init a dictionary, and it will get stored as internal
attributes.
Usual rules for attribute names apply (in particular, keys cannot start with
an underscore). If this is the case, a ValueError will be raised.
You can then change/delete/add more attributes before storing with the
usual methods of aiida.orm.Node
"""
[docs] def set_dict(self, dict):
"""
Replace the current dictionary with another one.
:param dict: The dictionary to set.
"""
import copy
from aiida.common.exceptions import ModificationNotAllowed
old_dict = copy.deepcopy(self.get_dict())
try:
# Delete existing attributes
self._del_all_attrs()
# I set the keys
self.update_dict(dict)
except ModificationNotAllowed:
# I reraise here to avoid to go in the generic 'except' below,
# that would raise again the same exception
raise
except:
# Try to restore the old data
self._del_all_attrs()
self.update_dict(old_dict)
raise
[docs] def update_dict(self, dict):
"""
Update the current dictionary with the keys provided in the dictionary.
:param dict: a dictionary with the keys to substitute. It works like
dict.update(), adding new keys and overwriting existing keys.
"""
for k, v in dict.iteritems():
self._set_attr(k, v)
[docs] def get_dict(self):
"""
Return a dict with the parameters
"""
return dict(self.iterattrs())
[docs] def keys(self):
"""
Iterator of valid keys stored in the ParameterData object
"""
for k in self.attrs():
yield k
def add_path(self, *args, **kwargs):
from aiida.common.exceptions import ModificationNotAllowed
raise ModificationNotAllowed("Cannot add files or directories to a ParameterData object")
# def validate(self):
# # There should be nothing specific to check
# super(ParameterData,self).validate()
@property
def dict(self):
"""
To be used to get direct access to the underlying dictionary with the
syntax node.dict.key or node.dict['key'].
:return: an instance of the AttributeResultManager.
"""
from aiida.orm.node import AttributeManager
return AttributeManager(self)