roverd.channels.utils
¶
Channel utilities and types.
roverd.channels.utils.Data
module-attribute
¶
Generic writable data.
Should generally behave as follows:
- If
Shaped[np.ndarray, "..."], the shape and dtype are assumed to have semantic meaning, and are verified. - If
bytesorbytearray, we assume that the caller has already done any necessary binary conversion. No type or shape verification is performed.
roverd.channels.utils.Streamable
module-attribute
¶
Streamable = Iterator[T] | Iterable[T] | Queue[T | None | ExceptionSentinel]
Any stream-like container.
Warning
Unlike Iterator[T] or Iterable[T], a Streamable: Queue may also yield
None at the end of the stream or ExceptionSentinel if an exception
occurs in the producer.
roverd.channels.utils.Buffer
¶
Bases: Generic[T]
Simple queue buffer (i.e. queue to iterator) with batching.
Accepts the following "signals":
None: indicates that the stream is complete (i.e.StopIteration).ExceptionSentinel: indicates that a generic exception occurred, which should be re-raised in the thread which reads from the buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queue
|
Queue[T | None | ExceptionSentinel]
|
queue to use as a buffer. Should return |
required |
Source code in format/src/roverd/channels/utils.py
roverd.channels.utils.ExceptionSentinel
dataclass
¶
Sentinel class to indicate an exception, which should bubble up.
Info
We use an ExceptionSentinel wrapping the actual exception in case
users want to put an actual Exception into a stream.
Source code in format/src/roverd/channels/utils.py
roverd.channels.utils.Prefetch
¶
Bases: Buffer[T]
Simple prefetch queue wrapper (i.e. iterator to queue).
Can be used as a prefetched iterator (for x in Prefetch(...)), or as a
queue (Prefetch(...).queue). When used as a queue, None is put in the
queue to indicate that the iterator has terminated.
Warning
Any exceptions raised in the provided iterator are re-raised in the thread which reads the queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterator
|
Iterable[T] | Iterator[T]
|
any python iterator; must never yield |
required |
size
|
int
|
prefetch buffer size. |
64
|
Source code in format/src/roverd/channels/utils.py
roverd.channels.utils.batch_iterator
¶
Convert an iterator into a batched version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterator
|
Iterator[T] | Iterable[T]
|
input iterator/iterable. |
required |
size
|
int
|
batch size. |
8
|