functions module
Module containing functions that binary_c-python uses to modify dictionaries.
- class binarycpython.utils.dicts.AutoVivificationDict[source]
Bases:
dict
Implementation of perl’s autovivification feature, by overriding the get item and the __iadd__ operator (https://docs.python.org/3/reference/datamodel.html?highlight=iadd#object.__iadd__)
This allows to set values within a subdict that might not exist yet:
Example
newdict = {} newdict[‘example’][‘mass’] += 10 print(newdict) >>> {‘example’: {‘mass’: 10}}
- binarycpython.utils.dicts.count_keys_recursive(input_dict)[source]
Function to count the total number of keys in a dictionary
- binarycpython.utils.dicts.custom_sort_dict(input_dict)[source]
Returns a dictionary that is ordered, but can handle numbers better than normal OrderedDict
When the keys of the current dictionary are of mixed type, we first find all the unique types. Sort that list of type names. Then find the values that fit that type. Sort those and append them to the sorted keys list. This is done until all the keys are sorted.
All objects other than dictionary types are directly return as they are
- binarycpython.utils.dicts.filter_dict(arg_dict, filter_list)[source]
Function to filter out keys that are contains in filter_list
- Parameters
arg_dict (
dict
) – dictionary containing the argument + default key pairs of binary_cfilter_list (
list
) – lists of keys to be filtered out
- Return type
dict
- Returns
filtered dictionary
- binarycpython.utils.dicts.filter_dict_through_values(arg_dict, filter_list)[source]
Function to filter out keys that contain values included in filter_list
- Parameters
arg_dict (
dict
) – dictionary containing the argument + default key pairs of binary_cfilter_list (
list
) – lists of values to be filtered out
- Return type
dict
- Returns
filtered dictionary
- binarycpython.utils.dicts.inspect_dict(input_dict, indent=0, print_structure=True)[source]
Function to (recursively) inspect a (nested) dictionary. The object that is returned is a dictionary containing the key of the input_dict, but as value it will return the type of what the value would be in the input_dict
In this way we inspect the structure of these dictionaries, rather than the exact contents.
- Parameters
input_dict (
dict
) – dictionary you want to inspectprint_structure (
bool
) – (optional, default = True)indent (
int
) – (optional, default = 0) indent of the first output
- Return type
dict
- Returns
- Dictionary that has the same structure as the input_dict, but as values it has the
type(input_dict[key]) (except if the value is a dict)
- binarycpython.utils.dicts.keys_to_floats(json_data)[source]
Function to convert all the keys of the dictionary to float to float
we need to convert keys to floats: this is ~ a factor 10 faster than David’s recursive_change_key_to_float routine, probably because this version only does the float conversion, nothing else.
- binarycpython.utils.dicts.merge_dicts(dict_1, dict_2)[source]
Function to merge two dictionaries in a custom way.
Behaviour:
- When dict keys are only present in one of either:
we just add the content to the new dict
- When dict keys are present in both, we decide based on the value types how to combine them:
dictionaries will be merged by calling recursively calling this function again
numbers will be added
(opt) lists will be appended
booleans are merged with logical OR
identical strings are just set to the string
non-identical strings are concatenated
NoneTypes are set to None
In the case that the instances do not match: for now I will raise an error
- Parameters
dict_1 (
dict
) – first dictionarydict_2 (
dict
) – second dictionary
- Return type
dict
- Returns
Merged dictionary
- binarycpython.utils.dicts.multiply_float_values(d, const, ignore=None)[source]
multiply_float_values : A function to recursively multiply values of a (nested) dictionary that are floats by a constant. Nested dictionaries call this function recursively.
- Parameters
dictionary (d = the) –
values (const = the constant that multiplies float) –
- binarycpython.utils.dicts.multiply_values_dict(input_dict, factor)[source]
Function that goes over dictionary recursively and multiplies the value if possible by a factor
If the key equals “general_info”, the multiplication gets skipped
- binarycpython.utils.dicts.prepare_dict(global_dict, list_of_sub_keys)[source]
Function that makes sure that the global dict is prepared to have a value set there. This dictionary will store values and factors for the distribution functions, so that they don’t have to be calculated each time.
- Parameters
global_dict (
dict
) – globally accessible dictionary where factors are stored inlist_of_sub_keys (
list
) – List of keys that must become be(come) present in the global_dict
- Return type
None
- binarycpython.utils.dicts.recursive_change_key_to_float(input_dict)[source]
Function to recursively change the key to float
This only works if the dict contains just sub-dicts or numbers/strings. Does not work with lists as values
- binarycpython.utils.dicts.recursive_change_key_to_string(input_dict, custom_format='{:g}')[source]
Function to recursively change the key back to a string but this time in a format that we decide
- binarycpython.utils.dicts.set_opts(opts, newopts)[source]
Function to take a default dict and override it with newer values.
# TODO: consider changing this to just a dict.update
- Parameters
opts (
dict
) – dictionary with default valuesnewopts (
dict
) – dictionary with new values
- Return type
dict
- Returns
returns an updated dictionary
- binarycpython.utils.dicts.subtract_dicts(dict_1, dict_2)[source]
Function to subtract two dictionaries.
Only allows values to be either a dict or a numerical type
- For the overlapping keys (key name present in both dicts):
- When the keys are of the same type:
If the types are of numerical type: subtract the value at dict 2 from dict 1.
If the types are both dictionaries: call this function with the subdicts
- When the keys are not of the same type:
if the keys are all of numerical types
- For the unique keys:
if the key is from dict 1: adds the value to the new dict (be it numerical value or dict)
- If the key is from dict 2: Adds the negative of its value in case of numerical type.
if the type is a dict, the result of subtract_dicts({}, dict_2[key]) will be set
If the result is 0, the key will be removed from the resulting dict. If that results in an empty dict, the dict will be removed too.
- Parameters
dict_1 (
dict
) – first dictionarydict_2 (
dict
) – second dictionary
- Return type
dict
- Returns
Subtracted dictionary
- binarycpython.utils.dicts.update_dicts(dict_1, dict_2)[source]
Function to update dict_1 with values of dict_2 in a recursive way.
Behaviour:
- When dict keys are only present in one of either:
we just add the content to the new dict
- When dict keys are present in both, we decide based on the value types how to combine them:
value of dict2 will be taken
- Parameters
dict_1 (
dict
) – first dictionarydict_2 (
dict
) – second dictionary
- Return type
dict
- Returns
New dictionary with Updated values