Skip to content

Commit

Permalink
support cf.to_dict()
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirroni committed Sep 26, 2023
1 parent 7b7b74e commit 12f8e0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions matpowercaseframes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 12f8e0d

Please sign in to comment.