Skip to content

Commit

Permalink
minor: use OrderedDict in get_attr_dict functions to provide more r…
Browse files Browse the repository at this point in the history
…eadable output

WIP

fixes: #274
  • Loading branch information
SylviaDu99 committed Nov 24, 2024
1 parent 731c921 commit ffb84a7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
7 changes: 4 additions & 3 deletions policyengine_core/parameters/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Dict, List, Optional

import numpy

from collections import OrderedDict
from policyengine_core.commons.misc import empty_clone
from policyengine_core.errors import ParameterParsingError
from policyengine_core.periods import INSTANT_PATTERN, period as get_period
Expand Down Expand Up @@ -239,7 +239,7 @@ def relative_change(self, start_instant, end_instant):
return end_value / start_value - 1

def get_attr_dict(self) -> dict:
data = self.__dict__.copy()
data = OrderedDict(self.__dict__.copy())
for attr in self._exclusion_list:
if attr in data.keys():
del data[attr]
Expand All @@ -251,4 +251,5 @@ def get_attr_dict(self) -> dict:
value = float(value)
value_dict[value_at_instant.instant_str] = value
data["values_list"] = value_dict
return data
data.move_to_end("values_list")
return dict(data)
8 changes: 5 additions & 3 deletions policyengine_core/parameters/parameter_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Iterable, Union

import yaml
from collections import OrderedDict

from policyengine_core import commons, parameters, tools
from policyengine_core.periods.instant_ import Instant
Expand Down Expand Up @@ -280,16 +281,17 @@ def get_child(self, path: str) -> "ParameterNode":
return node

def get_attr_dict(self) -> dict:
data = self.__dict__.copy()
data = OrderedDict(self.__dict__.copy())
for attr in self._exclusion_list:
if attr in data.keys():
del data[attr]
if "children" in data.keys():
child_dict = data.get("children")
for child_name, child in child_dict.items():
data[child_name] = child.get_attr_dict()
del data["children"]
return data
data.move_to_end(child_name)
del data["children"]
return dict(data)

class NoAliasDumper(yaml.SafeDumper):
def ignore_aliases(self, data):
Expand Down
7 changes: 4 additions & 3 deletions policyengine_core/parameters/parameter_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import typing
from typing import Any, Iterable

from collections import OrderedDict
from policyengine_core import commons, parameters, tools
from policyengine_core.errors import ParameterParsingError
from policyengine_core.parameters import AtInstantLike, config, helpers
Expand Down Expand Up @@ -174,7 +174,7 @@ def _get_at_instant(self, instant: Instant) -> TaxScaleLike:
return scale

def get_attr_dict(self) -> dict:
data = self.__dict__.copy()
data = OrderedDict(self.__dict__.copy())
for attr in self._exclusion_list:
if attr in data.keys():
del data[attr]
Expand All @@ -185,4 +185,5 @@ def get_attr_dict(self) -> dict:
node_list[i] = node.get_attr_dict()
i += 1
data["brackets"] = node_list
return data
data.move_to_end("brackets")
return dict(data)

0 comments on commit ffb84a7

Please sign in to comment.