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
- class jam.types.base.choices.Option(initial: Codable = Null)[source]
Bases:
ChoiceAn 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