From f9860b92022270971ea82bb26dd013267218010f Mon Sep 17 00:00:00 2001 From: Muhammad Yasirroni Date: Mon, 29 Jun 2026 18:54:18 +1000 Subject: [PATCH 1/2] minor doc string to csv Path --- matpowercaseframes/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matpowercaseframes/core.py b/matpowercaseframes/core.py index 9fe7726..97bff52 100644 --- a/matpowercaseframes/core.py +++ b/matpowercaseframes/core.py @@ -1300,7 +1300,7 @@ def to_csv(self, path, prefix="", suffix="", attributes=None): Args: - path (str): + path (str | os.PathLike): Directory path where the CSV files will be saved. prefix (str): File prefix for each attribute CSV file. From 827f89bbabbc5c7817335d8eb8c45165289b632d Mon Sep 17 00:00:00 2001 From: Muhammad Yasirroni Date: Mon, 29 Jun 2026 19:20:51 +1000 Subject: [PATCH 2/2] support attribute to to_csv --- matpowercaseframes/core.py | 39 +++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/matpowercaseframes/core.py b/matpowercaseframes/core.py index 97bff52..2773c04 100644 --- a/matpowercaseframes/core.py +++ b/matpowercaseframes/core.py @@ -1306,21 +1306,37 @@ def to_csv(self, path, prefix="", suffix="", attributes=None): File prefix for each attribute CSV file. suffix (str): File suffix for each attribute CSV file. - attributes (list | None): - Specific attributes to save (unused parameter). + attributes (list | str | None): + Specific attributes to save. If None, all attributes are saved. """ # make dir os.makedirs(path, exist_ok=True) - data = {"INFO": {}} - for attribute in ATTRIBUTES_INFO: - if attribute in self._attributes: - data["INFO"][attribute] = getattr(self, attribute, None) - pd.DataFrame(data=data).to_csv(os.path.join(path, f"{prefix}info{suffix}.csv")) + if attributes is None: + attributes_to_save = list(self._attributes) + elif isinstance(attributes, str): + attributes_to_save = [attributes] + else: + attributes_to_save = list(attributes) + + attributes_to_save = list(dict.fromkeys(attributes_to_save)) + + missing_attributes = [ + attribute + for attribute in attributes_to_save + if attribute not in self._attributes + ] + if missing_attributes: + msg = ( + f"Attributes not found: {missing_attributes}. " + f"Available attributes: {self._attributes}." + ) + raise AttributeError(msg) - for attribute in self._attributes: + data = {"INFO": {}} + for attribute in attributes_to_save: if attribute in ATTRIBUTES_INFO: - continue + data["INFO"][attribute] = getattr(self, attribute, None) elif attribute in ATTRIBUTES_NAME: pd.DataFrame(data={attribute: getattr(self, attribute)}).to_csv( os.path.join(path, f"{prefix}{attribute}{suffix}.csv") @@ -1330,6 +1346,11 @@ def to_csv(self, path, prefix="", suffix="", attributes=None): os.path.join(path, f"{prefix}{attribute}{suffix}.csv") ) + if data["INFO"]: + pd.DataFrame(data=data).to_csv( + os.path.join(path, f"{prefix}info{suffix}.csv") + ) + def to_dict(self): """ Convert the CaseFrames data into a dictionary.