From 12f8e0d3d06734d36c70dc557c75c2e820603fc4 Mon Sep 17 00:00:00 2001 From: Muhammad Yasirroni Date: Tue, 26 Sep 2023 16:14:22 +0700 Subject: [PATCH] support cf.to_dict() --- matpowercaseframes/core.py | 17 +++++++++++++++++ tests/test_core.py | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/matpowercaseframes/core.py b/matpowercaseframes/core.py index 6272f26..a5c74b5 100644 --- a/matpowercaseframes/core.py +++ b/matpowercaseframes/core.py @@ -242,3 +242,20 @@ def to_csv(self, path): }).to_csv(os.path.join(path, f"{attribute}.csv")) else: getattr(self, attribute).to_csv(os.path.join(path, f"{attribute}.csv")) + + def to_dict(self): + """ + Convert CaseFrames into dictionary + + The value of the data will be in str, numeric, and list. + """ + data = { + 'version': getattr(self, 'version', None), + 'baseMVA': getattr(self, 'baseMVA', None), + } + for attribute in self._attributes: + if attribute == "version" or attribute == "baseMVA": + data[attribute] = getattr(self, attribute) + else: + data[attribute] = getattr(self, attribute).values.tolist() + return data diff --git a/tests/test_core.py b/tests/test_core.py index abb3ce0..a0b3ee8 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -99,3 +99,7 @@ def test_to_xlsx(): def test_to_csv(): cf = CaseFrames(CASE_PATH) cf.to_csv('tests/results') + +def test_to_dict(): + cf = CaseFrames(CASE_PATH) + dict_object = cf.to_dict()