GitHub

ChunkedMessage

API reference for ChunkedMessage.

Part part[0] of part[1] of the message id.

Attributes

AttributeTypeDescription
idint = S.field(S.Int)
parttuple[int, int] = S.field(S.Tuple(S.Natural, S.Natural).check(S.make_filter(_part_in_range, expected='a chunk part with an index less than its total')))
databytes = S.field(TransferableBytes)
chunk_size= CHUNK_SIZE

Functions

initial_join_state

initial_join_state() -> JoinState

Returns JoinState

@staticmethod
def initial_join_state() -> JoinState:
    return {}

split

split(id, data) -> list[ChunkedMessage]

data in parts of at most CHUNK_SIZE bytes (one part when empty).

ParameterTypeDescription
idint
databytes

Returns list[ChunkedMessage]

@staticmethod
def split(id: int, data: bytes) -> list[ChunkedMessage]:
    """``data`` in parts of at most ``CHUNK_SIZE`` bytes (one part when
    empty)."""
    parts = max(1, math.ceil(len(data) / CHUNK_SIZE))
    return [
        ChunkedMessage(id=id, part=(i, parts), data=data[i * CHUNK_SIZE : (i + 1) * CHUNK_SIZE])
        for i in range(parts)
    ]

join

join(state, part) -> bytes | None

Record part; the whole message once every part has arrived (a repeated part is ignored).

ParameterTypeDescription
stateJoinState
partChunkedMessage

Returns bytes | None

@staticmethod
def join(state: JoinState, part: ChunkedMessage) -> bytes | None:
    """Record ``part``; the whole message once every part has arrived
    (a repeated part is ignored)."""
    index, total = part.part
    entry = state.get(part.id)
    if entry is None:
        entry = state[part.id] = _Parts(total)
    if index in entry.parts:
        return None
    entry.parts[index] = part.data
    entry.length = max(entry.length, index + 1)
    entry.count += 1
    entry.bytes += len(part.data)
    if entry.count != total:
        return None
    if len(entry.parts) != entry.length:
        # Effect joins every slot, and an empty one throws (keeping the entry).
        raise ValueError("Chunked message is missing a part")
    del state[part.id]
    return b"".join(entry.parts[i] for i in range(entry.length))

fields

fields() -> Mapping[str, Bottom[object, object, Never, Never]]

Each field's schema, the tag's first in a tagged class.

Returns Mapping[str, Bottom[object, object, Never, Never]]

@classmethod
def fields(cls) -> Mapping[str, Bottom[object, object, Never, Never]]:
    """Each field's schema, the tag's first in a tagged class."""
    shape = _shape_of(cls)
    schemas = shape.schemas
    if shape.tag is None:
        return dict(schemas)
    tag: Bottom[object, object, Never, Never] = Bottom(ast.Literal(shape.tag))
    return {"_tag": tag, **schemas}

make

make(input, **options) -> Self

input checked against the class's schema (its checks included); raises SchemaError. Construct with cls(...); an instance is kept as it is.

ParameterTypeDescription
inputSelf
optionsUnpack[ParseOptions] = {}

Returns Self

@classmethod
def make(cls, input: Self, /, **options: Unpack[ParseOptions]) -> Self:
    """``input`` checked against the class's schema (its checks
    included); raises ``SchemaError``. Construct with ``cls(...)``; an
    instance is kept as it is."""
    shape = _shape_of(cls)
    return make_class_sync(
        cls.ast.to_type(),
        input,
        options,
        lambda fields: cast("Self", shape.construct(dict(fields))),
    )

make_option

make_option(input, **options) -> Option[Self]
ParameterTypeDescription
inputSelf
optionsUnpack[ParseOptions] = {}

Returns Option[Self]

@classmethod
def make_option(cls, input: Self, /, **options: Unpack[ParseOptions]) -> Option[Self]:
    return cls.__codec__().make_option(input, **options)

make_effect

make_effect(input, **options) -> Effect[Self, Issue]
ParameterTypeDescription
inputSelf
optionsUnpack[ParseOptions] = {}

Returns Effect[Self, Issue]

@classmethod
def make_effect(cls, input: Self, /, **options: Unpack[ParseOptions]) -> Effect[Self, Issue]:
    return cls.__codec__().make_effect(input, **options)

annotate

annotate(**annotations) -> Bottom[Self, Mapping[str, object]]

The class's schema with annotations (a new schema, not a class).

ParameterTypeDescription
annotationsobject = {}

Returns Bottom[Self, Mapping[str, object]]

@classmethod
def annotate(cls, /, **annotations: object) -> Bottom[Self, Mapping[str, object]]:
    """The class's schema with annotations (a new schema, not a class)."""
    return cls.__codec__().annotate(**annotations)

check

check(first, *rest) -> Bottom[Self, Mapping[str, object]]

The class's schema with checks (a new schema, not a class).

ParameterTypeDescription
firstCheck[Self]
restCheck[Self] = ()

Returns Bottom[Self, Mapping[str, object]]

@classmethod
def check(cls, first: Check[Self], /, *rest: Check[Self]) -> Bottom[Self, Mapping[str, object]]:
    """The class's schema with checks (a new schema, not a class)."""
    return cls.__codec__().check(first, *rest)

pipe

pipe[B](f) -> B
ParameterTypeDescription
fCallable[[type[Self]], B]

Returns B

@classmethod
def pipe[B](cls, f: Callable[[type[Self]], B], /) -> B:
    return f(cls)