Commit 956ca8e0 authored by dboe's avatar dboe
Browse files

legacy test works

parent d2080189
......@@ -49,12 +49,9 @@ class Base_Check(object):
self.demand_equal(load_inst)
def test_dict(self):
d = self._inst._as_new_dict()
other = type(self._inst)._from_new_dict(d)
d = self._inst._as_dict()
other = type(self._inst)._from_dict(d)
self.demand_equal(other)
# d = self._inst._as_dict()
# other = type(self._inst)._from_dict(**d)
# self.demand_equal(other)
def tearDown(self):
del self._inst
......
......@@ -152,7 +152,7 @@ class AbstractObject(object):
[42]
"""
content_dict = self._as_new_dict()
content_dict = self._as_dict()
np.savez(path, **content_dict)
@classmethod
......@@ -165,7 +165,7 @@ class AbstractObject(object):
# wheter we could avoid pickling (potential security issue)
load_kwargs.setdefault('allow_pickle', True)
np_file = np.load(path, **load_kwargs)
return cls._from_new_dict(dict(np_file))
return cls._from_dict(dict(np_file))
def _args(self) -> tuple:
return tuple()
......@@ -175,7 +175,7 @@ class AbstractObject(object):
_HIERARCHY_SEPARATOR = '::'
def _as_new_dict(self):
def _as_dict(self):
d = {}
# type
......@@ -188,8 +188,8 @@ class AbstractObject(object):
('kwargs', self._kwargs().items())]:
for attr, value in iterable:
attr = base_attr + self._HIERARCHY_SEPARATOR + attr
if hasattr(value, '_as_new_dict'):
part_dict = value._as_new_dict()
if hasattr(value, '_as_dict'):
part_dict = value._as_dict()
for part_attr, part_value in part_dict.items():
d[
attr + self._HIERARCHY_SEPARATOR + part_attr
......@@ -199,8 +199,12 @@ class AbstractObject(object):
return d
@classmethod
def _from_new_dict(cls, d: dict):
d.pop('type')
def _from_dict(cls, d: dict):
try:
d.pop('type')
except KeyError:
# legacy
return cls._from_dict_legacy(**d)
here = {}
for string in d: # TOO no sortelist
......@@ -228,7 +232,7 @@ class AbstractObject(object):
# Found under nt. ???
obj_type = obj_type.decode("UTF-8")
obj_type = getattr(tfields, obj_type)
attr_value = obj_type._from_new_dict(here[attr][key])
attr_value = obj_type._from_dict(here[attr][key])
else: # if len(here[attr][key]) == 1:
attr_value = here[attr][key].pop('')
here[attr][key] = attr_value
......@@ -243,37 +247,11 @@ class AbstractObject(object):
obj = cls(*args, **kwargs)
return obj
def _as_dict(self):
"""
Recursively walk trough all __slots__ and describe all elements
"""
d = {}
d["bulk"] = self.bulk
d["bulk_type"] = self.__class__.__name__
for attr in self._iter_slots():
value = getattr(self, attr)
if hasattr(value, '_as_dict'):
value = value._as_dict()
elif isinstance(value, (list)): # is_iterable
if len(value) == 0:
d[attr] = None
elif hasattr(value[0], '_as_dict'):
for i, part in enumerate(value):
part_dict = part._as_dict()
for part_attr, part_value in part_dict.items():
d[
"{attr}::{i}::{part_attr}".format(**locals())
] = part_value
continue
d[attr] = value
return d
@classmethod
def _from_dict(cls, **d):
def _from_dict_legacy(cls, **d):
"""
legacy method - Opposite of old _as_dict method which is removed in
favour of nested object saving under 'data'
legacy method of _from_dict - Opposite of old _as_dict method
which is overridden in this version
"""
list_dict = {}
kwargs = {}
......@@ -282,7 +260,6 @@ class AbstractObject(object):
"""
for key in sorted(list(d)):
if "::" in key:
splits = key.split("::")
attr, _, end = key.partition("::")
if attr not in list_dict:
list_dict[attr] = {}
......@@ -305,13 +282,13 @@ class AbstractObject(object):
list_dict[key] = []
for index in sorted(list(sub_dict)):
bulk_type = sub_dict[index].get("bulk_type")
# bulk_type = bulk_type.tolist() was necessary before. no clue
bulk_type = bulk_type.tolist()
if isinstance(bulk_type, bytes):
# asthonishingly, this is not necessary under linux.
# Found under nt. ???
bulk_type = bulk_type.decode("UTF-8")
bulk_type = getattr(tfields, bulk_type)
list_dict[key].append(bulk_type._from_dict(**sub_dict[index]))
list_dict[key].append(bulk_type._from_dict_legacy(**sub_dict[index]))
with cls._bypass_setters('fields', demand_existence=False):
'''
......
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