gdpc.utils

Various generic utilities.


sign(x: Any) int

Returns the sign of x

nonZeroSign(x: Any) int

Returns the sign of x, except that nonZeroSign(0) == 1

clamp(x: ComparableT, minimum: ComparableT, maximum: ComparableT) ComparableT

Clamps x to the range [minimum, maximum]

eagerAll(iterable: Iterable[Any]) bool

Like all(), but always evaluates every element

eagerAny(iterable: Iterable[Any]) bool

Like any(), but always evaluates every element

normalized(a: ndarray[tuple[int, ...], dtype[Any]], order: int = 2, axis: int = -1)

Normalizes a using the L<order> norm.

If axis is specified, normalizes along that axis.

withRetries(function: ~typing.Callable[[], ~gdpc.utils.T], exceptionType: ~typing.Type[Exception] = <class 'Exception'>, retries: int = 1, onRetry: ~typing.Callable[[Exception, int], None] = <function <lambda>>, reRaise: bool = True) T | None

Retries function up to retries times if an exception occurs.

Before retrying, calls onRetry(<last exception>, <remaining retries>). The default callback sleeps for one second.

If the retries have ran out and reRaise is True, the last exception is re-raised.

isIterable(value: Any) bool

Determine whether value is iterable.

isSequence(value: Any) bool

Determine whether value is a sequence.

class OrderedByLookupDict

Dict ordered from least to most recently looked-up key

Unless maxSize is 0, the dict size is limited to maxSize by evicting the least recently looked-up key when full.

__init__(maxSize: int, *args: Any, **kwargs: Any)
property maxSize : int
__getitem__(key: KT) VT

Return self[key].

__setitem__(key: KT, value: VT)

Set self[key] to value.

__orig_bases__ = (typing.OrderedDict[~KT, ~VT], typing.Generic[~KT, ~VT])
visualizeMaps(*arrays: ndarray[tuple[int, ...], dtype[Any]], title: str = '', normalize: bool = True) None

Deprecated

This function is deprecated and will be removed in a future version of GDPC.

It was only used by the now-removed visualize_map.py example, and its removal will allow us to remove the OpenCV dependency.

Visualizes one or multiple 2D numpy arrays.

readFileBytes(filePath: Path | str) bytes

Opens stored file and returns it a string of bytes.

rotateSequence(sequence: Sequence[T], n: int = 1) Generator[T, None, None]

Rotates a sequence of elements by n positions.

Args:

sequence (Sequence): The sequence of elements to rotate. n (int, optional): The number of positions to rotate the sequence by. Defaults to 1.

Yields:

Generator[Any, Any, None]: The rotated sequence of elements.