diff options
author | Théo de la Hogue | 2023-09-04 22:02:17 +0200 |
---|---|---|
committer | Théo de la Hogue | 2023-09-04 22:02:17 +0200 |
commit | 9215d4724912e3eb4a5673ba87cc736abe58be40 (patch) | |
tree | 0a987d4ce4e0465d0754245b520a9560f98d3f58 | |
parent | d3aaac8c0259bd295cd64085f321b9d78eb18ba6 (diff) | |
download | argaze-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.py | 11 |
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.""" |