Skip to content

Commit

Permalink
Merge pull request #33 from mdekstrand/tweak/ruff
Browse files Browse the repository at this point in the history
Format and lint with ruff
  • Loading branch information
mdekstrand authored Nov 10, 2023
2 parents fcbabfe + 4758d19 commit 93fe6c5
Show file tree
Hide file tree
Showing 18 changed files with 208 additions and 227 deletions.
17 changes: 0 additions & 17 deletions .github/flake8-matcher.json

This file was deleted.

44 changes: 13 additions & 31 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
types: [created,published]
pull_request:

concurrency:
group: test-${{github.ref}}
cancel-in-progress: true

jobs:
lint:
name: Check Source Style
Expand All @@ -17,38 +21,16 @@ jobs:
with:
fetch-depth: 0

- name: Set up Python ${{matrix.python}}
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Prep Pip caching
id: pip-cache
run: |
echo "::set-output name=dir::$(pip cache dir)"
shell: bash

- name: Cache Pip wheels
uses: actions/cache@v1
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: py38-lint-pip-${{ hashFiles('*.egg-info/requires.txt')}}

- name: Install environment
run: |
pip install -U flit
- name: Install package
run: |
flit install --pth-file --extras dev
- name: Run lint
- name: Check source code
run: |
# Flake8 problem matcher & transform regex from https://github.com/TrueBrain/actions-flake8
echo "::add-matcher::.github/flake8-matcher.json"
set -o pipefail
flake8 |sed -r 's/: ([^W][0-9][0-9][0-9])/: error: \1/;s/: (W[0-9][0-9][0-9])/: warning: \1/'
echo "::remove-matcher owner=flake8::"
status=0
if ! pipx run ruff check --output-format=github binpickle; then
status=1
fi
pipx run ruff format --diff binpickle
if [ $status -ne 0 ]; then
exit $status
fi
test:
name: Test with Python ${{matrix.python}} on ${{matrix.platform}}
Expand Down
4 changes: 2 additions & 2 deletions binpickle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Optimized format for pickling binary data.
"""

__version__ = '0.4.0'
__version__ = "0.4.0"

from .write import dump, BinPickler # noqa: F401
from .write import dump, BinPickler # noqa: F401
from .read import load, BinPickleFile # noqa: F401
6 changes: 3 additions & 3 deletions binpickle/codecs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ def get_codec(name, config):
if name is None:
return Null()
elif name in CODECS:
_log.debug('configuring %s: %s', name, config)
_log.debug("configuring %s: %s", name, config)
return CODECS[name](**config)
else:
raise ValueError(f'unknown codec {name}')
raise ValueError(f"unknown codec {name}")


from .chain import Chain # noqa: E402
from .chain import Chain # noqa: E402

register(Null)
register(Chain)
Expand Down
35 changes: 18 additions & 17 deletions binpickle/codecs/blosc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def _split_blocks(buf, blocksize):
if buf.itemsize > 1:
buf = buf.cast('B')
buf = buf.cast("B")
length = buf.nbytes
chunks = []
for start in range(0, length, blocksize):
Expand All @@ -21,7 +21,7 @@ def _split_blocks(buf, blocksize):
chunks.append(buf[start:end])

if not chunks:
chunks.append(memoryview(b''))
chunks.append(memoryview(b""))
return chunks


Expand All @@ -30,13 +30,12 @@ class Blosc(Codec):
Blosc codec.
"""

NAME = 'blosc'
AVAILABLE = find_spec('blosc') is not None
NAME = "blosc"
AVAILABLE = find_spec("blosc") is not None

def __init__(self, name='blosclz', level=9,
shuffle=1, blocksize=DEFAULT_BLOCKSIZE):
def __init__(self, name="blosclz", level=9, shuffle=1, blocksize=DEFAULT_BLOCKSIZE):
if not self.AVAILABLE:
raise ImportError('blosc is not available')
raise ImportError("blosc is not available")
self.name = name
self.level = level
self.shuffle = shuffle
Expand All @@ -45,22 +44,28 @@ def __init__(self, name='blosclz', level=9,
def encode_to(self, buf, out):
# We have to encode by chunks
import blosc

pack = msgpack.Packer()
mv = memoryview(buf)
_log.debug('encoding %d bytes (itemsize=%d, format=%s)',
mv.nbytes, mv.itemsize, mv.format)
_log.debug('splitting with block size %d', self.blocksize)
_log.debug("encoding %d bytes (itemsize=%d, format=%s)", mv.nbytes, mv.itemsize, mv.format)
_log.debug("splitting with block size %d", self.blocksize)
blocks = _split_blocks(mv, self.blocksize)
out.write(pack.pack_array_header(len(blocks)))
for block in blocks:
assert block.nbytes <= self.blocksize
comp = blosc.compress(block, cname=self.name, clevel=self.level,
shuffle=self.shuffle, typesize=mv.itemsize)
comp = blosc.compress(
block,
cname=self.name,
clevel=self.level,
shuffle=self.shuffle,
typesize=mv.itemsize,
)
out.write(pack.pack(comp))
block.release()

def decode_to(self, buf, out):
import blosc

blocks = msgpack.unpackb(buf, use_list=True)
pos = 0
for block in blocks:
Expand All @@ -77,8 +82,4 @@ def decode_to(self, buf, out):
del out[pos:]

def config(self):
return {
'name': self.name,
'level': self.level,
'shuffle': self.shuffle
}
return {"name": self.name, "level": self.level, "shuffle": self.shuffle}
7 changes: 3 additions & 4 deletions binpickle/codecs/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Chain(Codec):
Codec that chains together other codecs in sequence. The codecs are applied
in the provided order for encoding, and reverse order for decoding.
"""
NAME = 'chain'

NAME = "chain"

def __init__(self, codecs=()):
self.codecs = [make_codec(c, list_is_tuple=True) for c in codecs]
Expand All @@ -31,6 +32,4 @@ def decode_to(self, buf, out):
out[:] = self.decode(buf)

def config(self):
return {
'codecs': [(c.NAME, c.config()) for c in self.codecs]
}
return {"codecs": [(c.NAME, c.config()) for c in self.codecs]}
6 changes: 2 additions & 4 deletions binpickle/codecs/gz.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class GZ(Codec):
Zlib (gzip-compatible) codec.
"""

NAME = 'gz'
NAME = "gz"

def __init__(self, level=9):
self.level = level
Expand All @@ -27,6 +27,4 @@ def decode_to(self, buf, out):
out[:] = self.decode(buf)

def config(self):
return {
'level': self.level
}
return {"level": self.level}
3 changes: 2 additions & 1 deletion binpickle/codecs/null.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class Null(Codec):
"""
Null codec (passthrough).
"""
NAME = 'null'

NAME = "null"

def encode(self, buf):
return buf
Expand Down
7 changes: 5 additions & 2 deletions binpickle/codecs/numcodecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def is_numcodec(codec):
"Test whether a codec is a NumCodecs codec."
if NC.AVAILABLE:
import numcodecs

return isinstance(codec, numcodecs.abc.Codec)
else:
return False # if numcodecs aren't available, it can't be one
Expand All @@ -15,12 +16,14 @@ class NC(Codec):
"""
NumCodec wrapper.
"""
NAME = 'numcodec'
AVAILABLE = find_spec('numcodecs') is not None

NAME = "numcodec"
AVAILABLE = find_spec("numcodecs") is not None

def __init__(self, codec=None, **kwargs):
if codec is None:
import numcodecs

self.codec = numcodecs.get_codec(kwargs)
else:
self.codec = codec
Expand Down
20 changes: 11 additions & 9 deletions binpickle/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import struct
from typing import NamedTuple

MAGIC = b'BPCK'
MAGIC = b"BPCK"
VERSION = 1
HEADER_FORMAT = struct.Struct('!4sHHq')
TRAILER_FORMAT = struct.Struct('!QLL')
HEADER_FORMAT = struct.Struct("!4sHHq")
TRAILER_FORMAT = struct.Struct("!QLL")


class FileHeader(NamedTuple):
Expand All @@ -21,6 +21,7 @@ class FileHeader(NamedTuple):
3. File length (8 bytes, big-endian). Length is signed; if the file length is not known,
this field is set to -1.
"""

version: int = VERSION
"The NumPy file version."
length: int = -1
Expand All @@ -35,11 +36,11 @@ def decode(cls, buf, *, verify=True):
"Decode a file header from bytes."
m, v, pad, off = HEADER_FORMAT.unpack(buf)
if verify and m != MAGIC:
raise ValueError('invalid magic {}'.format(m))
raise ValueError("invalid magic {}".format(m))
if verify and v != VERSION:
raise ValueError('invalid version {}'.format(v))
raise ValueError("invalid version {}".format(v))
if verify and pad != 0:
raise ValueError('invalid padding')
raise ValueError("invalid padding")
return cls(v, off)

@classmethod
Expand All @@ -52,7 +53,7 @@ def trailer_pos(self):
if self.length >= HEADER_FORMAT.size + TRAILER_FORMAT.size:
return self.length - TRAILER_FORMAT.size
elif self.length > 0:
raise ValueError('file size {} not enough for BinPickle'.format(self.length))
raise ValueError("file size {} not enough for BinPickle".format(self.length))
else:
return None # We do not know the file size

Expand All @@ -79,14 +80,15 @@ def encode(self):
@classmethod
def decode(cls, buf, *, verify=True):
"Decode a file trailer from bytes."
o, l, c = TRAILER_FORMAT.unpack(buf)
return cls(o, l, c)
off, len, ck = TRAILER_FORMAT.unpack(buf)
return cls(off, len, ck)


class IndexEntry(NamedTuple):
"""
Index entry for a buffer in the BinPickle index.
"""

offset: int
"The position in the file where the buffer begins (bytes)."
enc_length: int
Expand Down
Loading

0 comments on commit 93fe6c5

Please sign in to comment.