From fe5bf0d352bd56df8f92c829e1823eba514f8e10 Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Wed, 27 Dec 2023 13:20:12 +0000 Subject: [PATCH] Make API to reform functionality more editable --- changelog_entry.yaml | 4 +++ policyengine_core/reforms/reform.py | 43 +++++++++++++++++------------ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29bb..8fa5dc0da 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -0,0 +1,4 @@ +- bump: patch + changes: + fixed: + - API to reform functionality made more editable. diff --git a/policyengine_core/reforms/reform.py b/policyengine_core/reforms/reform.py index 28a77fa03..3b31cfe00 100644 --- a/policyengine_core/reforms/reform.py +++ b/policyengine_core/reforms/reform.py @@ -5,7 +5,11 @@ from policyengine_core.parameters import ParameterNode, Parameter from policyengine_core.taxbenefitsystems import TaxBenefitSystem -from policyengine_core.periods import period as period_, instant as instant_ +from policyengine_core.periods import ( + period as period_, + instant as instant_, + Period, +) import requests @@ -163,22 +167,27 @@ def from_api( parameter_values = data.get("result", {}).get("policy_json", {}) - class reform(Reform): - def apply(self): - for parameter, schedule in parameter_values.items(): - for time_period_string, value in schedule.items(): - start, end = time_period_string.split(".") - self.modify_parameters( - set_parameter( - path=parameter, - start=start, - stop=end, - value=value, - return_modifier=True, - ) - ) - - return reform + for path in parameter_values: + keys_to_remove = [] + for start_stop_str in list(parameter_values[path].keys()): + start, stop = start_stop_str.split(".") + time_period = str( + period_("year:2000:100").intersection( + instant_(start), instant_(stop) + ) + ) + parameter_values[path][time_period] = parameter_values[path][ + start_stop_str + ] + keys_to_remove.append(start_stop_str) + for key in keys_to_remove: + del parameter_values[path][key] + + return Reform.from_dict( + parameter_values, + country_id, + data.get("result", {}).get("label", None), + ) @classproperty def api_id(self):