Domain

source: bluebase/core/domain.py


class Domain[source]

Bases: ABC

An abstract base class for domains.

ENDIAN: ClassVar[str] = '<'

The endian format used by the struct module.

enum: ClassVar[int]

The enum value of the domain.

classmethod restore(enum: int, size: int | None = None) → Domain[source]

Restore a domain from its enum value and size.

The size is required only for StringDomain.

classmethod parse(format: str) → Domain[source]

Restore a domain from its format.

Remove the endian prefix, if present, and use the default endian.

abstract property format: strread-only

The format of the domain.

This format is used by the struct module.

property size: intread-only

The size of the domain.

abstractmethod validate(value: bool | int | float | str) → None[source]

Validate the type of a value.

Raises:

TypeError – if the value has an invalid type

abstractmethod encode(value: bool | int | float | str) → bytes[source]

Encode a value into data.

abstractmethod decode(data: bytes) → bool | int | float | str[source]

Decode data into a value.

dataclassclass BoolDomain[source]

Bases: Domain

A domain for bool values.

enum: ClassVar[int] = 0

The enum value of the domain.

property format: strread-only

The format of the domain.

This format is used by the struct module.

validate(value: bool | int | float | str) → None[source]

Validate the type of a value.

Raises:

TypeError – if the value has an invalid type

encode(value: bool | int | float | str) → bytes[source]

Encode a value into data.

decode(data: bytes) → bool[source]

Decode data into a value.

dataclassclass IntDomain[source]

Bases: Domain

A domain for int values.

enum: ClassVar[int] = 1

The enum value of the domain.

property format: strread-only

The format of the domain.

This format is used by the struct module.

validate(value: bool | int | float | str) → None[source]

Validate the type of a value.

Raises:

TypeError – if the value has an invalid type

encode(value: bool | int | float | str) → bytes[source]

Encode a value into data.

decode(data: bytes) → int[source]

Decode data into a value.

dataclassclass FloatDomain[source]

Bases: Domain

A domain for float values.

enum: ClassVar[int] = 2

The enum value of the domain.

property format: strread-only

The format of the domain.

This format is used by the struct module.

validate(value: bool | int | float | str) → None[source]

Validate the type of a value.

Raises:

TypeError – if the value has an invalid type

encode(value: bool | int | float | str) → bytes[source]

Encode a value into data.

decode(data: bytes) → float[source]

Decode data into a value.

dataclassclass StringDomain(length: int)[source]

Bases: Domain

A domain for str values.

Variables:

length (int) – the length of a string

Raises:

ValueError – if the string length is invalid

ENCODING: ClassVar[str] = 'utf-8'

The encoding used for strings.

MAX_LENGTH: ClassVar[int] = 255

The maximum length of a string.

enum: ClassVar[int] = 3

The enum value of the domain.

property format: strread-only

The format of the domain.

This format is used by the struct module.

validate(value: bool | int | float | str) → None[source]

Validate the type and length of a value.

Raises:
  • TypeError – if the value has an invalid type

  • ValueError – if the value length exceeds the domain length

encode(value: bool | int | float | str) → bytes[source]

Encode a value into data.

decode(data: bytes) → str[source]

Decode data into a value.

Raises:

ValueError – if the data length does not match the domain length