liblaf.grapes.itertools
ยค
Functions:
-
as_iterable
โ.
-
as_sequence
โ.
-
deep_merge
โ -
first_not_none
โReturns the first
not None
value in theargs
. -
len_or_none
โ
as_iterable
ยค
.
Examples:
If obj
is iterable, return an iterator over its items:
>>> obj = (1, 2, 3)
>>> as_iterable(obj)
(1, 2, 3)
If obj
is not iterable, return a one-item iterable containing obj
:
>>> obj = 1
>>> as_iterable(obj)
(1,)
If obj
is None
, return an empty iterable:
>>> obj = None
>>> as_iterable(None)
()
By default, binary and text strings are not considered iterable:
>>> obj = "foo"
>>> as_iterable(obj)
('foo',)
If base_type
is set, objects for which isinstance(obj, base_type)
returns True
won't be considered iterable.
>>> obj = {"a": 1}
>>> as_iterable(obj)
{'a': 1}
>>> as_iterable(obj, base_type=dict) # Treat dicts as a unit
({'a': 1},)
Set base_type
to None
to avoid any special handling and treat objects Python considers iterable as iterable:
>>> obj = "foo"
>>> as_iterable(obj, base_type=None)
'foo'
References
Source code in src/liblaf/grapes/itertools/_as_iterable.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
as_sequence
ยค
.
Examples:
If obj
is iterable, return an iterator over its items:
>>> obj = (1, 2, 3)
>>> as_sequence(obj)
(1, 2, 3)
If obj
is not iterable, return a one-item iterable containing obj
:
>>> obj = 1
>>> as_sequence(obj)
(1,)
If obj
is None
, return an empty iterable:
>>> obj = None
>>> as_sequence(None)
()
By default, binary and text strings are not considered iterable:
>>> obj = "foo"
>>> as_sequence(obj)
('foo',)
If base_type
is set, objects for which isinstance(obj, base_type)
returns True
won't be considered iterable.
>>> obj = {"a": 1}
>>> as_sequence(obj)
({'a': 1},)
>>> as_sequence(obj, base_type=dict) # Treat dicts as a unit
({'a': 1},)
Set base_type
to None
to avoid any special handling and treat objects Python considers iterable as iterable:
>>> obj = "foo"
>>> as_sequence(obj, base_type=None)
'foo'
References
Source code in src/liblaf/grapes/itertools/_as_sequence.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
deep_merge
ยค
Source code in src/liblaf/grapes/itertools/_deep_merge.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
first_not_none
ยค
first_not_none(*args: T | None) -> T
Returns the first not None
value in the args
.
Examples:
>>> first_not_none(1, 2)
1
>>> first_not_none(None, 1)
1
References
Source code in src/liblaf/grapes/itertools/_first_not_none.py
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|