Utils#

Module defines helper functions for the better_dict package.

Functions#

flatten(line)[source]#

Flatten an arbitrarily nested sequence.

Parameters:

line (Any) – The possibly nested sequence to flatten.

Returns:

flattened – A generator that yields the flattened sequence.

Return type:

generator

Examples

>>> list(flatten([[1, 2, 3], 4, [5, [6, 7, 8]]]))
[1, 2, 3, 4, 5, 6, 7, 8]
>>> list(flatten(1))
[1]

Notes

  • This function doesn’t consider strings sequences.

  • If the input is not a sequence, it is returned as a single element list.

iterable_not_string(value)[source]#

Check if the value is iterable but not a string.

Parameters:

value (object) – Value to check.

Returns:

True if the value is iterable but not a string, False otherwise.

Return type:

bool

Examples

>>> iterable_not_string([1, 2, 3])
True
>>> iterable_not_string('abc')
False
>>> iterable_not_string(1)
False
make_list(value)[source]#

Make a list from the value.

Parameters:

value (Any) – Value to make a list from.

Returns:

List with the value.

Return type:

List[Any]

same_length(keys, values)[source]#

Check if the keys and values have the same length.

Parameters:
  • keys (Any) – Keys to check.

  • values (Any) – Values to check.

Returns:

True if the keys and values are iterables and have the same length, False otherwise.

Return type:

bool

Examples

>>> same_length([1, 2, 3], [4, 5, 6])
True
>>> same_length([1, 2, 3], [4, 5])
False
>>> same_length([1, 2, 3], 4)
False