GitHub

channel_schema

API reference for channel_schema.

Channels that decode or encode each chunk with a schema (Effect's ChannelSchema).

decode(schema) reads chunks of encoded values and writes the decoded ones; encode(schema) goes the other way. A value that does not fit fails the channel with SchemaError (its path starts at the value's index in the chunk). duplex wraps a channel of encoded values so it reads and writes decoded ones::

from pyfect import channel_schema
from pyfect import schema as S

numbers = text_stream.pipe_through_channel(channel_schema.decode(S.NumberFromString))

Functions

decode

decode[T, I, RD, E = Never, D = None](schema, **options) -> Channel[tuple[T, ...], E | SchemaError, D, tuple[I, ...], E, D, RD]

Decode each chunk of schema's encoded values, failing with SchemaError at the first chunk that does not decode.

ParameterTypeDescription
schemaCodec[T, I, RD, object]
optionsUnpack[ParseOptions] = {}

Returns Channel[tuple[T, ...], E | SchemaError, D, tuple[I, ...], E, D, RD]

def decode[T, I, RD, E = Never, D = None](
    schema: Codec[T, I, RD, object], /, **options: Unpack[ParseOptions]
) -> Channel[tuple[T, ...], E | SchemaError, D, tuple[I, ...], E, D, RD]:
    """Decode each chunk of ``schema``'s encoded values, failing with
    ``SchemaError`` at the first chunk that does not decode."""
    return _chunks(decode_with(NonEmptyArray(schema), **options))

decode_unknown

decode_unknown[T, RD, E = Never, D = None](schema, **options) -> Channel[tuple[T, ...], E | SchemaError, D, tuple[object, ...], E, D, RD]

decode reading chunks of any values.

ParameterTypeDescription
schemaCodec[T, object, RD, object]
optionsUnpack[ParseOptions] = {}

Returns Channel[tuple[T, ...], E | SchemaError, D, tuple[object, ...], E, D, RD]

def decode_unknown[T, RD, E = Never, D = None](
    schema: Codec[T, object, RD, object], /, **options: Unpack[ParseOptions]
) -> Channel[tuple[T, ...], E | SchemaError, D, tuple[object, ...], E, D, RD]:
    """``decode`` reading chunks of any values."""
    return _chunks(decode_with(NonEmptyArray(schema), **options))

encode

encode[T, I, RE, E = Never, D = None](schema, **options) -> Channel[tuple[I, ...], E | SchemaError, D, tuple[T, ...], E, D, RE]

Encode each chunk of schema's values, failing with SchemaError at the first chunk that does not encode.

ParameterTypeDescription
schemaCodec[T, I, object, RE]
optionsUnpack[ParseOptions] = {}

Returns Channel[tuple[I, ...], E | SchemaError, D, tuple[T, ...], E, D, RE]

def encode[T, I, RE, E = Never, D = None](
    schema: Codec[T, I, object, RE], /, **options: Unpack[ParseOptions]
) -> Channel[tuple[I, ...], E | SchemaError, D, tuple[T, ...], E, D, RE]:
    """Encode each chunk of ``schema``'s values, failing with
    ``SchemaError`` at the first chunk that does not encode."""
    return _chunks(encode_with(NonEmptyArray(schema), **options))

encode_unknown

encode_unknown[I, RE, E = Never, D = None](schema, **options) -> Channel[tuple[I, ...], E | SchemaError, D, tuple[object, ...], E, D, RE]

encode reading chunks of any values.

ParameterTypeDescription
schemaCodec[object, I, object, RE]
optionsUnpack[ParseOptions] = {}

Returns Channel[tuple[I, ...], E | SchemaError, D, tuple[object, ...], E, D, RE]

def encode_unknown[I, RE, E = Never, D = None](
    schema: Codec[object, I, object, RE], /, **options: Unpack[ParseOptions]
) -> Channel[tuple[I, ...], E | SchemaError, D, tuple[object, ...], E, D, RE]:
    """``encode`` reading chunks of any values."""
    return _chunks(encode_with(NonEmptyArray(schema), **options))

duplex

duplex[TI, II, TO, IO, OE, OD, ID, RR, IE = Never](channel: Channel[tuple[IO, ...], OE, OD, tuple[II, ...], IE | SchemaError, ID, RR], /, *, input_schema: Codec[TI, II, object, RR], output_schema: Codec[TO, IO, RR, object]) -> Channel[tuple[TO, ...], OE | SchemaError, OD, tuple[TI, ...], IE, ID, RR]
duplex[TI, II, TO, IO, OE, OD, ID, RR](channel: Channel[tuple[IO, ...], OE, OD, tuple[II, ...], SchemaError, ID, RR], /, *, input_schema: Codec[TI, II, object, RR], output_schema: Codec[TO, IO, RR, object]) -> Channel[tuple[TO, ...], OE | SchemaError, OD, tuple[TI, ...], Never, ID, RR]

channel, a channel of encoded values, reading values that input_schema encodes for it and writing what output_schema decodes from its output.

ParameterTypeDescription
channel
input_schema
output_schema
@overload
def duplex[TI, II, TO, IO, OE, OD, ID, RR, IE = Never](
    channel: Channel[tuple[IO, ...], OE, OD, tuple[II, ...], IE | SchemaError, ID, RR],
    /,
    *,
    input_schema: Codec[TI, II, object, RR],
    output_schema: Codec[TO, IO, RR, object],
) -> Channel[tuple[TO, ...], OE | SchemaError, OD, tuple[TI, ...], IE, ID, RR]: ...
@overload
def duplex[TI, II, TO, IO, OE, OD, ID, RR](
    channel: Channel[tuple[IO, ...], OE, OD, tuple[II, ...], SchemaError, ID, RR],
    /,
    *,
    input_schema: Codec[TI, II, object, RR],
    output_schema: Codec[TO, IO, RR, object],
) -> Channel[tuple[TO, ...], OE | SchemaError, OD, tuple[TI, ...], Never, ID, RR]: ...
def duplex(
    channel: Channel[Any, Any, Any, Any, Any, Any, Any],
    /,
    *,
    input_schema: Codec[Any, Any, Any, Any],
    output_schema: Codec[Any, Any, Any, Any],
) -> Channel[Any, Any, Any, Any, Any, Any, Any]:
    """``channel``, a channel of encoded values, reading values that
    ``input_schema`` encodes for it and writing what ``output_schema``
    decodes from its output."""
    require_channel(channel, "channel")
    outgoing: Channel[Any, Any, Any, Any, Any, Any, Any] = encode(input_schema)
    incoming: Channel[Any, Any, Any, Any, Any, Any, Any] = decode(output_schema)
    return outgoing.pipe_to(channel).pipe_to(incoming)

duplex_unknown

duplex_unknown[TI, TO, OE, OD, ID, RR, IE = Never](channel: Channel[tuple[object, ...], OE, OD, tuple[Any, ...], IE | SchemaError, ID, RR], /, *, input_schema: Codec[TI, object, object, RR], output_schema: Codec[TO, object, RR, object]) -> Channel[tuple[TO, ...], OE | SchemaError, OD, tuple[TI, ...], IE, ID, RR]
duplex_unknown[TI, TO, OE, OD, ID, RR](channel: Channel[tuple[object, ...], OE, OD, tuple[Any, ...], SchemaError, ID, RR], /, *, input_schema: Codec[TI, object, object, RR], output_schema: Codec[TO, object, RR, object]) -> Channel[tuple[TO, ...], OE | SchemaError, OD, tuple[TI, ...], Never, ID, RR]

duplex for a channel that writes values of any type.

ParameterTypeDescription
channel
input_schema
output_schema
@overload
def duplex_unknown[TI, TO, OE, OD, ID, RR, IE = Never](
    channel: Channel[tuple[object, ...], OE, OD, tuple[Any, ...], IE | SchemaError, ID, RR],
    /,
    *,
    input_schema: Codec[TI, object, object, RR],
    output_schema: Codec[TO, object, RR, object],
) -> Channel[tuple[TO, ...], OE | SchemaError, OD, tuple[TI, ...], IE, ID, RR]: ...
@overload
def duplex_unknown[TI, TO, OE, OD, ID, RR](
    channel: Channel[tuple[object, ...], OE, OD, tuple[Any, ...], SchemaError, ID, RR],
    /,
    *,
    input_schema: Codec[TI, object, object, RR],
    output_schema: Codec[TO, object, RR, object],
) -> Channel[tuple[TO, ...], OE | SchemaError, OD, tuple[TI, ...], Never, ID, RR]: ...
def duplex_unknown(
    channel: Channel[Any, Any, Any, Any, Any, Any, Any],
    /,
    *,
    input_schema: Codec[Any, Any, Any, Any],
    output_schema: Codec[Any, Any, Any, Any],
) -> Channel[Any, Any, Any, Any, Any, Any, Any]:
    """``duplex`` for a channel that writes values of any type."""
    require_channel(channel, "channel")
    outgoing: Channel[Any, Any, Any, Any, Any, Any, Any] = encode(input_schema)
    incoming: Channel[Any, Any, Any, Any, Any, Any, Any] = decode_unknown(output_schema)
    return outgoing.pipe_to(channel).pipe_to(incoming)