Public API¶
See also
Check out Examples derived from real and fully tested source code.
-
dotty_dict.dotty_dict.
dotty
(dictionary=None, no_list=False)[source]¶ Factory function for Dotty class.
Create Dotty wrapper around existing or new dictionary.
Parameters: - dictionary (dict) – Any dictionary or dict-like object
- no_list (bool) – If set to True then numeric keys will NOT be converted to list indices
Returns: Dotty instance
-
class
dotty_dict.dotty_dict.
Dotty
(dictionary, separator='.', esc_char='\', no_list=False)[source]¶ Dictionary and dict-like objects wrapper.
Dotty wraps dictionary and provides proxy for quick accessing to deeply nested keys and values using dot notation.
Dot notation can be customize in special cases. Let’s say dot character has special meaning, and you want to use other character for accessing deep keys.
Dotty does not copy original dictionary but it operates on it. All changes made in original dictionary are reflected in dotty wrapped dict and vice versa.
Parameters: - dictionary (dict) – Any dictionary or dict-like object
- separator (str) – Character used to chain deep access.
- esc_char (str) – Escape character for separator.
- no_list (bool) – If set to True then numeric keys will NOT be converted to list indices
-
clear
()¶ Removes all elements from dotty dict.
-
items
()¶ Returns generator of dotty dict’s (key, value) tuple pairs.
-
keys
()¶ Returns generator of dotty dict’s keys.
-
values
()¶ Returns generator of dotty dict’s values.
-
update
(dict2)¶ Adds dictionary dict2’s key-values pairs to dotty dict.
-
static
fromkeys
(seq, value=None)[source]¶ Create a new dictionary with keys from seq and values set to value.
New created dictionary is wrapped in Dotty.
Parameters: - seq – Sequence of elements which is to be used as keys for the new dictionary
- value – Value which is set to each element of the dictionary
Returns: Dotty instance
-
get
(key, default=None)[source]¶ Get value from deep key or default if key does not exist.
This method match 1:1 with dict .get method except that it accepts deeply nested key with dot notation.
Parameters: - key (str) – Single key or chain of keys
- default (Any) – Default value if deep key does not exist
Returns: Any or default value
-
pop
(key, default=None)[source]¶ Pop key from Dotty.
This method match 1:1 with dict .pop method except that it accepts deeply nested key with dot notation.
Parameters: - key (str) – Single key or chain of keys
- default (Any) – If default is provided will be returned
Raises: KeyError – If key does not exist and default has not been provided
Returns: Any or default value
-
static
set_list_index
(data, index, value)[source]¶ Set value in list at specified index. All the values before target index should stay unchanged or be filled with None. :param data: List where value should be set :param index: String or Int of target index :param value: Target value to put under index
-
setdefault
(key, default=None)[source]¶ Get key value if exist otherwise set default value under given key and return its value.
This method match 1:1 with dict .setdefault method except that it accepts deeply nested key with dot notation.
Parameters: - key (str) – Single key or chain of keys
- default (Any) – Default value for not existing key
Returns: Value under given key or default