Giter VIP home page Giter VIP logo

python-bz3's Introduction

✨ python-bz3 ✨

The python binding for bzip3 with parallel support

pypi python implementation wheel license action

install

pip install bzip3

Usage

from bz3 import compress_file, decompress_file, test_file, compress, decompress
import bz3

with open("test_inp.txt", "rb") as inp, open("compressed.bz3", "wb") as out:
    compress_file(inp, out, 1000 * 1000)

with open("compressed.bz3", "rb") as inp:
    test_file(inp, True)    

with open("compressed.bz3", "rb") as inp, open("output.txt", "wb") as out:
    decompress_file(inp, out)

print(decompress(compress(b"12121")))

with bz3.open("test.bz3", "wt", encoding="utf-8", num_threads=4) as f:
    f.write("test data")

with bz3.open("test.bz3", "rt", encoding="utf-8", num_threads=4) as f:
    print(f.read())
  • use BZ3_USE_CFFI env var to specify a backend
  • num_threads is only available on cython backend which have openmp support

Public functions

from typing import IO, Optional, Union

def compress_file(input: IO, output: IO, block_size: int) -> None: ...
def decompress_file(input: IO, output: IO) -> None: ...
def recover_file(input: IO, output: IO) -> None: ...
def test_file(input: IO, should_raise: bool = ...) -> bool: ...


class BZ3File:
    def __init__(self, filename, mode: str = ..., block_size: int = ..., num_threads: int = ..., ignore_error: bool = False) -> None: ...
    def close(self) -> None: ...
    @property
    def closed(self): ...
    def fileno(self): ...
    def seekable(self): ...
    def readable(self): ...
    def writable(self): ...
    def peek(self, n: int = ...): ...
    def read(self, size: int = ...): ...
    def read1(self, size: int = ...): ...
    def readinto(self, b): ...
    def readline(self, size: int = ...): ...
    def readlines(self, size: int = ...): ...
    def write(self, data): ...
    def writelines(self, seq): ...
    def seek(self, offset, whence=...): ...
    def tell(self): ...

def open(filename, mode: str = ..., block_size: int = ..., encoding: str = ..., errors: str = ..., newline: str = ..., num_threads: int = 1, ignore_error: bool = False) -> BZ3File: ...
def compress(data: bytes, block_size: int = ..., num_threads: int = 1) -> bytes: ...
def decompress(data: bytes, num_threads: int = 1) -> bytes: ...

def libversion() -> str: ... # Get bzip3 version
def bound(inp: int) -> int: ... # Return the recommended size of the output buffer for the compression functions.

# High-level api
# Compress a block of data into out buffer, zerocopy, both parameters accept objects which implements buffer-protocol.
# out must be writabel, size of out must be at least equal to bound(len(inp))
def compress_into(inp: Union[bytes, bytearray], out: bytearray) -> int: ...
# Decompress a block of data into out buffer, zerocopy
def decompress_into(inp: Union[bytes, bytearray], out: bytearray) -> int: ...
  • Note, high-level api won't work with low-level api, see this

python-bz3's People

Contributors

synodriver avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

python-bz3's Issues

About multithreading compress/decompress

In current implemention, the BZ3OmpCompressor will not perform a compress until it's internal buffer is long enough for all threads to perform a compress, which means it has to wait until num_threads blocks are received by calling compress method, otherwise it will just return b"". This is called lazy compress, which guaranteed maximum performance. It's okey for compressor, but for decompressor, things changed. The decompressor just have no idea about when does the stream end. If the decompressor still buffers the input, the caller might thought that the input is not enough for a block decompress and drop it. So it only assume that the input could end at any time. In this way, the decompressor can't buffer the input to perform multi-threaded decompress. It perform a decompress when it's buffer is long enough for one thread to decompress(although it would be better if more blocks are received in the same time), making it degenerate to a single thread decompressor. An effective measure to avoid this is to fill at least num_threads blocks, for BZIP3File usage, you can

from bz3 import compression
compression.BUFFER_SIZE = 300*10**6

to increase the buffer size, making more blocks receive at the same time.

GitHub description misspelling

The GitHub description has a typo, it says "python bingding for bzip3"; I imagine it's supposed to say "python binding for bzip3" or "python bindings for bzip3"

image

poor performance on windows

Why does it so slow compare to the original one, the compress speed is just about 4MB/s, much slower than the origin one. Maybe we should replace bytearray with uint8_t* and manage it by PyMem_Realloc

unittest

Now the unittest needs some extra test file to run. Those files are too big to upload. Should we generate them at runtime?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.