diff options
author | Théo de la Hogue | 2023-09-13 11:44:12 +0200 |
---|---|---|
committer | Théo de la Hogue | 2023-09-13 11:44:12 +0200 |
commit | dfa02fde90ad01e32bbf20335243e607291fdeef (patch) | |
tree | 19ad4a39acb26ad6a869b4b0c5f57c7126bb84ca | |
parent | d44f851b16e8ca5bc1222dead01c3aeb55c82f72 (diff) | |
download | argaze-dfa02fde90ad01e32bbf20335243e607291fdeef.zip argaze-dfa02fde90ad01e32bbf20335243e607291fdeef.tar.gz argaze-dfa02fde90ad01e32bbf20335243e607291fdeef.tar.bz2 argaze-dfa02fde90ad01e32bbf20335243e607291fdeef.tar.xz |
Fixing as_dict function.
-rw-r--r-- | src/argaze/DataStructures.py | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/argaze/DataStructures.py b/src/argaze/DataStructures.py index e6dfc8b..08a7d2c 100644 --- a/src/argaze/DataStructures.py +++ b/src/argaze/DataStructures.py @@ -8,7 +8,7 @@ __copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)" __license__ = "BSD" from typing import TypeVar, Tuple -from dataclasses import asdict +from inspect import getmembers import collections import json import ast @@ -30,13 +30,20 @@ TimeStampedBufferType = TypeVar('TimeStampedBuffer', bound="TimeStampedBuffer") def as_dict(dataclass_object) -> dict: """ - Get dataclass object attributes values as a dictionary. + Get dataclass object fields's values as a dictionary. Returns: - values: dictionary of dataclass attributes values + values: dictionary of dataclass fields's values """ - return {key: vars(dataclass_object)[key] for key in asdict(dataclass_object).keys()} + # Get data class fields names + fields_names = [] + for member_name, member_value in getmembers(dataclass_object): + if member_name == '__dataclass_fields__': + fields_names = member_value.keys() + + # Copy fields values + return {name: vars(dataclass_object)[name] for name in fields_names} class JsonEncoder(json.JSONEncoder): """Specific ArGaze JSON Encoder.""" |