Changed formatting

This commit is contained in:
2023-03-15 06:06:58 +01:00
parent b3fedb8214
commit 75824c306f
2 changed files with 252 additions and 182 deletions

View File

@@ -1,6 +1,6 @@
from __future__ import annotations
from typing import Any, List, Union, Optional, Literal, Type
from typing import Any, List, Literal, Optional, Type, Union
# for seek()
SEEK_SET = 0
@@ -30,8 +30,10 @@ def bits_to_int(bits: List[bool], little_endian: bool = False) -> int:
return num
def int_to_bits(number: int, bit_length: int, little_endian: bool = False) -> List[bool]:
bits = [bool(number & (0b1 << i)) for i in range(bit_length-1, -1, -1)]
def int_to_bits(
number: int, bit_length: int, little_endian: bool = False
) -> List[bool]:
bits = [bool(number & (0b1 << i)) for i in range(bit_length - 1, -1, -1)]
return bits[::-1] if little_endian else bits
@@ -56,7 +58,9 @@ class Bitstream:
@staticmethod
def from_int(value: int, bit_length: int) -> Bitstream:
return Bitstream([bool(value & (0b1 << i)) for i in range(bit_length-1, -1, -1)])
return Bitstream(
[bool(value & (0b1 << i)) for i in range(bit_length - 1, -1, -1)]
)
def __len__(self) -> int:
return self._bit_length
@@ -82,7 +86,9 @@ class Bitstream:
mask = 1 << (7 - (bit_index % 8))
return bool(self._bytearray[bit_index >> 3] & mask)
def __setitem__(self, bit_index: int, value: Union[bool, List[bool], bytes, Bitstream]):
def __setitem__(
self, bit_index: int, value: Union[bool, List[bool], bytes, Bitstream]
):
if bit_index >= self._bit_length:
raise IndexError("bit index out of range")
@@ -101,7 +107,9 @@ class Bitstream:
self._bytearray[bit_index >> 3] |= mask
else:
self._bytearray[bit_index >> 3] &= ~mask
elif isinstance(value, Bitstream) or (type(value) is list and all(type(v) is bool for v in value)):
elif isinstance(value, Bitstream) or (
type(value) is list and all(type(v) is bool for v in value)
):
for i in range(value_bit_length):
self[bit_index + i] = value[i]
else:
@@ -164,7 +172,10 @@ class Bitstream:
def __repr__(self) -> str:
return "<Bitstream addr:0x%012x offset:%d len:%d data:'%s'>" % (
id(self), self._offset, len(self), bytes(self).hex(" ")
id(self),
self._offset,
len(self),
bytes(self).hex(" "),
)
@property
@@ -220,11 +231,11 @@ class Bitstream:
return value
def read(
self,
type: Type[Union[bool, bytes, Bitstream, int]],
bit_length: int,
bit_index: Optional[int] = None
) -> Union[List[bool], bytes, Bitstream, int]:
self,
type: Type[Union[bool, bytes, Bitstream, int]],
bit_length: int,
bit_index: Optional[int] = None,
) -> Union[List[bool], bytes, Bitstream, int]:
start = self._offset if bit_index is None else bit_index
if (start + bit_length) > self._bit_length or (start < 0):
@@ -248,7 +259,11 @@ class Bitstream:
raise TypeError("Invalid data type")
def write(self, value: Union[bool, List[bool], bytes, Bitstream], bit_index: Optional[int] = None):
def write(
self,
value: Union[bool, List[bool], bytes, Bitstream],
bit_index: Optional[int] = None,
):
start = self._offset if bit_index is None else bit_index
if start < 0: