Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F7124677
D6319.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
D6319.diff
View Options
diff --git a/swh/model/model.py b/swh/model/model.py
--- a/swh/model/model.py
+++ b/swh/model/model.py
@@ -18,6 +18,7 @@
from .collections import ImmutableDict
from .hashutil import DEFAULT_ALGORITHMS, MultiHash, hash_to_bytes
from .identifiers import (
+ _BaseSWHID,
directory_identifier,
extid_identifier,
normalize_timestamp,
@@ -66,16 +67,19 @@
def dictify(value):
"Helper function used by BaseModel.to_dict()"
- if isinstance(value, BaseModel):
+ if isinstance(value, (int, float, str, bytes)):
+ # short-path for common objects
+ return value
+ elif hasattr(value, "to_dict"): # model object
return value.to_dict()
- elif isinstance(value, (CoreSWHID, ExtendedSWHID)):
- return str(value)
+ elif hasattr(value, "items"): # dict
+ return {k: dictify(v) for k, v in value.items()}
+ elif hasattr(value, "__iter__"): # tuple
+ return tuple(map(dictify, value))
elif isinstance(value, Enum):
return value.value
- elif isinstance(value, (dict, ImmutableDict)):
- return {k: dictify(v) for k, v in value.items()}
- elif isinstance(value, tuple):
- return tuple(dictify(v) for v in value)
+ elif isinstance(value, _BaseSWHID):
+ return str(value)
else:
return value
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Dec 21 2024, 5:03 PM (11 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3227563
Attached To
D6319: model: optimize dictify()
Event Timeline
Log In to Comment