Skip to content

Commit

Permalink
run ruff --select UP --select I --fix src
Browse files Browse the repository at this point in the history
  • Loading branch information
zariiii9003 committed Oct 8, 2023
1 parent fbbf30b commit bf64067
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

import setuptools


if platform.python_implementation() == "CPython":
ext_modules = [
setuptools.Extension(
"bitstruct.c",
sources=["src/bitstruct/c.c", "src/bitstruct/bitstream.c"],
sources=[
"src/bitstruct/c.c",
"src/bitstruct/bitstream.c",
],
)
]

Expand Down
42 changes: 18 additions & 24 deletions src/bitstruct/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
__version__ = '8.17.0'

import binascii
import re
import struct
from io import BytesIO
import binascii


class Error(Exception):
pass


class _Info(object):
class _Info:

def __init__(self, size, name=None):
self.size = size
Expand All @@ -21,7 +21,7 @@ def __init__(self, size, name=None):
class _SignedInteger(_Info):

def __init__(self, size, name):
super(_SignedInteger, self).__init__(size, name)
super().__init__(size, name)
self.minimum = -2 ** (size - 1)
self.maximum = -self.minimum - 1

Expand Down Expand Up @@ -55,18 +55,15 @@ def unpack(self, bits):
class _UnsignedInteger(_Info):

def __init__(self, size, name):
super(_UnsignedInteger, self).__init__(size, name)
super().__init__(size, name)
self.maximum = 2 ** size - 1

def pack(self, arg):
value = int(arg)

if value < 0 or value > self.maximum:
raise Error(
'"u{}" requires 0 <= integer <= {} (got {})'.format(
self.size,
self.maximum,
arg))
f'"u{self.size}" requires 0 <= integer <= {self.maximum} (got {arg})')

return bin(value + (1 << self.size))[3:]

Expand All @@ -77,10 +74,10 @@ def unpack(self, bits):
class _Boolean(_UnsignedInteger):

def pack(self, arg):
return super(_Boolean, self).pack(int(bool(arg)))
return super().pack(int(bool(arg)))

def unpack(self, bits):
return bool(super(_Boolean, self).unpack(bits))
return bool(super().unpack(bits))


class _Float(_Info):
Expand All @@ -95,8 +92,7 @@ def pack(self, arg):
elif self.size == 64:
value = struct.pack('>d', value)
else:
raise Error('expected float size of 16, 32, or 64 bits (got {})'.format(
self.size))
raise Error(f'expected float size of 16, 32, or 64 bits (got {self.size})')

return bin(int(b'01' + binascii.hexlify(value), 16))[3:]

Expand All @@ -110,8 +106,7 @@ def unpack(self, bits):
elif self.size == 64:
value = struct.unpack('>d', packed)[0]
else:
raise Error('expected float size of 16, 32, or 64 bits (got {})'.format(
self.size))
raise Error(f'expected float size of 16, 32, or 64 bits (got {self.size})')

return value

Expand Down Expand Up @@ -153,7 +148,7 @@ def pack(self):
class _Text(_Info):

def __init__(self, size, name, encoding, errors):
super(_Text, self).__init__(size, name)
super().__init__(size, name)
self.encoding = encoding
self.errors = errors

Expand All @@ -178,7 +173,7 @@ def _parse_format(fmt, names, text_encoding, text_errors):
parsed_infos = re.findall(r'([<>]?)([a-zA-Z])(\d+)(\s*)', fmt)

if ''.join([''.join(info) for info in parsed_infos]) != fmt:
raise Error("bad format '{}'".format(fmt + byte_order))
raise Error(f"bad format '{fmt + byte_order}'")

# Use big endian as default and use the endianness of the previous
# value if none is given for the current value.
Expand All @@ -194,7 +189,7 @@ def _parse_format(fmt, names, text_encoding, text_errors):
size = int(parsed_info[2])

if size == 0:
raise Error("bad format '{}'".format(fmt + byte_order))
raise Error(f"bad format '{fmt + byte_order}'")

if names is None:
name = i
Expand Down Expand Up @@ -224,7 +219,7 @@ def _parse_format(fmt, names, text_encoding, text_errors):
elif type_ == 'P':
info = _OnePadding(size)
else:
raise Error("bad char '{}' in format".format(type_))
raise Error(f"bad char '{type_}' in format")

info.endianness = endianness

Expand All @@ -246,7 +241,7 @@ def _unpack_bytearray(size, bits):
return binascii.unhexlify(hex(int('10000000' + bits, 2))[4:].rstrip('L'))


class _CompiledFormat(object):
class _CompiledFormat:

def __init__(self,
fmt,
Expand Down Expand Up @@ -362,8 +357,7 @@ def pack_into_any(self, buf, offset, data, **kwargs):

if len(bits) > len(buf_bits):
raise Error(
'pack_into requires a buffer of at least {} bits'.format(
len(bits)))
f'pack_into requires a buffer of at least {len(bits)} bits')

buf[:] = _unpack_bytearray(len(bits), bits)

Expand All @@ -385,7 +379,7 @@ class CompiledFormat(_CompiledFormat):
"""

def __init__(self, fmt, text_encoding='utf-8', text_errors='strict'):
super(CompiledFormat, self).__init__(fmt, None, text_encoding, text_errors)
super().__init__(fmt, None, text_encoding, text_errors)
self._number_of_arguments = 0

for info in self._infos:
Expand Down Expand Up @@ -448,7 +442,7 @@ def pack(self, data):
try:
return self.pack_any(data)
except KeyError as e:
raise Error('{} not found in data dictionary'.format(str(e)))
raise Error(f'{str(e)} not found in data dictionary')

def unpack(self, data, allow_truncated=False):
"""See :func:`~bitstruct.unpack_dict()`.
Expand All @@ -465,7 +459,7 @@ def pack_into(self, buf, offset, data, **kwargs):
try:
self.pack_into_any(buf, offset, data, **kwargs)
except KeyError as e:
raise Error('{} not found in data dictionary'.format(str(e)))
raise Error(f'{str(e)} not found in data dictionary')

def unpack_from(self, data, offset=0, allow_truncated=False):
"""See :func:`~bitstruct.unpack_from_dict()`.
Expand Down

0 comments on commit bf64067

Please sign in to comment.