Skip to content

Commit

Permalink
feat: add function in AtInstantLike to return desired attributes in d…
Browse files Browse the repository at this point in the history
…ictionary form

WIP

fixes: PolicyEngine#274
  • Loading branch information
SylviaDu99 committed Oct 28, 2024
1 parent ab9a573 commit 4fb27f8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
29 changes: 29 additions & 0 deletions policyengine_core/parameters/at_instant_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,32 @@ def get_at_instant(self, instant: Instant) -> Any:

@abc.abstractmethod
def _get_at_instant(self, instant): ...

def get_attr_dict(self) -> dict:
attr_dict = {}
attr_list = [
"name",
"description",
"documentation",
"file_path",
"metadata",
"trace",
"tracer",
"branch_name",
"modified",
"values_list",
]
for attr in attr_list:
if hasattr(self, attr):
attr_dict[attr] = getattr(self, attr)
if hasattr(self, "children"):
for child_name, child in self.children.items():
attr_dict[child_name] = child.get_attr_dict()
if hasattr(self, "values_list"):
value_dict = {}
attr_dict["values_list"] = value_dict
for value_at_instant in self.values_list:
value_dict[value_at_instant.instant_str] = (
value_at_instant.value
)
return attr_dict
12 changes: 2 additions & 10 deletions policyengine_core/parameters/parameter_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,9 @@ def get_child(self, path: str) -> "ParameterNode":
return node

def write_yaml(self, file_path: Path) -> yaml:
data = {"name": self.name, "description": self.description}
for key, value in self.children.items():
name = key
value_dict = {}
data[name] = {"values": value_dict}
for value_at_instant in value.values_list:
value_dict[value_at_instant.instant_str] = (
value_at_instant.value
)
data = self.get_attr_dict()
try:
with open(file_path, "w") as f:
yaml.dump(data, f, sort_keys=False)
yaml.dump(data, f, sort_keys=True)
except Exception as e:
print(f"Error when writing YAML file: {e}")
10 changes: 0 additions & 10 deletions tests/core/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,18 @@ def test_write_yaml():
"2015-01-01": {"value": 550},
"2016-01-01": {"value": 600},
},
"branch_name": "default",
"description": "The amount of the basic income",
"documentation": None,
"file_path": "test_path/to/file/1",
# "metadata": Unclear yet what form this dict takes
"modified": False,
"trace": True,
"tracer": None,
},
"min_age": {
"values": {
"2015-01-01": {"value": 25},
"2016-01-01": {"value": 18},
},
"branch_name": "labor_supply_1",
"description": "The minimum age to receive the basic income",
"documentation": None,
"file_path": "test_path/to/file/2",
# "metadata": Unclear yet what form this dict takes
"modified": True,
"trace": False,
"tracer": None,
},
}
parameter = ParameterNode("root", data=parameter_data)
Expand Down

0 comments on commit 4fb27f8

Please sign in to comment.