From 9215d4724912e3eb4a5673ba87cc736abe58be40 Mon Sep 17 00:00:00 2001 From: Théo de la Hogue Date: Mon, 4 Sep 2023 22:02:17 +0200 Subject: Adding as_dict function to return dataclass attributes values as dict. --- src/argaze/DataStructures.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src') 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.""" -- cgit v1.1