redex.util module¶
General utility functions.
- redex.util.expand_to_tuple(item: Any) tuple[typing.Any, ...][source]¶
Wraps anything but tuple into a tuple.
- Parameters
item – any sequence or a single item.
- Returns
a tuple.
>>> from redex import util >>> util.expand_to_tuple((1,)) (1,) >>> util.expand_to_tuple((1,2)) (1, 2)
- redex.util.flatten(item: Any) List[Any][source]¶
Recursively flatten any sequence but a string or bytes.
- Parameters
item – a sequence or a single item. A single item will be packet into a list.
- Returns
a list with tuples got flattened.
>>> from redex import util >>> util.flatten([1, [(2, 3), 4]]) [1, 2, 3, 4] >>> util.flatten(1) [1]
- redex.util.flatten_tuple_annotation_shape(shape: tuple[typing.Any, ...]) List[Any][source]¶
Recursively flatten tuple annotation shapes.
Flattening a single shaped item ((),) and a single flat item () both result into a single item list [()].
- Parameters
shape – annotation shape.
- Returns
a flat list of annotation shapes.
>>> from redex import util >>> util.flatten_tuple_annotation_shape(((), (((), ()), ()))) [(), (), (), ()] >>> util.flatten_tuple_annotation_shape(((),)) [()] >>> util.flatten_tuple_annotation_shape(()) [()]
- redex.util.flatten_tuple_annotations(annotation: Any) List[Any][source]¶
Recursively flatten tuples in the type annotation.
- Parameters
annotation – a type annotation.
- Returns
a list with tuple annotations got flattened.
- Raises
ValueError – if the annotation include variadic tuples. Variadic tuples nested in other then tuple annotations (e.g. Sequence(tuple[Any, …])) are fine.
>>> from redex import util >>> from typing import Any >>> util.flatten_tuple_annotations(tuple[Any, tuple[tuple[Any, Any], Any]]) [typing.Any, typing.Any, typing.Any, typing.Any] >>> util.flatten_tuple_annotations(Any) [typing.Any]
- redex.util.flatten_tuples(item: Any) List[Any][source]¶
Recursively flatten tuples in a sequence.
- Parameters
item – a sequence or a single item. A single item will be packet into a list.
- Returns
a list with tuples got flattened.
>>> from redex import util >>> util.flatten_tuples((1, ((2, 3), 4))) [1, 2, 3, 4] >>> util.flatten_tuples(1) [1]
- redex.util.infer_tuple_annotation_shape(annotation: Any) tuple[typing.Any, ...][source]¶
Recursively infer shapes of the tuples in the type annotation.
Each tuple in the annotation shape represent an item.
Note that: - ((), ((), ())) represents some shape, for example tuple[Any, tuple[Any, Any]]. - ((),) represents a single shaped item, meaning tuple[Any]. - () represents an unshaped item, such as Any or int. - None is acceptable value.
- Parameters
annotation – a type annotation.
- Returns
annotation shapes.
- Raises
ValueError – if the annotation include variadic tuples. Variadic tuples nested in other then tuple annotations (e.g. Sequence(tuple[Any, …])) are fine.
>>> from redex import util >>> from typing import Any >>> util.infer_tuple_annotation_shape(tuple[Any, tuple[tuple[Any, Any], Any]]) ((), (((), ()), ())) >>> util.infer_tuple_annotation_shape(tuple[Any]) ((),) >>> util.infer_tuple_annotation_shape(Any) () >>> util.infer_tuple_annotation_shape(None) ()
- redex.util.reshape_tuples(item: Any, shape: tuple[typing.Any, ...]) Any[source]¶
Recursively shape a sequence into tuples.
- Parameters
item – a flat sequence or a single item.
shape – a desired shape.
- Returns
shaped tuples or original input for the shape ().
- Raises
RuntimeError – if input sequence wasn’t entirely consumed. It can only happen if there is a difference in the number of items in the input sequence and provided shape.
>>> from redex import util >>> util.reshape_tuples((1,2,3,4), ((),(((),()),()))) (1, ((2, 3), 4)) >>> util.reshape_tuples(1, ()) 1
- redex.util.squeeze_tuple(item: Any) Any[source]¶
Reduces a tuple to a single item if only it consists of a single item.
- Parameters
item – any sequence or a single item.
- Returns
a single item if possible, or an input sequence if not.
>>> from redex import util >>> util.squeeze_tuple((1,)) 1 >>> util.squeeze_tuple((1,2)) (1, 2)