Skip to content
Snippets Groups Projects
Commit 0a6d7e05 authored by David Douard's avatar David Douard
Browse files

Extract the dictify() function from BaseModel.to_dict()

this function does not need to be a local function of the to_dict namespace.
parent a5a9f57c
No related branches found
No related tags found
No related merge requests found
......@@ -32,6 +32,20 @@ SHA1_SIZE = 20
Sha1Git = bytes
def dictify(value):
"Helper function used by BaseModel.to_dict()"
if isinstance(value, BaseModel):
return value.to_dict()
elif isinstance(value, Enum):
return value.value
elif isinstance(value, dict):
return {k: dictify(v) for k, v in value.items()}
elif isinstance(value, list):
return [dictify(v) for v in value]
else:
return value
class BaseModel:
"""Base class for SWH model classes.
......@@ -41,21 +55,7 @@ class BaseModel:
def to_dict(self):
"""Wrapper of `attr.asdict` that can be overridden by subclasses
that have special handling of some of the fields."""
def dictify(value):
if isinstance(value, BaseModel):
return value.to_dict()
elif isinstance(value, Enum):
return value.value
elif isinstance(value, dict):
return {k: dictify(v) for k, v in value.items()}
elif isinstance(value, list):
return [dictify(v) for v in value]
else:
return value
ret = attr.asdict(self, recurse=False)
return dictify(ret)
return dictify(attr.asdict(self, recurse=False))
@classmethod
def from_dict(cls, d):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment