Encoding

These are helpers used to encode/decode encoded strings to something else like bytes.

Base classes

aiostem.utils.encoding.T = TypeVar(T, bound=bytes | int)

Invariant TypeVar bound to typing.Union[bytes, int].

Generic type used for our encoders.

class aiostem.utils.encoding.EncoderProtocol[source]

Bases: Protocol, Generic[T]

Protocol for encoding from and decoding data to another type.

classmethod decode(data: str) T[source]

Decode the data using the encoder.

Parameters:

data (str) – A string that can be decoded to type T.

Returns:

TypeVar(T, bound= bytes | int) – The newly decoded type.

classmethod encode(value: T) str[source]

Encode the provided value using the encoder.

Parameters:

value (TypeVar(T, bound= bytes | int)) – A generic value of type T.

Returns:

str – The exact value encoded to a string.

classmethod get_json_format() str[source]

Get the JSON format for the encoded data.

Returns:

str – A short descriptive name for the format.

class aiostem.utils.encoding.EncodedBase[source]

Bases: Generic[T]

Generic encoded value to/from a string using the EncoderProtocol.

CORE_SCHEMA: ClassVar[InvalidSchema | AnySchema | NoneSchema | BoolSchema | IntSchema | FloatSchema | DecimalSchema | StringSchema | BytesSchema | DateSchema | TimeSchema | DatetimeSchema | TimedeltaSchema | LiteralSchema | MissingSentinelSchema | EnumSchema | IsInstanceSchema | IsSubclassSchema | CallableSchema | ListSchema | TupleSchema | SetSchema | FrozenSetSchema | GeneratorSchema | DictSchema | AfterValidatorFunctionSchema | BeforeValidatorFunctionSchema | WrapValidatorFunctionSchema | PlainValidatorFunctionSchema | WithDefaultSchema | NullableSchema | UnionSchema | TaggedUnionSchema | ChainSchema | LaxOrStrictSchema | JsonOrPythonSchema | TypedDictSchema | ModelFieldsSchema | ModelSchema | DataclassArgsSchema | DataclassSchema | ArgumentsSchema | ArgumentsV3Schema | CallSchema | CustomErrorSchema | JsonSchema | UrlSchema | MultiHostUrlSchema | DefinitionsSchema | DefinitionReferenceSchema | UuidSchema | ComplexSchema]

Main core validation schema.

CORE_TYPE: ClassVar[type[Any]]

Core python type associated with the schema.

encoder: type[EncoderProtocol[TypeVar(T, bound= bytes | int)]]

The encoder protocol to use.

when_used: Literal['always', 'unless-none', 'json', 'json-unless-none']

When to use the encoder.

from_string(string: str) T[source]

Decode a string to the underlying type.

Return type:

TypeVar(T, bound= bytes | int)

to_string(value: T) str[source]

Encode the value using the specified encoder.

Return type:

str

Byte encoders

class aiostem.utils.encoding.EncodedBytes[source]

Bases: EncodedBase[bytes]

Bytes that can be encoded and decoded from a string using an external encoder.

CORE_SCHEMA: ClassVar[InvalidSchema | AnySchema | NoneSchema | BoolSchema | IntSchema | FloatSchema | DecimalSchema | StringSchema | BytesSchema | DateSchema | TimeSchema | DatetimeSchema | TimedeltaSchema | LiteralSchema | MissingSentinelSchema | EnumSchema | IsInstanceSchema | IsSubclassSchema | CallableSchema | ListSchema | TupleSchema | SetSchema | FrozenSetSchema | GeneratorSchema | DictSchema | AfterValidatorFunctionSchema | BeforeValidatorFunctionSchema | WrapValidatorFunctionSchema | PlainValidatorFunctionSchema | WithDefaultSchema | NullableSchema | UnionSchema | TaggedUnionSchema | ChainSchema | LaxOrStrictSchema | JsonOrPythonSchema | TypedDictSchema | ModelFieldsSchema | ModelSchema | DataclassArgsSchema | DataclassSchema | ArgumentsSchema | ArgumentsV3Schema | CallSchema | CustomErrorSchema | JsonSchema | UrlSchema | MultiHostUrlSchema | DefinitionsSchema | DefinitionReferenceSchema | UuidSchema | ComplexSchema] = {'strict': True, 'type': 'bytes'}

Our core schema is for bytes.

CORE_TYPE

Core python type associated with the schema.

alias of bytes

class aiostem.utils.encoding.Base16Encoder[source]

Bases: EncoderProtocol[bytes]

Specific encoder for hex encoded strings.

classmethod decode(data: str) bytes[source]

Decode the provided hex string to original bytes data.

Parameters:

data (str) – A hex-encoded string to decode.

Raises:

PydanticCustomError – On decoding error.

Returns:

bytes – The corresponding decoded bytes.

classmethod encode(value: bytes) str[source]

Encode a value to a hex encoded string.

Parameters:

value (bytes) – A byte value to encode to a hexadecimal string.

Returns:

str – The corresponding encoded string value.

classmethod get_json_format() Literal['base16'][source]

Get the JSON format for the encoded data.

Return type:

Literal['base16']

class aiostem.utils.encoding.Base32Encoder[source]

Bases: EncoderProtocol[bytes]

Encoder for base32 bytes.

casefold: ClassVar[bool] = True

Whether we are case insensitive when decoding.

trim_padding: ClassVar[bool] = True

Whether to remove the padding characters when serializing.

classmethod decode(data: str) bytes[source]

Decode the provided base32 bytes to original bytes data.

Parameters:

data (str) – A base32-encoded string to decode.

Raises:

PydanticCustomError – On decoding error.

Returns:

bytes – The corresponding decoded bytes.

classmethod encode(value: bytes) str[source]

Encode a value to a base32 encoded string.

Parameters:

value (bytes) – A byte value to encode to base32.

Returns:

str – The corresponding encoded string value.

classmethod get_json_format() Literal['base32'][source]

Get the JSON format for the encoded data.

Return type:

Literal['base32']

class aiostem.utils.encoding.Base64Encoder[source]

Bases: EncoderProtocol[bytes]

Encoder for base64 bytes.

trim_padding: ClassVar[bool] = True

Whether to remove the padding characters when serializing.

classmethod decode(data: str) bytes[source]

Decode the provided base64 bytes to original bytes data.

Parameters:

data (str) – A base64-encoded string to decode.

Raises:

PydanticCustomError – On decoding error.

Returns:

bytes – The corresponding decoded bytes.

classmethod encode(value: bytes) str[source]

Encode a value to a base64 encoded string.

Parameters:

value (bytes) – A byte value to encode to base64.

Returns:

str – The corresponding encoded string value.

classmethod get_json_format() Literal['base64'][source]

Get the JSON format for the encoded data.

Return type:

Literal['base64']