Module bare.number

Classes

class Bool (value: T)

A boolean BARE type, encoded as a single byte.

Expand source code
class Bool(NumberMixin[bool]):
    """
    A boolean BARE type, encoded as a single byte.
    """

    def pack(self) -> bytes:
        return struct.pack("<?", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> Bool:
        buf = fp.read(struct.calcsize("<?"))
        return cls(struct.unpack("<?", buf)[0])  # type: ignore

    @classmethod
    def validate(cls, value: bool) -> bool:
        return isinstance(value, (bool, cls))

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> Bool
def validate(value: bool) ‑> bool

Methods

def pack(self) ‑> bytes
class F32 (value: T)

A single-precision floating point BARE type.

An example:

x = F32(123.5)
x.pack()  # prints b'÷B'
fp = io.Bytes(x.pack())
F64.unpack(fp)  # => F64(123.5)
Expand source code
class F32(NumberMixin[float]):
    """
    A single-precision floating point BARE type.

    An example:

    ```
    x = F32(123.5)
    x.pack()  # prints b'\x00\x00\xf7B'
    fp = io.Bytes(x.pack())
    F64.unpack(fp)  # => F64(123.5)
    ```
    """

    def pack(self) -> bytes:
        return struct.pack("<f", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> F32:
        buf = fp.read(struct.calcsize("<f"))
        return cls(struct.unpack("<f", buf)[0])  # type: ignore

    @classmethod
    def validate(cls, value: float) -> bool:
        if isinstance(value, cls):
            return True
        try:
            float(value)
            return True
        except (ValueError, TypeError):
            return False

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> F32
def validate(value: float) ‑> bool

Methods

def pack(self) ‑> bytes
class F64 (value: T)

A double-precision floating point BARE type.

An example:

x = F64(123.5)
x.pack()  # prints b'à^@'
fp = io.Bytes(x.pack())
F64.unpack(fp)  # => F64(123.5)
Expand source code
class F64(NumberMixin[float]):
    """
    A double-precision floating point BARE type.

    An example:
    ```
    x = F64(123.5)
    x.pack()  # prints b'\x00\x00\x00\x00\x00\xe0^@'
    fp = io.Bytes(x.pack())
    F64.unpack(fp)  # => F64(123.5)
    ```
    """

    def pack(self) -> bytes:
        return struct.pack("<d", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> F64:
        buf = fp.read(struct.calcsize("<d"))
        return cls(struct.unpack("<d", buf)[0])

    @classmethod
    def validate(cls, value: Any) -> bool:
        if isinstance(value, cls):
            return True
        try:
            float(value)
            return True
        except (ValueError, TypeError):
            return False

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> F64
def validate(value: Any) ‑> bool

Methods

def pack(self) ‑> bytes
class I16 (value: T)

A signed 16-bit integer BARE type.

Expand source code
class I16(NumberMixin[int]):
    """
    A signed 16-bit integer BARE type.
    """

    def pack(self) -> bytes:
        return struct.pack("<h", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> I16:
        buf = fp.read(struct.calcsize("<h"))
        return cls(struct.unpack("<h", buf)[0])  # type: ignore

    @classmethod
    def validate(cls, value: int) -> bool:
        if isinstance(value, cls):
            return True
        try:
            value = int(value)
            return -(1 << 15) <= value and value < (1 << 15)
        except (ValueError, TypeError):
            return False

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> I16
def validate(value: int) ‑> bool

Methods

def pack(self) ‑> bytes
class I32 (value: T)

A signed 32-bit integer BARE type.

Expand source code
class I32(NumberMixin[int]):
    """
    A signed 32-bit integer BARE type.
    """

    def pack(self) -> bytes:
        return struct.pack("<l", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> I32:
        buf = fp.read(struct.calcsize("<l"))
        return cls(struct.unpack("<l", buf)[0])  # type: ignore

    @classmethod
    def validate(cls, value: int) -> bool:
        if isinstance(value, cls):
            return True
        try:
            value = int(value)
            return -(1 << 31) <= value and value < (1 << 31)
        except (ValueError, TypeError):
            return False

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> I32
def validate(value: int) ‑> bool

Methods

def pack(self) ‑> bytes
class I64 (value: T)

A signed 64-bit integer BARE type.

Expand source code
class I64(NumberMixin[int]):
    """
    A signed 64-bit integer BARE type.
    """

    def pack(self) -> bytes:
        return struct.pack("<q", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> I64:
        buf = fp.read(struct.calcsize("<q"))
        return cls(struct.unpack("<q", buf)[0])  # type: ignore

    @classmethod
    def validate(cls, value: int) -> bool:
        if isinstance(value, cls):
            return True
        try:
            value = int(value)
            return -(1 << 63) <= value and value < (1 << 63)
        except (ValueError, TypeError):
            return False

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> I64
def validate(value: int) ‑> bool

Methods

def pack(self) ‑> bytes
class I8 (value: T)

A signed 8-bit integer BARE type.

Expand source code
class I8(NumberMixin[int]):
    """
    A signed 8-bit integer BARE type.
    """

    def pack(self) -> bytes:
        return struct.pack("<b", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> I8:
        buf = fp.read(struct.calcsize("<b"))
        return cls(struct.unpack("<b", buf)[0])  # type: ignore

    @classmethod
    def validate(cls, value: int) -> bool:
        if isinstance(value, cls):
            return True
        try:
            value = int(value)
            return -(1 << 7) <= value and value < (1 << 7)
        except (ValueError, TypeError):
            return False

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> I8
def validate(value: int) ‑> bool

Methods

def pack(self) ‑> bytes
class Int (value: T)

An unsigned varint BARE type. When serialized, it encodes itself as a LEB128 unsigned varint.

An example:

x = Int(-10)
x.pack()  # prints b''
fp = io.Bytes(x.pack())
Int.unpack(fp)  # => UInt(-10)
Expand source code
class Int(NumberMixin[int]):
    """
    An unsigned varint BARE type. When serialized, it encodes itself
    as a LEB128 unsigned varint.

    An example:

    ```
    x = Int(-10)
    x.pack()  # prints b'\x14'
    fp = io.Bytes(x.pack())
    Int.unpack(fp)  # => UInt(-10)
    ```
    """

    def pack(self) -> bytes:
        buf = io.BytesIO()
        _write_varint(buf, self.value, True)
        return buf.getvalue()

    @classmethod
    def unpack(cls, fp: BinaryIO) -> Int:
        return cls(_read_varint(fp, True))

    @classmethod
    def validate(cls, value: int) -> bool:
        if isinstance(value, cls):
            return True
        try:
            value = int(value)
            return -(1 << 63) <= value and value <= (1 << 63)
        except (ValueError, TypeError):
            return False

    def __hash__(self):
        return hash(self.__class__) + hash(self.value)

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> Int
def validate(value: int) ‑> bool

Methods

def pack(self) ‑> bytes
class U16 (value: T)

An unsigned 16-bit integer BARE type.

Expand source code
class U16(NumberMixin[int]):
    """
    An unsigned 16-bit integer BARE type.
    """

    def pack(self) -> bytes:
        return struct.pack("<H", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> U16:
        buf = fp.read(struct.calcsize("<H"))
        return cls(struct.unpack("<H", buf)[0])  # type: ignore

    @classmethod
    def validate(cls, value: int) -> bool:
        if isinstance(value, cls):
            return True
        try:
            value = int(value)
            return 0 <= value and value < (1 << 16)
        except (ValueError, TypeError):
            return False

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> U16
def validate(value: int) ‑> bool

Methods

def pack(self) ‑> bytes
class U32 (value: T)

An unnsigned 32-bit integer BARE type.

Expand source code
class U32(NumberMixin[int]):
    """
    An unnsigned 32-bit integer BARE type.
    """

    def pack(self) -> bytes:
        return struct.pack("<L", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> U32:
        buf = fp.read(struct.calcsize("<L"))
        return cls(struct.unpack("<L", buf)[0])  # type: ignore

    @classmethod
    def validate(cls, value: int) -> bool:
        if isinstance(value, cls):
            return True
        try:
            value = int(value)
            return 0 <= value and value < (1 << 32)
        except (ValueError, TypeError):
            return False

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> U32
def validate(value: int) ‑> bool

Methods

def pack(self) ‑> bytes
class U64 (value: T)

An unsigned 64-bit integer BARE type.

Expand source code
class U64(NumberMixin[int]):
    """
    An unsigned 64-bit integer BARE type.
    """

    def pack(self) -> bytes:
        return struct.pack("<Q", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> U64:
        buf = fp.read(struct.calcsize("<Q"))
        return cls(struct.unpack("<Q", buf)[0])  # type: ignore

    @classmethod
    def validate(cls, value: int) -> bool:
        if isinstance(value, cls):
            return True
        try:
            value = int(value)
            return 0 <= value and value < (1 << 64)
        except (ValueError, TypeError):
            return False

    def __hash__(self):
        return hash(self.__class__)

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> U64
def validate(value: int) ‑> bool

Methods

def pack(self) ‑> bytes
class U8 (value: T)

An unsigned 8-bit integer BARE type.

Expand source code
class U8(NumberMixin[int]):
    """
    An unsigned 8-bit integer BARE type.
    """

    def pack(self) -> bytes:
        return struct.pack("<B", self.value)

    @classmethod
    def unpack(cls, fp: BinaryIO) -> U8:
        buf = fp.read(struct.calcsize("<B"))
        return cls(struct.unpack("<B", buf)[0])  # type: ignore

    @classmethod
    def validate(cls, value: int) -> bool:
        if isinstance(value, cls):
            return True
        try:
            value = int(value)
            return 0 <= value and value < (1 << 8)
        except (ValueError, TypeError):
            return False

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> U8
def validate(value: int) ‑> bool

Methods

def pack(self) ‑> bytes
class UInt (value: T)

An unsigned varint BARE type. When serialized, it encodes itself as a LEB128 unsigned varint.

An example:
```
x = UInt(10)
x.pack()  # prints b'

' fp = io.Bytes(x.pack()) UInt.unpack(fp) # => UInt(10) ```

Expand source code
class UInt(NumberMixin[int], BAREType[int]):
    """
    An unsigned varint BARE type. When serialized, it encodes itself
    as a LEB128 unsigned varint.

    An example:
    ```
    x = UInt(10)
    x.pack()  # prints b'\x0a'
    fp = io.Bytes(x.pack())
    UInt.unpack(fp)  # => UInt(10)
    ```
    """

    def pack(self) -> bytes:
        buf = io.BytesIO()
        _write_varint(buf, self.value, False)
        return buf.getvalue()

    @classmethod
    def unpack(cls, fp: BinaryIO) -> UInt:
        return cls(_read_varint(fp, False))

    @classmethod
    def validate(cls, value: int) -> bool:
        if isinstance(value, cls):
            return True
        try:
            value = int(value)
            return 0 <= value and value < (1 << 64)
        except (ValueError, TypeError):
            return False

    def __hash__(self):
        return hash(self.__class__)

Ancestors

  • bare.number.NumberMixin
  • BAREType
  • typing.Protocol
  • typing.Generic

Static methods

def unpack(fp: BinaryIO) ‑> UInt
def validate(value: int) ‑> bool

Methods

def pack(self) ‑> bytes