-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dataclass): add serialization function (#68)
* feat: add a function to serialize dataclass Signed-off-by: ktro2828 <[email protected]> * refactor: remove unused shortcuts method Signed-off-by: ktro2828 <[email protected]> --------- Signed-off-by: ktro2828 <[email protected]>
- Loading branch information
Showing
11 changed files
with
42 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
# `common` | ||
|
||
<!-- prettier-ignore-start --> | ||
::: t4_devkit.common.converter | ||
|
||
::: t4_devkit.common.geometry | ||
|
||
::: t4_devkit.common.io | ||
|
||
::: t4_devkit.common.serialize | ||
|
||
::: t4_devkit.common.timestamp | ||
<!-- prettier-ignore-end --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
from typing import Any | ||
|
||
import numpy as np | ||
from attrs import asdict, fields, filters | ||
from pyquaternion import Quaternion | ||
|
||
|
||
def serialize_dataclass(data: Any) -> dict[str, Any]: | ||
"""Serialize attrs' dataclasses into dict. | ||
Note that all fields specified with `init=False` will be skipped to be serialized. | ||
Args: | ||
data (Any): Dataclass object. | ||
Returns: | ||
dict[str, Any]: Serialized dict. | ||
""" | ||
excludes = filters.exclude(*[a for a in fields(data.__class__) if not a.init]) | ||
return asdict(data, filter=excludes, value_serializer=_value_serializer) | ||
|
||
|
||
def _value_serializer(data: Any, attribute: Any, value: Any) -> Any: | ||
if isinstance(value, np.ndarray): | ||
return value.tolist() | ||
elif isinstance(value, Quaternion): | ||
return value.q.tolist() | ||
elif isinstance(value, tuple): | ||
return list(value) | ||
elif isinstance(value, Enum): | ||
return value.value | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters