Commit fe0b5b62 authored by payno's avatar payno

[Dim] add to_dict and from_dict functions to cast from/to a dictionary

parent bf9bbb2e
......@@ -483,3 +483,38 @@ class Dim(object):
def __str__(self):
return " ".join((str(self.kind), str(self.name), 'size:', str(self.size)))
def to_dict(self):
"""translate the current Dim to a dictionary"""
return {
'name': self.name,
'kind': self.kind,
'size': self.size,
'relative_prev_val': self.relative_prev_val,
}
@staticmethod
def from_dict(_dict):
"""
:param dict _dict: dict defining the dimension. Should contains the
following keys: name, kind, size, relative_prev_val
unique values are not stored into it because it
depends on the metadata and should be obtain from a
fit / set_dims
:return: Dim corresponding to the dict given
:rtype: :class:`Dim`
"""
assert type(_dict) is dict
missing_keys = []
for _key in ('name', 'kind', 'size', 'relative_prev_val'):
if _key not in _dict:
missing_keys.append(missing_keys)
if len(missing_keys) > 0:
raise ValueError('There is some missing keys (%s), unable to create'
'a valid Dim')
else:
return Dim(name=_dict['name'],
kind=_dict['kind'],
size=_dict['size'],
relative_prev_val=_dict['relative_prev_val'])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment