Get Nested
Get nested value from dictionary.
- param d:
Input dictionary.
- type d:
dict
- param path:
The nested path of keys in a ordered list. Example: [‘level 1’, ‘level 2’]
- type path:
list
- returns:
value –
value belonging to the key.
None: if the path does not exists.
- rtype:
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
Set Nested
Set nested value from dictionary.
Set nested value in dictionary.
- param d:
Input dictionary.
- type d:
dict
- param path:
The nested path of keys in a ordered list.
- type path:
list
- param value:
The value that needs to be set.
- type value:
str
- rtype:
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'}}}
Update Nested Dictionary
Update a nested dictionary for a specific key with a new value.
- param dictionary:
The nested dictionary to update.
- type dictionary:
dict
- param key_to_update:
The key to update.
- type key_to_update:
str
- param new_value:
The new value to assign to the specified key.
- type new_value:
any
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'
Traverse
Traverse through all paths in dictionary.
- param d:
Input dictionary.
- type d:
dict
- param sep:
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.
- type sep:
str, default is ‘$->$’
- param keys_as_list:
True: Output is structured list with keys. False: Output is list with keys seperated with the “sep” string.
- type keys_as_list:
bool, default is True
- param verbose:
- 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.
- type verbose:
int, default is ‘info’ or 20
- returns:
dic – list containing two columns: [[key], value].
- rtype:
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
Flatten
Flatten dictionary to the deepest level and return key-value.
- param d:
Input dictionary.
- type d:
dict
- returns:
d_flatten – Flattened dictionary containing key and value.
- rtype:
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)
Check key exists
Check whether key exists in dictionary.
Check whether the dictionary contains the key.
- param d:
Input dictionary.
- type d:
dict
- param path:
The nested path of keys in a ordered list. Example: [‘level 1’, ‘level 2’]
- type path:
list
- returns:
True: Key exists. False: Key does not exist.
- rtype:
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"])
Depth
Depth of dictionary.
- param d:
Input dictionary.
- type d:
dict
- returns:
depth – Depth of dictionary.
- rtype:
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)
Compare
Compare two dictionaries.
The second dictionary is compared with the first one, and results are shown accordingly.
- param d1:
Dictionary.
- type d1:
dict
- param d2:
Dictionary.
- type d2:
dict
- 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'])
>
To DataFrame
Save dictionary to json.
- param d:
Dictionary.
- type d:
dict
- param filepath:
File path to store the dictionary.
- type filepath:
str
- param overwrite:
True: Overwrite if file exists. False: Do not overwrite existing files.
- type overwrite:
bool, default: False
- param verbose:
- 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.
- type verbose:
int, default is ‘info’ or 20
- returns:
writeok – True: Succesful writing to json on disk. False: Nothing is written to disk.
- rtype:
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)
Save
Save dictionary to json.
- param d:
Dictionary.
- type d:
dict
- param filepath:
File path to store the dictionary.
- type filepath:
str
- param overwrite:
True: Overwrite if file exists. False: Do not overwrite existing files.
- type overwrite:
bool, default: False
- param verbose:
- 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.
- type verbose:
int, default is ‘info’ or 20
- returns:
writeok – True: Succesful writing to json on disk. False: Nothing is written to disk.
- rtype:
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)
Load
Load dictionary from json file.
- param filepath:
path location to json.
- type filepath:
str
- param verbose:
- 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.
- type verbose:
str, optional
- returns:
dictionary – None or Dictionary.
- rtype:
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)