Iterable Modules¶
Contents¶
pyavia.iter.all_none |
Returns True if all arguments (or elements of a single iterable argument) are None. |
pyavia.iter.all_not_none |
Shorthand function for all(x is not None for x in args) . |
pyavia.iter.any_in |
Shorthand function for any(x in target for x in iterable) . |
pyavia.iter.any_none |
Returns True if any argument (or element of a single iterable argument) are None. |
pyavia.iter.bounded_by |
Returns True if x is bounded by the given iterable. |
pyavia.iter.bracket_list |
Returns left and right indicies (l_idx, r_idx) of a sorted list that bracket x. |
pyavia.iter.count_op |
Return a count of the number of items in it where oper(value) == True . |
pyavia.iter.first |
Function returning the first item in the iterable that satisfies the condition. |
pyavia.iter.flatten |
Generator returning entries from a flattened representation of any sequence container (except strings). |
pyavia.iter.flatten_list |
Generator similar to flatten , however only flattens lists until a non-list element is found. |
pyavia.iter.split_dict |
Split dict a by keys and return two dicts x and y: |
pyavia.iter.singlify |
If x only has exactly one element, return that element only. |
Members¶
Handy functions for dealing with various iterables.
-
pyavia.iter.
all_none
(*args)¶ Returns True if all arguments (or elements of a single iterable argument) are None.
-
pyavia.iter.
all_not_none
(*args)¶ Shorthand function for
all(x is not None for x in args)
. Returns True if all *args are not None, otherwise False.
-
pyavia.iter.
any_in
(it: Iterable, target)¶ Shorthand function for
any(x in target for x in iterable)
. Returns True if found, otherwise False.
-
pyavia.iter.
any_none
(*args)¶ Returns True if any argument (or element of a single iterable argument) are None.
-
pyavia.iter.
bounded_by
(x, iterable, key=None)¶ Returns True if x is bounded by the given iterable. I.E.
min(iterable) <= x <= max(iterable)
. If key function is provided this is applied to x and the iterable values before comparison.
-
pyavia.iter.
bracket_list
(li, x, key=None)¶ Returns left and right indicies (l_idx, r_idx) of a sorted list that bracket x. I.E. where li`[`l_idx] <= x <= li`[`r_idx].
Note
This is not the same as the usual one-sided methods which ensure strict inequality on one side (e.g. low <= x < high). This means that for boundary values two brackets may satisfy the condition.
Parameters: - li (List) – Sorted list / tuple. Sorting can be in either direction.
- x – Value to bracket.
- key (function) – Comparison function if supplied. Default = None.
Returns: l_idx, r_idx – Note that r_idx = l_idx + 1 on return. For x equal to a middle list value, the left side bracket is returned.
Return type: Tuple
-
pyavia.iter.
count_op
(it: Iterable, oper, value)¶ Return a count of the number of items in it where
oper(value) == True
. This allows user-defined objects to be included and is subtly different to[...].count(...)
which uses the__eq__
operator.
-
pyavia.iter.
first
(it: Iterable, condition=<function <lambda>>)¶ Function returning the first item in the iterable that satisfies the condition. This function is taken almost directly from: https://stackoverflow.com/a/35513376
>>> first((1,2,3), condition=lambda x: x % 2 == 0) 2 >>> first(range(3, 100)) 3 >>> first(()) Traceback (most recent call last): ... StopIteration
Parameters: - it (Iterable) – Iterable to search.
- condition (boolean function (optional)) – Boolean condition applied to each iterable. If the condition is not given, the first item is returned.
Returns: - first_item – First item in the iterable it that satisfying the condition.
- Raises – StopIteration if no item satysfing the condition is found.
-
pyavia.iter.
flatten
(seq)¶ Generator returning entries from a flattened representation of any sequence container (except strings). Taken from https://stackoverflow.com/a/2158532
Examples
>>> for x in flatten((2, (3, (4, 5), 6), 7)): ... print(x, end='') 234567
Parameters: seq (list_like) – Sequence container Yields: Each entry in turn.
-
pyavia.iter.
flatten_list
(li)¶ Generator similar to
flatten
, however only flattens lists until a non-list element is found. Note that non-lists may contain sub-lists and these are not flattened.Parameters: li (List) – List to flatten. Yields: Each non-list entry in turn.
-
pyavia.iter.
singlify
(x)¶ If x only has exactly one element, return that element only. Otherwise return x unaltered.
-
pyavia.iter.
split_dict
(a: Dict, keys: Sequence) -> (typing.Dict, typing.Dict)¶ - Split dict a by keys and return two dicts x and y:
- x: Items in a having a key in keys.
- y: All other items in a.