aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThéo de la Hogue2023-09-04 22:02:17 +0200
committerThéo de la Hogue2023-09-04 22:02:17 +0200
commit9215d4724912e3eb4a5673ba87cc736abe58be40 (patch)
tree0a987d4ce4e0465d0754245b520a9560f98d3f58
parentd3aaac8c0259bd295cd64085f321b9d78eb18ba6 (diff)
downloadargaze-9215d4724912e3eb4a5673ba87cc736abe58be40.zip
argaze-9215d4724912e3eb4a5673ba87cc736abe58be40.tar.gz
argaze-9215d4724912e3eb4a5673ba87cc736abe58be40.tar.bz2
argaze-9215d4724912e3eb4a5673ba87cc736abe58be40.tar.xz
Adding as_dict function to return dataclass attributes values as dict.
-rw-r--r--src/argaze/DataStructures.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/argaze/DataStructures.py b/src/argaze/DataStructures.py
index 74fb8b0..e6dfc8b 100644
--- a/src/argaze/DataStructures.py
+++ b/src/argaze/DataStructures.py
@@ -8,6 +8,7 @@ __copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "BSD"
from typing import TypeVar, Tuple
+from dataclasses import asdict
import collections
import json
import ast
@@ -27,6 +28,16 @@ DataType = TypeVar('Data')
TimeStampedBufferType = TypeVar('TimeStampedBuffer', bound="TimeStampedBuffer")
# Type definition for type annotation convenience
+def as_dict(dataclass_object) -> dict:
+ """
+ Get dataclass object attributes values as a dictionary.
+
+ Returns:
+ values: dictionary of dataclass attributes values
+ """
+
+ return {key: vars(dataclass_object)[key] for key in asdict(dataclass_object).keys()}
+
class JsonEncoder(json.JSONEncoder):
"""Specific ArGaze JSON Encoder."""