Utils#
Module defines helper functions for the better_dict
package.
Functions#
iterable_not_string()
: Check if the value is iterable but not a string.same_length()
: Check if the keys and values have the same length.make_list()
: Make a list from the value.flatten()
: Flatten the iterable object.
- 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:
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:
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