src.policies.policy_tools

Tools to work with policy dictionaries without accidentally modifying them.

Module Contents

Functions

filter_dictionary(function, dictionary, by='keys')

Filter a dictionary by conditions on keys or values.

shorten_policies(policies, start_date=None, end_date=None)

Shorten policies to only go from start_date to end_date.

update_dictionary(dictionary, other)

Create a copy of dictionary and update it with other.

combine_dictionaries(dictionaries)

Combine a list of non-overlapping dictionaries into one.

split_policies(policies, split_date, start_date=None, end_date=None, suffixes=('first', 'second'))

Split a policy dictionary and reduce it to start and end dates.

_rename_duplicates(policies, duplicates, suffix)

remove_work_policies(policies)

Return reduced policy dicts where the work policies have been removed.

remove_educ_policies(policies)

Return reduced policy dicts where the educ policies have been removed.

remove_other_policies(policies)

Return reduced policy dicts where the other policies have been removed.

remove_school_policies(policies)

Return reduced policy dicts where the school policies have been removed.

remove_young_educ_policies(policies)

Return reduced policy dicts where the young educ policies have been removed.

filter_dictionary(function, dictionary, by='keys')[source]

Filter a dictionary by conditions on keys or values.

Parameters
  • function (callable) – Function that takes one argument and returns True or False.

  • dictionary (dict) – Dictionary to be filtered.

Returns

Filtered dictionary

Return type

dict

Examples

>>> filter_dictionary(lambda x: "bla" in x, {"bla": 1, "blubb": 2})
{'bla': 1}
>>> filter_dictionary(lambda x: x <= 1, {"bla": 1, "blubb": 2}, by="values")
{'bla': 1}
shorten_policies(policies, start_date=None, end_date=None)[source]

Shorten policies to only go from start_date to end_date.

Parameters
  • policies (dict) – policies dictionary with “start” and “end” as keys.

  • start_date (pd.Timestamp or str) –

  • end_date (pd.Timestamp or str) –

Returns

reduced policies only including policies that are active between

start_date and end_date. Kept policies are cropped to not start before start_date and to not end after end_date.

Return type

dict

update_dictionary(dictionary, other)[source]

Create a copy of dictionary and update it with other.

Parameters
  • dictionary (dict) –

  • other (dict) –

Returns

The updated dictionary

Return type

dict

Examples

# make sure input is not changed >>> first = {“a”: 1, “b”: 2} >>> updated = update_dictionary(first, {“c”: 3}) >>> assert first == {“a”: 1, “b”: 2}

# make sure update works >>> update_dictionary({“a”: 1, “b”: 2}, {“c”: 3}) {‘a’: 1, ‘b’: 2, ‘c’: 3}

combine_dictionaries(dictionaries)[source]

Combine a list of non-overlapping dictionaries into one.

Parameters

dictionaries (list) – List of dictionaries.

Returns

The combined dictionary.

Return type

dict

Examples

>>> combine_dictionaries([{"a": 1}, {"b": 2}])
{'a': 1, 'b': 2}
split_policies(policies, split_date, start_date=None, end_date=None, suffixes=('first', 'second'))[source]

Split a policy dictionary and reduce it to start and end dates.

The split date is included in the second dictionary. To make it possible that split dictionaries can be combined again to obtain a policy dictionary that is equivalent to the original one, we add suffixes to all keys that occur in both resulting dictionaries.

Parameters
Returns

Tuple with two non-overlapping policy dictionaries.

Return type

tuple

_rename_duplicates(policies, duplicates, suffix)[source]
remove_work_policies(policies)[source]

Return reduced policy dicts where the work policies have been removed.

remove_educ_policies(policies)[source]

Return reduced policy dicts where the educ policies have been removed.

remove_other_policies(policies)[source]

Return reduced policy dicts where the other policies have been removed.

remove_school_policies(policies)[source]

Return reduced policy dicts where the school policies have been removed.

remove_young_educ_policies(policies)[source]

Return reduced policy dicts where the young educ policies have been removed.