Skip to content

Commit

Permalink
add auto style check and auto test (#1)
Browse files Browse the repository at this point in the history
* add auto style check and auto test

* fix typing to support python 3.7 and 3.8
  • Loading branch information
xyb authored Nov 6, 2022
1 parent 9da5ad9 commit 43f49bd
Show file tree
Hide file tree
Showing 8 changed files with 463 additions and 116 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: test

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
strategy:
fail-fast: false
matrix:
python-version: [3.7, 3.8, 3.9, "3.10", 3.11]
os:
- ubuntu
- macOS
#- windows

name: tests
runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true

- run: poetry install --no-interaction --no-root

- name: run tests
run: |
source .venv/bin/activate
pytest --doctest-modules --last-failed --durations=3
lint:
name: pre-commit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-python@v4
with:
python-version: 3.x

- name: install pre-commit
run: pip install pre-commit

- name: pre-commit cache
uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: "${{ hashFiles('.pre-commit-config.yaml') }}"

- run: pre-commit run --all-files --show-diff-on-failure
48 changes: 48 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/codespell-project/codespell
rev: v2.2.2
hooks:
- id: codespell

- repo: https://github.com/asottile/reorder_python_imports
rev: v3.9.0
hooks:
- id: reorder-python-imports
args:
- --py37-plus

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.82
hooks:
- id: lint

- repo: https://github.com/asottile/pyupgrade
rev: v3.2.0
hooks:
- id: pyupgrade
args:
- --py37-plus

- repo: https://github.com/asottile/add-trailing-comma
rev: v2.3.0
hooks:
- id: add-trailing-comma
args:
- --py36-plus

- repo: https://github.com/psf/black
rev: "22.10.0"
hooks:
- id: black

- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.10.1
hooks:
- id: validate-pyproject
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ unittest:
pytest --doctest-modules --last-failed --durations=3

format:
autoflake -i chunksum/*.py
isort chunksum/*.py
pre-commit run --all-files
54 changes: 32 additions & 22 deletions chunksum/cdc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
from typing import List

from fastcdc import fastcdc

Expand All @@ -8,20 +9,25 @@
_default_chunker = None


def get_chunk_func(avg_chunk_size: int = CHUNKER_AVG_CHUNK_SIZE,
min_chunk_size: int = None,
max_chunk_size: int = None) -> callable:
def get_chunk_func(
avg_chunk_size: int = CHUNKER_AVG_CHUNK_SIZE,
min_chunk_size: int = None,
max_chunk_size: int = None,
) -> callable:
def chunk_func(stream):
return fastcdc(stream,
min_size=min_chunk_size,
avg_size=avg_chunk_size,
max_size=max_chunk_size,
fat=True,
hf=None)
return fastcdc(
stream,
min_size=min_chunk_size,
avg_size=avg_chunk_size,
max_size=max_chunk_size,
fat=True,
hf=None,
)

return chunk_func


def default_chunker() -> 'Chunker':
def default_chunker() -> "Chunker":
global _default_chunker

if not _default_chunker:
Expand Down Expand Up @@ -53,29 +59,33 @@ class Chunker:
>>> assert chunk1 + chunk2 == chunks
"""

def __init__(self,
avg_chunk_size: int = CHUNKER_AVG_CHUNK_SIZE,
min_chunk_size: int = None,
max_chunk_size: int = None):
def __init__(
self,
avg_chunk_size: int = CHUNKER_AVG_CHUNK_SIZE,
min_chunk_size: int = None,
max_chunk_size: int = None,
):
self.avg_chunk_size = avg_chunk_size
self._chunker = get_chunk_func(avg_chunk_size=self.avg_chunk_size,
min_chunk_size=min_chunk_size,
max_chunk_size=max_chunk_size)
self._chunker = get_chunk_func(
avg_chunk_size=self.avg_chunk_size,
min_chunk_size=min_chunk_size,
max_chunk_size=max_chunk_size,
)
self._iter = None
self._tail = b''
self._tail = b""

def update(self, message: bytes) -> 'Chunker':
def update(self, message: bytes) -> "Chunker":
self.message = message
self._iter = self._chunker(io.BytesIO(self._tail + message))
return self

@property
def chunks(self) -> list[bytes]:
def chunks(self) -> List[bytes]:
"""Get bytes of chunks."""
if not self._iter:
return

self._tail = b''
self._tail = b""
prev = None
for chunk in self._iter:
content = chunk.data
Expand Down Expand Up @@ -119,5 +129,5 @@ def reset(self) -> bytes:
b'67890'
"""
remaining = self._tail
self._tail = b''
self._tail = b""
return remaining
Loading

0 comments on commit 43f49bd

Please sign in to comment.