Skip to content

risa.codecs

How InMessage state is written into custom_id characters.

BASE85, the default, is built in and keeps every character printable ASCII. BASE32768 and URO14 come with the [radixly] extra and carry more than twice as many bytes per character.

BASE32768 module-attribute

BASE32768: Final[Codec] = _radixly('w', 'base32768')

15 bits per character, the densest codec; needs the [radixly] extra.

BASE85 module-attribute

BASE85: Final[Codec] = Codec(
    tag="a",
    name="base85",
    encode=_b85_encode,
    decode=_b85_decode,
    max_bytes=_b85_max_bytes,
)

The default codec: printable ASCII, built in, needs no extra.

RADIXLY_EXTRA module-attribute

RADIXLY_EXTRA: Final[str] = 'radixly'

The name of the extra (hikari-risa[radixly]) that BASE32768 and URO14 need.

URO14 module-attribute

URO14: Final[Codec] = _radixly('u', 'uro14')

14 bits per CJK character behind a length prefix; needs the [radixly] extra.

Codec

Codec(
    tag: str,
    name: str,
    encode: Callable[[bytes], str],
    decode: Callable[[str], bytes],
    max_bytes: Callable[[int], int],
)

Bases: Struct

A way of writing bytes as custom_id characters.

Attributes:

  • tag (str) –

    One character, written ahead of the state, that names the codec on the wire.

  • name (str) –

    A human-readable name for messages and logs.

  • encode (Callable[[bytes], str]) –

    Turns bytes into text.

  • decode (Callable[[str], bytes]) –

    Turns that text back into the same bytes.

  • max_bytes (Callable[[int], int]) –

    How many bytes fit into a given number of characters.

by_tag

by_tag(tag: str) -> Codec | None

Return the codec registered under tag, or None.

Parameters:

  • tag (str) –

    The one-character tag a codec writes ahead of its state.

Returns:

  • Codec | None –

    The codec, or None if no codec has the tag.

ensure_available

ensure_available(codec: Codec) -> None

Raise CodecUnavailableError if codec needs an extra that is not installed.

Parameters:

  • codec (Codec) –

    The codec to check. BASE85 always passes.

Raises:

register

register(codec: Codec) -> None

Make codec known under its tag, so state written with it can be read back.

Parameters:

  • codec (Codec) –

    The codec to register, under its one-character tag.

Raises:

  • ValueError –

    If a different codec already has the tag.