API Reference

class flexdict.FlexDict(data=None)[source]

Bases: dict

Provides automatic and arbitrary levels of nesting along with additional utility methods.

Parameters:data (dict) – Data to initialize the FlexDict with.
locked

Flag indicating if auto-nesting is locked.

Type:bool
contains(subset)[source]

Checks if this dictionary is a superset of a given one.

Parameters:subset (dict) – Dictionary to check if it is a subset.
Returns:True if self contains subset else False.
Return type:bool
flatten()[source]

Flattens the dictionary.

Returns:A list of tuples containing key-paths and values.
Return type:list
get(keys, default=None)[source]

Gets a value from the dictionary with the provided keys.

Parameters:
  • keys – Keys pointing to the target value.
  • default (any) – Default value to return if target does not exists.
Returns:

The corresponding dictionary value.

Return type:

any

inside(superset)[source]

Checks if this dictionary is a subset of a given one.

Parameters:superset (dict) – Dictionary to check if it is a superset.
Returns:True if self is inside the superset else False.
Return type:bool
keys(nested=False, unique=False)[source]

Gets keys from the dictionary.

Parameters:
  • nested (bool) – Gets all keys recursively if set to True.
  • unique (bool) – Gets only the unique keys if set to True.
Returns:

dict_keys

If nested is False and unique is False.

list

If nested is True and unique is False.

set

If unique is True.

Return type:

Union[dict_keys, list, set]

length(nested=False, unique=False)[source]

Counts the number of keys inside the dictionary.

Parameters:
  • nested (bool) – Counts all keys recursively if set to True.
  • unique (bool) – Counts only the unique keys if set to True.
Returns:

Number of keys.

Return type:

int

lock(inplace=True)[source]

Locks the automatic nesting mechanism.

Parameters:inplace (bool) – Creates a locked copy if True.
Returns:
None
If inplace is set to True.
FlexDict
If inplace`set to `False.
Return type:Union[None, FlexDict]
pop()[source]

Removes and returns the last key-value pair from the dictionary.

Returns:
FlexDict
The last key-value pair of the dictionary.
None
If self is empty.
Return type:Union[FlexDict, None]
set(keys, value, overwrite=True, increment=False)[source]

Sets a dictionary value with the given keys.

Parameters:
  • keys (any) – Key(s) pointing to the value.
  • value (any) – Value to set.
  • overwrite (bool) – If False, only sets a value if it not exists.
  • increment (bool) – Increments the value by value if set to True. overwrite argument has no effect on this. Causes the method to return the target value.
Returns:

Final state of the target value if increment is enabled.

Return type:

Union[int, float, None]

size(unique=False)[source]

Counts the number of keys and values inside the dictionary.

Parameters:
  • nested (bool) – Counts all items recursively if set to True.
  • unique (bool) – Counts only the unique items if set to True.
Returns:

Number of items.

Return type:

int

unlock(inplace=True)[source]

Unlocks the automatic nesting mechanism.

Parameters:inplace (bool) – Creates an unlocked copy if True.
Returns:
None
If inplace is set to True.
FlexDict
If inplace`set to `False.
Return type:Union[None, FlexDict]
values(nested=False, unique=False)[source]

Gets values from the dictionary.

Parameters:
  • nested (bool) – Gets all values recursively if set to True.
  • unique (bool) – Gets only the unique values if set to True.
Returns:

dict_values

If nested is False and unique is False.

list:

If nested is True and unique is False.

list:

If unique is True.

Return type:

Union[dict_values list, set]