From 12e7ae87e279f7dc05373fed2cb093ad805ad2e1 Mon Sep 17 00:00:00 2001 From: Scott Griffiths Date: Wed, 3 Jan 2024 16:01:00 +0000 Subject: [PATCH] Renaming options.py to bitstring_options.py to avoid name clash with options object. --- bitstring/__init__.py | 2 +- bitstring/array_.py | 10 ++-- bitstring/bitarray_.py | 12 ++--- bitstring/bits.py | 47 +++++++++---------- .../{options.py => bitstring_options.py} | 1 - bitstring/dtypes.py | 3 +- bitstring/methods.py | 38 +++++++++------ 7 files changed, 59 insertions(+), 54 deletions(-) rename bitstring/{options.py => bitstring_options.py} (99%) diff --git a/bitstring/__init__.py b/bitstring/__init__.py index 54e8ab19..04f0308b 100644 --- a/bitstring/__init__.py +++ b/bitstring/__init__.py @@ -62,7 +62,7 @@ import sys from .bits import Bits -from .options import Options +from .bitstring_options import Options from .bitarray_ import BitArray from .bitstream import ConstBitStream, BitStream from .methods import pack diff --git a/bitstring/array_.py b/bitstring/array_.py index e6f80b0a..13e29c50 100644 --- a/bitstring/array_.py +++ b/bitstring/array_.py @@ -8,7 +8,7 @@ from bitstring.bits import Bits, BitsType from bitstring.bitarray_ import BitArray from bitstring.dtypes import Dtype, dtype_register -from bitstring.utils import parse_name_length_token, parse_single_struct_token, preprocess_tokens +from bitstring import utils import copy import array import operator @@ -122,7 +122,7 @@ def _set_dtype(self, new_dtype: Union[str, Dtype]) -> None: try: dtype = Dtype(new_dtype) except ValueError: - name_length = parse_single_struct_token(new_dtype) + name_length = utils.parse_single_struct_token(new_dtype) if name_length is not None: dtype = Dtype(*name_length) else: @@ -258,7 +258,7 @@ def extend(self, iterable: Union[Array, array.array, Iterable]) -> None: self.data.append(iterable.data) elif isinstance(iterable, array.array): # array.array types are always native-endian, hence the '=' - name_value = parse_single_struct_token('=' + iterable.typecode) + name_value = utils.parse_single_struct_token('=' + iterable.typecode) if name_value is None: raise ValueError(f"Cannot extend from array with typecode {iterable.typecode}.") other_dtype = dtype_register.get_dtype(*name_value) @@ -371,10 +371,10 @@ def pp(self, fmt: Optional[str] = None, width: int = 120, dtypes = [self.dtype] parameter_str = f"dtype='{self.dtype}'" else: - token_list = preprocess_tokens(fmt) + token_list = utils.preprocess_tokens(fmt) if len(token_list) not in [1, 2]: raise ValueError(f"Only one or two tokens can be used in an Array.pp() format - '{fmt}' has {len(token_list)} tokens.") - dtypes = [Dtype(*parse_name_length_token(t)) for t in token_list] + dtypes = [Dtype(*utils.parse_name_length_token(t)) for t in token_list] parameter_str = f"fmt='{fmt}'" token_name, token_length = dtypes[0].name, dtypes[0].bitlength diff --git a/bitstring/bitarray_.py b/bitstring/bitarray_.py index 12d5fef6..7a1bf07e 100644 --- a/bitstring/bitarray_.py +++ b/bitstring/bitarray_.py @@ -4,8 +4,8 @@ import numbers import re from collections import abc -from typing import Union, List, Iterable, Any, Optional, Pattern, Dict, Callable -from bitstring.utils import BYTESWAP_STRUCT_PACK_RE, STRUCT_SPLIT_RE, PACK_CODE_SIZE +from typing import Union, List, Iterable, Any, Optional +from bitstring import utils from bitstring.exceptions import CreationError, Error from bitstring.bits import Bits, BitsType, TBits @@ -553,17 +553,17 @@ def byteswap(self, fmt: Optional[Union[int, Iterable[int], str]] = None, start: raise ValueError(f"Improper byte length {fmt}.") bytesizes = [fmt] elif isinstance(fmt, str): - if not (m := BYTESWAP_STRUCT_PACK_RE.match(fmt)): + if not (m := utils.BYTESWAP_STRUCT_PACK_RE.match(fmt)): raise ValueError(f"Cannot parse format string {fmt}.") # Split the format string into a list of 'q', '4h' etc. - formatlist = re.findall(STRUCT_SPLIT_RE, m.group('fmt')) + formatlist = re.findall(utils.STRUCT_SPLIT_RE, m.group('fmt')) # Now deal with multiplicative factors, 4h -> hhhh etc. bytesizes = [] for f in formatlist: if len(f) == 1: - bytesizes.append(PACK_CODE_SIZE[f]) + bytesizes.append(utils.PACK_CODE_SIZE[f]) else: - bytesizes.extend([PACK_CODE_SIZE[f[-1]]] * int(f[:-1])) + bytesizes.extend([utils.PACK_CODE_SIZE[f[-1]]] * int(f[:-1])) elif isinstance(fmt, abc.Iterable): bytesizes = fmt for bytesize in bytesizes: diff --git a/bitstring/bits.py b/bitstring/bits.py index f20f58bc..68884a41 100644 --- a/bitstring/bits.py +++ b/bitstring/bits.py @@ -16,10 +16,7 @@ import bitarray.util import bitstring from bitstring.bitstore import BitStore -from bitstring.bitstore_helpers import float2bitstore, uint2bitstore, ue2bitstore, str_to_bitstore, se2bitstore, \ - bfloat2bitstore, floatle2bitstore, uintbe2bitstore, uintle2bitstore, intbe2bitstore, intle2bitstore, bfloatle2bitstore, \ - bin2bitstore, bin2bitstore_unsafe, hex2bitstore, int2bitstore, oct2bitstore, sie2bitstore, uie2bitstore, \ - e5m2float2bitstore, e4m3float2bitstore +from bitstring import bitstore_helpers # Things that can be converted to Bits when a Bits type is needed @@ -526,7 +523,7 @@ def _clear(self) -> None: def _setauto_no_length_or_offset(self, s: BitsType, /) -> None: """Set bitstring from a bitstring, file, bool, array, iterable or string.""" if isinstance(s, str): - self._bitstore = str_to_bitstore(s) + self._bitstore = bitstore_helpers.str_to_bitstore(s) return if isinstance(s, Bits): self._bitstore = s._bitstore.copy() @@ -623,10 +620,10 @@ def _setbits(self, bs: BitsType, length: None = None) -> None: self._bitstore = bs._bitstore def _sete5m2float(self, f: float, length: None = None) -> None: - self._bitstore = e5m2float2bitstore(f) + self._bitstore = bitstore_helpers.e5m2float2bitstore(f) def _sete4m3float(self, f: float, length: None = None) -> None: - self._bitstore = e4m3float2bitstore(f) + self._bitstore = bitstore_helpers.e4m3float2bitstore(f) def _setbytes(self, data: Union[bytearray, bytes], length:None = None) -> None: """Set the data from a bytes or bytearray object.""" @@ -671,7 +668,7 @@ def _setuint(self, uint: int, length: Optional[int] = None) -> None: length = len(self) if length is None or length == 0: raise bitstring.CreationError("A non-zero length must be specified with a uint initialiser.") - self._bitstore = uint2bitstore(uint, length) + self._bitstore = bitstore_helpers.uint2bitstore(uint, length) def _getuint(self) -> int: """Return data as an unsigned int.""" @@ -686,7 +683,7 @@ def _setint(self, int_: int, length: Optional[int] = None) -> None: length = len(self) if length is None or length == 0: raise bitstring.CreationError("A non-zero length must be specified with an int initialiser.") - self._bitstore = int2bitstore(int_, length) + self._bitstore = bitstore_helpers.int2bitstore(int_, length) def _getint(self) -> int: """Return data as a two's complement signed int.""" @@ -700,7 +697,7 @@ def _setuintbe(self, uintbe: int, length: Optional[int] = None) -> None: length = len(self) if length is None or length == 0: raise bitstring.CreationError("A non-zero length must be specified with a uintbe initialiser.") - self._bitstore = uintbe2bitstore(uintbe, length) + self._bitstore = bitstore_helpers.uintbe2bitstore(uintbe, length) def _getuintbe(self) -> int: """Return data as a big-endian two's complement unsigned int.""" @@ -714,7 +711,7 @@ def _setintbe(self, intbe: int, length: Optional[int] = None) -> None: length = len(self) if length is None or length == 0: raise bitstring.CreationError("A non-zero length must be specified with a intbe initialiser.") - self._bitstore = intbe2bitstore(intbe, length) + self._bitstore = bitstore_helpers.intbe2bitstore(intbe, length) def _getintbe(self) -> int: """Return data as a big-endian two's complement signed int.""" @@ -727,7 +724,7 @@ def _setuintle(self, uintle: int, length: Optional[int] = None) -> None: length = len(self) if length is None or length == 0: raise bitstring.CreationError("A non-zero length must be specified with a uintle initialiser.") - self._bitstore = uintle2bitstore(uintle, length) + self._bitstore = bitstore_helpers.uintle2bitstore(uintle, length) def _getuintle(self) -> int: """Interpret as a little-endian unsigned int.""" @@ -741,7 +738,7 @@ def _setintle(self, intle: int, length: Optional[int] = None) -> None: length = len(self) if length is None or length == 0: raise bitstring.CreationError("A non-zero length must be specified with an intle initialiser.") - self._bitstore = intle2bitstore(intle, length) + self._bitstore = bitstore_helpers.intle2bitstore(intle, length) def _getintle(self) -> int: """Interpret as a little-endian signed int.""" @@ -763,7 +760,7 @@ def _setfloatbe(self, f: float, length: Optional[int] = None) -> None: length = len(self) if length is None or length not in [16, 32, 64]: raise bitstring.CreationError("A length of 16, 32, or 64 must be specified with a float initialiser.") - self._bitstore = float2bitstore(f, length) + self._bitstore = bitstore_helpers.float2bitstore(f, length) def _getfloatbe(self) -> float: """Interpret the whole bitstring as a big-endian float.""" @@ -775,7 +772,7 @@ def _setfloatle(self, f: float, length: Optional[int] = None) -> None: length = len(self) if length is None or length not in [16, 32, 64]: raise bitstring.CreationError("A length of 16, 32, or 64 must be specified with a float initialiser.") - self._bitstore = floatle2bitstore(f, length) + self._bitstore = bitstore_helpers.floatle2bitstore(f, length) def _getfloatle(self) -> float: """Interpret the whole bitstring as a little-endian float.""" @@ -789,7 +786,7 @@ def _getbfloatbe(self) -> float: def _setbfloatbe(self, f: Union[float, str], length: Optional[int] = None) -> None: if length is not None and length != 16: raise bitstring.CreationError(f"bfloats must be length 16, received a length of {length} bits.") - self._bitstore = bfloat2bitstore(f) + self._bitstore = bitstore_helpers.bfloat2bitstore(f) def _getbfloatle(self) -> float: zero_padded = Bits(16) + self @@ -798,7 +795,7 @@ def _getbfloatle(self) -> float: def _setbfloatle(self, f: Union[float, str], length: Optional[int] = None) -> None: if length is not None and length != 16: raise bitstring.CreationError(f"bfloats must be length 16, received a length of {length} bits.") - self._bitstore = bfloatle2bitstore(f) + self._bitstore = bitstore_helpers.bfloatle2bitstore(f) def _setue(self, i: int, length: None = None) -> None: """Initialise bitstring with unsigned exponential-Golomb code for integer i. @@ -810,7 +807,7 @@ def _setue(self, i: int, length: None = None) -> None: raise bitstring.CreationError("Cannot specify a length for exponential-Golomb codes.") if bitstring.options.lsb0: raise bitstring.CreationError("Exp-Golomb codes cannot be used in lsb0 mode.") - self._bitstore = ue2bitstore(i) + self._bitstore = bitstore_helpers.ue2bitstore(i) def _readue(self, pos: int) -> Tuple[int, int]: """Return interpretation of next bits as unsigned exponential-Golomb code. @@ -846,7 +843,7 @@ def _setse(self, i: int, length: None = None) -> None: raise bitstring.CreationError("Cannot specify a length for exponential-Golomb codes.") if bitstring.options.lsb0: raise bitstring.CreationError("Exp-Golomb codes cannot be used in lsb0 mode.") - self._bitstore = se2bitstore(i) + self._bitstore = bitstore_helpers.se2bitstore(i) def _readse(self, pos: int) -> Tuple[int, int]: """Return interpretation of next bits as a signed exponential-Golomb code. @@ -874,7 +871,7 @@ def _setuie(self, i: int, length: None = None) -> None: raise bitstring.CreationError("Cannot specify a length for exponential-Golomb codes.") if bitstring.options.lsb0: raise bitstring.CreationError("Exp-Golomb codes cannot be used in lsb0 mode.") - self._bitstore = uie2bitstore(i) + self._bitstore = bitstore_helpers.uie2bitstore(i) def _readuie(self, pos: int) -> Tuple[int, int]: """Return interpretation of next bits as unsigned interleaved exponential-Golomb code. @@ -904,7 +901,7 @@ def _setsie(self, i: int, length: None = None) -> None: raise bitstring.CreationError("Cannot specify a length for exponential-Golomb codes.") if bitstring.options.lsb0: raise bitstring.CreationError("Exp-Golomb codes cannot be used in lsb0 mode.") - self._bitstore = sie2bitstore(i) + self._bitstore = bitstore_helpers.sie2bitstore(i) def _readsie(self, pos: int) -> Tuple[int, int]: """Return interpretation of next bits as a signed interleaved exponential-Golomb code. @@ -949,11 +946,11 @@ def _setpad(self, value: None, length: int) -> None: def _setbin_safe(self, binstring: str, length: None = None) -> None: """Reset the bitstring to the value given in binstring.""" - self._bitstore = bin2bitstore(binstring) + self._bitstore = bitstore_helpers.bin2bitstore(binstring) def _setbin_unsafe(self, binstring: str, length: None = None) -> None: """Same as _setbin_safe, but input isn't sanity checked. binstring mustn't start with '0b'.""" - self._bitstore = bin2bitstore_unsafe(binstring) + self._bitstore = bitstore_helpers.bin2bitstore_unsafe(binstring) def _getbin(self) -> str: """Return interpretation as a binary string.""" @@ -961,7 +958,7 @@ def _getbin(self) -> str: def _setoct(self, octstring: str, length: None = None) -> None: """Reset the bitstring to have the value given in octstring.""" - self._bitstore = oct2bitstore(octstring) + self._bitstore = bitstore_helpers.oct2bitstore(octstring) def _getoct(self) -> str: """Return interpretation as an octal string.""" @@ -971,7 +968,7 @@ def _getoct(self) -> str: def _sethex(self, hexstring: str, length: None = None) -> None: """Reset the bitstring to have the value given in hexstring.""" - self._bitstore = hex2bitstore(hexstring) + self._bitstore = bitstore_helpers.hex2bitstore(hexstring) def _gethex(self) -> str: """Return the hexadecimal representation as a string. diff --git a/bitstring/options.py b/bitstring/bitstring_options.py similarity index 99% rename from bitstring/options.py rename to bitstring/bitstring_options.py index cb3b366c..121a8960 100644 --- a/bitstring/options.py +++ b/bitstring/bitstring_options.py @@ -2,7 +2,6 @@ import bitstring - class Options: """Internal class to create singleton module options instance.""" diff --git a/bitstring/dtypes.py b/bitstring/dtypes.py index 869eea15..0f91e860 100644 --- a/bitstring/dtypes.py +++ b/bitstring/dtypes.py @@ -4,6 +4,7 @@ from typing import Optional, Dict, Any, Union, Tuple from collections.abc import Iterable import bitstring +from bitstring import utils CACHE_SIZE = 256 @@ -22,7 +23,7 @@ def __new__(cls, token: Union[str, Dtype, None] = None, /, length: Optional[int] def _new_from_token(cls, token: str, length: Optional[int] = None) -> Dtype: token = ''.join(token.split()) if length is None: - name, length = bitstring.utils.parse_name_length_token(token) + name, length = utils.parse_name_length_token(token) else: name = token d = dtype_register.get_dtype(name, length) diff --git a/bitstring/methods.py b/bitstring/methods.py index 0b94310e..c42f07ea 100644 --- a/bitstring/methods.py +++ b/bitstring/methods.py @@ -1,10 +1,18 @@ from __future__ import annotations +from bitstring.bits import Bits +from bitstring.bitstream import BitStream +from bitstring.utils import tokenparser +from bitstring.exceptions import CreationError from typing import Union, List -import bitstring +from bitstring.bitstore import BitStore +from bitstring.bitstore_helpers import bitstore_from_token +from bitstring.dtypes import dtype_register +from bitstring.bitstring_options import Options +options = Options() -def pack(fmt: Union[str, List[str]], *values, **kwargs) -> bitstring.BitStream: +def pack(fmt: Union[str, List[str]], *values, **kwargs) -> BitStream: """Pack the values according to the format string and return a new BitStream. fmt -- A single string or a list of strings with comma separated tokens @@ -44,12 +52,12 @@ def pack(fmt: Union[str, List[str]], *values, **kwargs) -> bitstring.BitStream: fmt = [fmt] try: for f_item in fmt: - _, tkns = bitstring.utils.tokenparser(f_item, tuple(sorted(kwargs.keys()))) + _, tkns = tokenparser(f_item, tuple(sorted(kwargs.keys()))) tokens.extend(tkns) except ValueError as e: - raise bitstring.CreationError(*e.args) + raise CreationError(*e.args) value_iter = iter(values) - bsl: List[bitstring.BitStore] = [] + bsl: List[BitStore] = [] try: for name, length, value in tokens: # If the value is in the kwd dictionary then it takes precedence. @@ -60,7 +68,7 @@ def pack(fmt: Union[str, List[str]], *values, **kwargs) -> bitstring.BitStream: length = kwargs[length] # Also if we just have a dictionary name then we want to use it if name in kwargs and length is None and value is None: - bsl.append(bitstring.BitStream(kwargs[name])._bitstore) + bsl.append(BitStream(kwargs[name])._bitstore) continue if length is not None: length = int(length) @@ -68,25 +76,25 @@ def pack(fmt: Union[str, List[str]], *values, **kwargs) -> bitstring.BitStream: # Take the next value from the ones provided value = next(value_iter) if name == 'bits': - value = bitstring.Bits(value) + value = Bits(value) if length is not None and length != len(value): - raise bitstring.CreationError(f"Token with length {length} packed with value of length {len(value)}.") + raise CreationError(f"Token with length {length} packed with value of length {len(value)}.") bsl.append(value._bitstore) continue - bsl.append(bitstring.bitstore_helpers.bitstore_from_token(name, length, value)) + bsl.append(bitstore_from_token(name, length, value)) except StopIteration: - raise bitstring.CreationError(f"Not enough parameters present to pack according to the " + raise CreationError(f"Not enough parameters present to pack according to the " f"format. {len(tokens)} values are needed.") try: next(value_iter) except StopIteration: # Good, we've used up all the *values. - s = bitstring.BitStream() - if bitstring.options.lsb0: + s = BitStream() + if options.lsb0: for name, _, _ in tokens: - if name in bitstring.dtypes.dtype_register.unknowable_length_names(): - raise bitstring.CreationError(f"Unknown length tokens ('{name}') cannot be used in lsb0 mode.") + if name in dtype_register.unknowable_length_names(): + raise CreationError(f"Unknown length tokens ('{name}') cannot be used in lsb0 mode.") for b in bsl[::-1]: s._bitstore += b else: @@ -94,4 +102,4 @@ def pack(fmt: Union[str, List[str]], *values, **kwargs) -> bitstring.BitStream: s._bitstore += b return s - raise bitstring.CreationError(f"Too many parameters present to pack according to the format. Only {len(tokens)} values were expected.") + raise CreationError(f"Too many parameters present to pack according to the format. Only {len(tokens)} values were expected.")