Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example using loadcase #6

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Octave
octave-workspace

# Local
tests/results/*.xlsx

Expand Down
45 changes: 35 additions & 10 deletions matpowercaseframes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,11 @@ def __init__(self, data, update_index=True):
TypeError: Error input data invalid.
"""
# TODO: support read excel
# TODO: support Path object
if isinstance(data, str):
# TYPE: str of path | str of matpower case name
if not os.path.isfile(data):
# TYPE: str of matpower case name
if MATPOWER_EXIST:
data_ = os.path.join(matpower.path_matpower, f"data/{data}")
if os.path.isfile(data_):
data = data_
else:
raise FileNotFoundError
path = self._get_path(data)
# TYPE: str of path
self._read_matpower(filepath=data)
self._read_matpower(filepath=path)
elif isinstance(data, dict):
# TYPE: dict | oct2py.io.Struct
self._read_oct2py_struct(struct=data)
Expand All @@ -64,6 +57,30 @@ def __init__(self, data, update_index=True):
if update_index:
self._update_index()

@staticmethod
def _get_path(path):
# TYPE: str of path | str of matpower case name
if os.path.isfile(path):
return path

path_added_m = path + '.m'
if os.path.isfile(path_added_m):
return path_added_m

# TYPE: str of matpower case name
if MATPOWER_EXIST:
path_added_matpower = os.path.join(matpower.path_matpower, f"data/{path}")
if os.path.isfile(path_added_matpower):
return path_added_matpower

path_added_matpower_m = os.path.join(
matpower.path_matpower, f"data/{path_added_m}"
)
if os.path.isfile(path_added_matpower_m):
return path_added_matpower_m

raise FileNotFoundError

def _read_matpower(self, filepath):
# ! Old attribute is not guaranted to be replaced in re-read
with open(filepath) as f:
Expand Down Expand Up @@ -259,3 +276,11 @@ def to_dict(self):
else:
data[attribute] = getattr(self, attribute).values.tolist()
return data

def to_mpc(self):
"""
Convert CaseFrames into matpower-compatible data, that is dictionary

The value of the data will be in str, numeric, and list.
"""
return self.to_dict()
Loading
Loading