Skip to content

Commit

Permalink
Moving DtypeName to the Dtype base class and giving DtypeTuple's all …
Browse files Browse the repository at this point in the history
…the name 'tuple'.
  • Loading branch information
scott-griffiths committed Feb 5, 2025
1 parent 9c40b74 commit f335840
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
1 change: 1 addition & 0 deletions bitformat/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class DtypeName(Enum):
BITS = 'bits' # Bits object
BOOL = 'bool' # boolean
PAD = 'pad' # padding
TUPLE = 'tuple' # tuple of dtypes

def __str__(self):
return self.value
Expand Down
25 changes: 8 additions & 17 deletions bitformat/_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Dtype(abc.ABC):
"""

_name: DtypeName
_endianness: Endianness

def __new__(cls, token: str | None = None, /) -> Dtype:
Expand Down Expand Up @@ -106,6 +107,11 @@ def unpack(self, b: BitsType, /):
"""
...

@property
def name(self) -> DtypeName:
"""An Enum giving the name of the data type."""
return self._name

@property
def endianness(self) -> Endianness:
"""The endianness of the data type."""
Expand Down Expand Up @@ -191,14 +197,8 @@ def __len__(self):

class DtypeSingle(Dtype):

_name: DtypeName
_size: int

@property
def name(self) -> DtypeName:
"""An Enum giving the name of the data type."""
return self._name

@classmethod
@functools.lru_cache(CACHE_SIZE)
def _create(cls, definition: DtypeDefinition, size: int,
Expand Down Expand Up @@ -336,15 +336,9 @@ def bit_length(self) -> int:

class DtypeArray(Dtype):

_name: DtypeName
_size: int
_items: int | None

@property
def name(self) -> DtypeName:
"""An Enum giving the name of the data type."""
return self._name

@classmethod
@functools.lru_cache(CACHE_SIZE)
def _create(cls, definition: DtypeDefinition, size: int, items: int = 1,
Expand Down Expand Up @@ -632,6 +626,7 @@ def __new__(cls, s: str) -> DtypeTuple:
def from_params(cls, dtypes: Sequence[Dtype | str]) -> DtypeTuple:
x = super().__new__(cls)
x._dtypes = []
x._name = DtypeName.TUPLE
for d in dtypes:
dtype = d if isinstance(d, Dtype) else Dtype.from_string(d)
if dtype.bit_length == 0:
Expand All @@ -654,11 +649,7 @@ def pack(self, values: Sequence[Any]) -> bitformat.Bits:
raise ValueError(f"Expected {len(self)} values, but got {len(values)}.")
return bitformat.Bits.from_joined(dtype.pack(value) for dtype, value in zip(self._dtypes, values))

def unpack(
self,
b: bitformat.Bits | str | Iterable[Any] | bytearray | bytes | memoryview,
/,
) -> tuple[tuple[Any] | Any]:
def unpack(self, b: bitformat.Bits | str | Iterable[Any] | bytearray | bytes | memoryview, /) -> tuple[tuple[Any] | Any]:
"""Unpack a Bits to find its value.
The b parameter should be a Bits of the appropriate length, or an object that can be converted to a Bits.
Expand Down

0 comments on commit f335840

Please # to comment.