Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Daniel Boeckenhoff
tfields
Commits
956ca8e0
Commit
956ca8e0
authored
May 18, 2020
by
dboe
Browse files
legacy test works
parent
d2080189
Changes
2
Hide whitespace changes
Inline
Side-by-side
test/test_core.py
View file @
956ca8e0
...
...
@@ -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
...
...
tfields/core.py
View file @
956ca8e0
...
...
@@ -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
):
'''
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment