Skip to content
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
4 changes: 4 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- bump: minor
changes:
added:
- Easy way to update parameters with uprating behaviour.
14 changes: 11 additions & 3 deletions policyengine_core/parameters/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,22 @@ def clone(self):
]
return clone

def update(self, value=None, period=None, start=None, stop=None):
def update(
self,
value=None,
period=None,
start=None,
stop=None,
remove_after: bool = False,
):
"""
Change the value for a given period.

:param period: Period where the value is modified. If set, `start` and `stop` should be `None`.
:param start: Start of the period. Instance of `policyengine_core.Instant`. If set, `period` should be `None`.
:param stop: Stop of the period. Instance of `policyengine_core.Instant`. If set, `period` should be `None`.
:param value: New value. If `None`, the parameter is removed from the legislation parameters for the given period.
:param remove_after: If `True`, the value given here becomes the last value in the parameter history (uprating will apply on the next interval if only `start` given, or after the period if `period` given).
"""
if period is not None:
if start is not None or stop is not None:
Expand All @@ -166,7 +174,7 @@ def update(self, value=None, period=None, start=None, stop=None):
i = 0

# Future intervals : not affected
if stop_str:
if stop_str and not remove_after:
while (i < n) and (old_values[i].instant_str >= stop_str):
new_values.append(old_values[i])
i += 1
Expand All @@ -175,7 +183,7 @@ def update(self, value=None, period=None, start=None, stop=None):
if stop_str:
if new_values and (stop_str == new_values[-1].instant_str):
pass # such interval is empty
else:
elif not remove_after:
if i < n:
overlapped_value = old_values[i].value
value_name = _compose_name(self.name, item_name=stop_str)
Expand Down
Loading