src.policies.policy_tools
¶
Tools to work with policy dictionaries without accidentally modifying them.
Module Contents¶
Functions¶
|
Filter a dictionary by conditions on keys or values. |
|
Shorten policies to only go from start_date to end_date. |
|
Create a copy of dictionary and update it with other. |
|
Combine a list of non-overlapping dictionaries into one. |
|
Split a policy dictionary and reduce it to start and end dates. |
|
|
|
Return reduced policy dicts where the work policies have been removed. |
|
Return reduced policy dicts where the educ policies have been removed. |
|
Return reduced policy dicts where the other policies have been removed. |
|
Return reduced policy dicts where the school policies have been removed. |
|
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
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
- 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
- update_dictionary(dictionary, other)[source]¶
Create a copy of dictionary and update it with other.
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
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
policies (dict) –
split_date (pandas.Timestamp or str) – The start date of the second dictionary.
start_date (pandas.Timestamp or str) – The start date of the first dictionary.
end_date (pandas.Timestamp or str) – The end date of the second dictionary.
- Returns
Tuple with two non-overlapping policy dictionaries.
- Return type
- 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.