Various dictionary functionalities with dicter.
Name : dicter.py Author : E.Taskesen Contact : erdogant@gmail.com github : https://github.com/erdogant/XXX Licence : See licences
- dicter.dicter.check_logger(verbose: [<class 'str'>, <class 'int'>] = 'info')
Check the logger.
- dicter.dicter.clean_filename(name)
Clean filename.
- dicter.dicter.compare(d1: dict, d2: dict)
Compare two dictionaries.
The second dictionary is compared with the first one, and results are shown accordingly.
- Parameters:
d1 (dict) – Dictionary.
d2 (dict) – Dictionary.
- Returns:
added (list) – added keys in d1 compared with d2.
removed (list) – Removed keys in d1 compared with d2.
modified (list) – Modified keys in d1 compared with d2.
same (list) – Similar keys in d1 compared with d2.
Examples
>>> # Import dicter >>> import dicter as dt >>> # >>> Example: Add >>> d1 = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 'new in d2'} >>> d2 = {'level_a': 1, 'level_b': {'a': 'hello world'}} >>> out = dt.compare(d1, d2) >>> print(out) >>> # >>> Example: Remove >>> d1 = {'level_a': 1, 'level_b': {'a': 'hello world'}} >>> d2 = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 'new in d2'} >>> out = dt.compare(d1, d2) >>> print(out) >>> # >>> Example: Modified >>> d1 = {'level_a': 1, 'level_b': {'a': 'hello world'}} >>> d2 = {'level_a': 1, 'level_b': {'a': 'modified'}} >>> out = dt.compare(d1, d2) >>> print(out['modified']) >
- dicter.dicter.depth(d: dict)
Depth of dictionary.
- Parameters:
d (dict) – Input dictionary.
- Returns:
depth – Depth of dictionary.
- Return type:
int
Examples
>>> # Import dicter >>> import dicter as dt >>> # >>> Example dict >>> d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2} >>> n = dt.depth(d)
- dicter.dicter.disable_tqdm()
Set the logger for verbosity messages.
- dicter.dicter.flatten(d: dict)
Flatten dictionary to the deepest level and return key-value.
- Parameters:
d (dict) – Input dictionary.
- Returns:
d_flatten – Flattened dictionary containing key and value.
- Return type:
list
Examples
>>> # Import dicter >>> import dicter as dt >>> # >>> # Example dict >>> d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2} >>> # Flatten dictionary >>> dflat = dt.flatten(d) >>> # >>> print(d_flat)
- dicter.dicter.get_nested(d: dict, path: list)
Get nested value from dictionary.
- Parameters:
d (dict) – Input dictionary.
path (list) – The nested path of keys in a ordered list. Example: [‘level 1’, ‘level 2’]
- Returns:
value –
value belonging to the key.
None: if the path does not exists.
- Return type:
string or numerical
Examples
>>> # Import dicter >>> import dicter as dt >>> # >>> # Example dictionary >>> d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2} >>> # Get the value for the nested path for: >>> value = dt.get_nested(d, ["level_a"]) >>> print(value) # 1 >>> # >>> # Get the value for the nested path for: >>> value = dt.get_nested(d, ["level_b","a"]) >>> print(value) # hello world >>> # >>> # Get the value for the nested path for: >>> value = dt.get_nested(d, ["level_d","c", "e"]) >>> print(value) # 10 >>> # >>> # Get the value for the nested path for a list: >>> l = [[[1,2,3],[10,20,30]]] >>> value = dt.get_nested(l, [0,1,2]) # =>>> 30 >>> print(value) # 30
- dicter.dicter.is_key(d: dict, path: list) bool
Check whether the dictionary contains the key.
- Parameters:
d (dict) – Input dictionary.
path (list) – The nested path of keys in a ordered list. Example: [‘level 1’, ‘level 2’]
- Returns:
True: Key exists. False: Key does not exist.
- Return type:
Bool
Examples
>>> # Import dicter >>> import dicter as dt >>> # >>> # Example dictionary >>> d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2} >>> # >>> # Get the value for the nested path for: >>> value = dt.is_key(d, ["level_b","a"])
- dicter.dicter.load(filepath: str, verbose: [<class 'str'>, <class 'int'>] = 'info')
Load dictionary from json file.
- Parameters:
filepath (str) – path location to json.
verbose (str, optional) –
- Set the verbose messages using string or integer values.
[0, None, ‘silent’, ‘off’, ‘no’]: No message.
[10, ‘debug’]: Messages from debug level and higher.
[20, ‘info’]: Messages from info level and higher.
[30, ‘warning’]: Messages from warning level and higher.
[40, ‘critical’]: Messages from critical level and higher.
- Returns:
dictionary – None or Dictionary.
- Return type:
dict
Examples
>>> # Import dicter >>> import dicter as dt >>> # >>> d = {'level_a': None, 'level_b': {'a': 'hello world'}, 'level_c': True, 'level_d': 2.3, 'level_e': [[1,2,3], [1,2]]} >>> filepath='c:/temp/test/dicter_save.json' >>> # First save >>> dt.save(d, filepath=filepath, overwrite=True) >>> # Load >>> d = dt.load(filepath)
- dicter.dicter.mkdir(dirpath)
Create directory.
- dicter.dicter.save(d: dict, filepath: str, overwrite: bool = False, verbose: [<class 'str'>, <class 'int'>] = 'info')
Save dictionary to json.
- Parameters:
d (dict) – Dictionary.
filepath (str) – File path to store the dictionary.
overwrite (bool, default: False) – True: Overwrite if file exists. False: Do not overwrite existing files.
verbose (int, default is 'info' or 20) –
- Set the verbose messages using string or integer values.
0, None, ‘silent’, ‘off’, ‘no’: No message.
10,’debug’: Messages from debug level and higher.
20,’info’: Messages from info level and higher.
30,’warning’: Messages from warning level and higher.
40,’critical’: Messages from critical level and higher.
- Returns:
writeok – True: Succesful writing to json on disk. False: Nothing is written to disk.
- Return type:
bool
Examples
>>> # Import dicter >>> import dicter as dt > >>> d = {'level_a': None, 'level_b': {'a': 'hello world'}, 'level_c': True, 'level_d': 2.3, 'level_e': [[1,2,3], [1,2]]} >>> dt.save(d, filepath='c:/temp/test/dicter_save.json', overwrite=True)
- dicter.dicter.set_logger(verbose: [<class 'str'>, <class 'int'>] = 'info')
Set the logger for verbosity messages.
- Parameters:
verbose ([str, int], default is 'info' or 20) –
- Set the verbose messages using string or integer values.
[10, ‘debug’]: Messages from debug level and higher.
[20, ‘info’]: Messages from info level and higher.
[30, ‘warning’]: Messages from warning level and higher.
[50, ‘critical’]: Messages from critical level and higher.
[60, 0, None, ‘silent’, ‘off’, ‘no’]: No message.
- Return type:
None.
Examples
>>> # Set the logger to warning >>> set_logger(verbose='warning') >>> # Test with different messages >>> logger.debug("Hello debug") >>> logger.info("Hello info") >>> logger.warning("Hello warning") >>> logger.critical("Hello critical")
- dicter.dicter.set_nested(d: dict, path: list, value: str) dict
Set nested value in dictionary.
- Parameters:
d (dict) – Input dictionary.
path (list) – The nested path of keys in a ordered list.
value (str) – The value that needs to be set.
- Return type:
Dictionary is updated without a return.
Examples
>>> # Import dicter >>> import dicter as dt >>> # >>> # Example: New path and value in dictionary. >>> d = {} >>> path = ['person', 'address', 'city'] >>> dt.set_nested(d, path, 'New York') >>> # Print updated dictionary >>> print(d) >>> # {'person': {'address': {'city': 'New York'}}} >>> # >>> # Example: Update value in path. >>> d = {'person': {'address': {'city': 'New York'}}} >>> path = ['person', 'address', 'city'] >>> dt.set_nested(d, path, 'Amsterdam') >>> # Print updated dictionary >>> print(d) >>> # {'person': {'address': {'city': 'Amsterdam'}}, 'person_2': {'address': {'city': 'Amsterdam'}}} >>> # >>> # Example: New path with value. >>> d = {'person': {'address': {'city': 'New York'}}} >>> path = ['person_2', 'address', 'city'] >>> dt.set_nested(d, path, 'Amsterdam') >>> # Print updated dictionary >>> print(d) >>> # {'person': {'address': {'city': 'New York'}}, 'person_2': {'address': {'city': 'Amsterdam'}}}
- dicter.dicter.to_df()
Convert to dataFrame.
- Parameters:
d (dict) – Input dictionary.
sep (str, default is '->') – The seperator should be unique and is used to normalize the dictionary.
- Return type:
None.
Examples
>>> # Import dicter >>> import dicter as dt >>> # >>> # Example 1 >>> d = {'Person 1': {'Country': 'NL', 'Income': {'Amsterdam': 100, 'The Haque': 50}, 'Age': 33, 'Type': 'bicycle'}, 'Person 2': {'Country': 'USA', 'Income': {'NY': 150, 'The Haque': 50}, 'Age': 23, 'Type': 'Taxi'}, 'Person 3': {'Country': 'DE', 'Income': {'BE': 90}, 'Age': 43, 'Type': 'Car'} } >>> # >>> # Convert to DataFrame >>> df = dt.to_df(d) >>> # >>> # >>> # Example 2 >>> d = {'Country': 'NL', 'Weight': {'Amsterdam': 100, 'The Haque': 50}, 'Age': 33} >>> df = dt.to_df(d) >>> #
- dicter.dicter.traverse(d: dict, sep: str = '$->$', keys_as_list: bool = True, verbose: [<class 'str'>, <class 'int'>] = 'info') list
Traverse through all paths in dictionary.
- Parameters:
d (dict) – Input dictionary.
sep (str, default is '$->$') – The seperator should be unique and is used to normalize the dictionary. Change the sep in case a string exists with such pattern in the input dictionary.
keys_as_list (bool, default is True) – True: Output is structured list with keys. False: Output is list with keys seperated with the “sep” string.
verbose (int, default is 'info' or 20) –
- Set the verbose messages using string or integer values.
[0, None, ‘silent’, ‘off’, ‘no’]: No message.
[10, ‘debug’]: Messages from debug level and higher.
[20, ‘info’]: Messages from info level and higher.
[30, ‘warning’]: Messages from warning level and higher.
[40, ‘critical’]: Messages from critical level and higher.
- Returns:
dic – list containing two columns: [[key], value].
- Return type:
list
Examples
>>> # Import dicter >>> import dicter as dt >>> # >>> Example dict >>> d = {'level_a': 1, 'level_b': {'a': 'hello world'}, 'level_c': 3, 'level_d': {'a': 1, 'b': 2, 'c': {'e': 10}}, 'level_e': 2} >>> # Traverse all paths in dictionary >>> dlist = dt.traverse(d) >>> # >>> print(dlist) >>> # [[['level_a'], 1], >>> # [['level_c'], 3], >>> # [['level_e'], 2], >>> # [['level_b', 'a'], 'hello world'], >>> # [['level_d', 'a'], 1], >>> # [['level_d', 'b'], 2], >>> # [['level_d', 'c', 'e'], 10]] >>> # >>> # Example to retrieve value from a dictionary >>> value = dt.get_nested(d, dlist[3]) >>> # Look up for ['level_b', 'a'] >>> print(value) # hello world
- dicter.dicter.update(dictionary, key_to_update, new_value)
Update a nested dictionary for a specific key with a new value.
- Parameters:
dictionary (dict) – The nested dictionary to update.
key_to_update (str) – The key to update.
new_value (any) – The new value to assign to the specified key.
Examples
>>> data_dict_template = { ... 'DEPARTURE': { ... 'SLOPE': 3, ... 'INTERSECTION': 'V4', ... 'TORA': '1234', ... 'CIRCUIT_ALTITUDE': '1000', ... 'TOWER': '122.108', ... 'alignment': 'concrete' ... } ... } >>> update(data_dict_template['DEPARTURE'], 'SLOPE', 5) >>> data_dict_template['DEPARTURE']['SLOPE'] 5
>>> update(data_dict_template['DEPARTURE'], 'INTERSECTION', 'V5') >>> data_dict_template['DEPARTURE']['INTERSECTION'] 'V5'
>>> update(data_dict_template['DEPARTURE'], 'TORA', '2000') >>> data_dict_template['DEPARTURE']['TORA'] '2000'
>>> update(data_dict_template['DEPARTURE'], 'CIRCUIT_ALTITUDE', '1500') >>> data_dict_template['DEPARTURE']['CIRCUIT_ALTITUDE'] '1500'
>>> update(data_dict_template['DEPARTURE'], 'TOWER', '122.109') >>> data_dict_template['DEPARTURE']['TOWER'] '122.109'
>>> update(data_dict_template['DEPARTURE'], 'alignment', 'asphalt') >>> data_dict_template['DEPARTURE']['alignment'] 'asphalt'