Source code for WrightTools.kit._list

"""Tools for working with lists."""

# --- import --------------------------------------------------------------------------------------

import itertools

# --- define --------------------------------------------------------------------------------------


__all__ = ["flatten_list", "intersperse", "get_index", "from_list_of_objects", "pairwise"]


# --- functions -----------------------------------------------------------------------------------


[docs] def flatten_list(items, seqtypes=(list, tuple), in_place=True): """Flatten an irregular sequence. Works generally but may be slower than it could be if you can make assumptions about your list. `Source`__ __ https://stackoverflow.com/a/10824086 Parameters ---------- items : iterable The irregular sequence to flatten. seqtypes : iterable of types (optional) Types to flatten. Default is (list, tuple). in_place : boolean (optional) Toggle in_place flattening. Default is True. Returns ------- list Flattened list. Examples -------- >>> l = [[[1, 2, 3], [4, 5]], 6] >>> wt.kit.flatten_list(l) [1, 2, 3, 4, 5, 6] """ if not in_place: items = items[:] for i, _ in enumerate(items): while i < len(items) and isinstance(items[i], seqtypes): items[i : i + 1] = items[i] return items
[docs] def intersperse(lis, value): """Put value between each existing item in list. Parameters ---------- lis : list List to intersperse. value : object Value to insert. Returns ------- list interspersed list """ out = [value] * (len(lis) * 2 - 1) out[0::2] = lis return out
[docs] def get_index(lis, argument): """Find the index of an item, given either the item or index as an argument. Particularly useful as a wrapper for arguments like channel or axis. Parameters ---------- lis : list List to parse. argument : int or object Argument. Returns ------- int Index of chosen object. """ # get channel if isinstance(argument, int): if -len(lis) <= argument < len(lis): return argument else: raise IndexError("index {0} incompatible with length {1}".format(argument, len(lis))) else: return lis.index(argument)
def from_list_of_objects(values: list, attrs: list, hint): """ convenience wrapper of `get_index`. Retrieve an object from a list of objects using either: * its index, * matching to an attr, or * passing the value itself the method normalizes a variety of input types to get the object itself. arguments --------- values: list list of objects to be searched attrs: list list of attrs that correspond to objects in the list `values` hint: str, list or object if hint is a member of values, return the hint itself returns ------- match: desired_type the member of `values` that matches the given hint. Raises ValueError if a match is not found. see also -------- WrightTools.kit.get_index """ if isinstance(hint, int) or isinstance(hint, str): return values[get_index(attrs, hint)] elif hint in values: return hint else: raise ValueError(f"could not find a member in values based on hint {hint}")
[docs] def pairwise(iterable): """s -> (s0,s1), (s1,s2), (s2, s3), ... Originally from `itertools docs`__ __ https://docs.python.org/3/library/itertools.html#itertools-recipes Parameters ---------- iterable : iterable Iterable from which to produce pairs Returns ------- generator Generator which producis pairwise tuples """ a, b = itertools.tee(iterable) next(b, None) return zip(a, b)