jam.types.base.choices package

Submodules

Module Contents

class jam.types.base.choices.Choice(initial: Dict[str, Codable[T]] | Codable[T])[source]

Bases: Codable[T], JsonSerde, Generic[T]

A choice is a value that can be one of several possible types.

A Choice represents a tagged union type that can hold a value of one of several possible Codable types. The actual type is determined by a tag byte during encoding/decoding.

To use a choice, you need to define all possible types:
>>> @decodable_choice([U8, U16])
>>> class MyChoice(Choice): ...
>>> my_choice: MyChoice = MyChoice(U8(1))
>>> assert my_choice.type == U8
>>> assert my_choice.value == U8(1)
To use a optional choice, we’d pair it with Nullable:
>>> @decodable_choice([U8, Nullable])
>>> class OptionalU8(Choice): ...
>>> my_choice: OptionalU8 = OptionalU8(U8(1))
>>> assert my_choice.type == U8
>>> assert my_choice.value == U8(1)
>>> my_choice: OptionalU8 = OptionalU8(Null)
>>> assert my_choice.type == Nullable
>>> assert my_choice.value is None
To use this as an enum:
>>> @decodable_choice([String, String, String])
>>> class OutputType(Choice): ...
__init__(initial: Dict[str, Codable[T]] | Codable[T])[source]

Initialize Choice.

Parameters:

initial – Mapping of initial choice name and its value. Should have only one key.

Raises:

ValueError – If types list is empty

__set_internal__(value: Dict[str, Codable[T]] | Codable[T]) None[source]

Set the choice value.

Parameters:

value – Value to set. Must be instance of one of the allowed types.

Raises:

ValueError – If value type is not in allowed types list

__get__() Codable[T] | None[source]

Get the current value.

Returns:

Current value or None if not set

__eq__(other: object) bool[source]

Compare for equality.

__bool__() bool[source]

Check if the choice has a value.

__repr__() str[source]

Get string representation.

classmethod from_json(data: Any) Choice[T][source]

Create from JSON representation.

class jam.types.base.choices.Option(initial: Codable = Null)[source]

Bases: Choice

An option is a choice that can be either None or a value.

__init__(initial: Codable = Null)[source]

Initialize Choice.

Parameters:

initial – Mapping of initial choice name and its value. Should have only one key.

Raises:

ValueError – If types list is empty

static option_to_choice(value: Codable | Nullable) Dict[str, Codable][source]
classmethod from_json(data: Any) Option[source]

Create from JSON representation.

to_json() Any[source]

Convert to JSON representation.

jam.types.base.choices.decodable_choice(cls: Type[Choice]) Type[Choice][source]
jam.types.base.choices.decodable_option(optional_type: Type[Codable]) Type[Option][source]

Decodable choice