Source code for mtc.base.numerical
import io
from typing import Self
from .parser import Parser
from .utils import int_to_bytes, bytes_to_int
[docs]
class Integer(Parser):
"""Base class for handling unsigned integers"""
size_in_bytes: int
def __init__(self, /, value: int) -> None:
self.value = value
[docs]
def to_bytes(self) -> bytes:
return int_to_bytes(self.value, self.size_in_bytes)
[docs]
@classmethod
def parse(cls, stream: io.BufferedIOBase) -> Self:
return cls(bytes_to_int(stream.read(cls.size_in_bytes)))
[docs]
@classmethod
def skip(cls, stream: io.BufferedIOBase) -> None:
stream.seek(cls.size_in_bytes, io.SEEK_CUR)
[docs]
def print(self) -> str:
return f"{self.size_in_bytes} {self.__class__.__name__} {self.value}"
[docs]
def validate(self) -> None:
if not 0 <= self.value <= 2 ** (8 * self.size_in_bytes) - 1:
raise self.ValidationError(f"{self.value} cannot fit into a uint{self.size_in_bytes}")
[docs]
class UInt8(Integer):
size_in_bytes = 1
[docs]
class UInt16(Integer):
size_in_bytes = 2
[docs]
class UInt32(Integer):
size_in_bytes = 4
[docs]
class UInt64(Integer):
size_in_bytes = 8