Skip to content

Commit

Permalink
Renaming options.py to bitstring_options.py to avoid name clash with …
Browse files Browse the repository at this point in the history
…options object.
  • Loading branch information
scott-griffiths committed Jan 3, 2024
1 parent 5641da4 commit 12e7ae8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 54 deletions.
2 changes: 1 addition & 1 deletion bitstring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions bitstring/array_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions bitstring/bitarray_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
47 changes: 22 additions & 25 deletions bitstring/bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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."""
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -949,19 +946,19 @@ 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."""
return self._bitstore.slice_to_bin()

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."""
Expand All @@ -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.
Expand Down
1 change: 0 additions & 1 deletion bitstring/options.py → bitstring/bitstring_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import bitstring


class Options:
"""Internal class to create singleton module options instance."""

Expand Down
3 changes: 2 additions & 1 deletion bitstring/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
38 changes: 23 additions & 15 deletions bitstring/methods.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -60,38 +68,38 @@ 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)
if value is None and name != 'pad':
# 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:
for b in bsl:
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.")

0 comments on commit 12e7ae8

Please sign in to comment.