Skip to main content

No project description provided

Project description

dsi-bitstream-py

GitHub CI license Supported Python versions Pypi total project downloads Pypi project

Python bindings for dsi-bitstream-rs, a Rust implementation of read/write bit streams supporting several types of instantaneous codes.

Installation

pip install dsi_bitstream

Usage

Reading and writing codes

from dsi_bitstream import BitWriterLittleEndian, BitReaderLittleEndian

writer = BitWriterLittleEndian("./bitstream.bin")

# All write methods return the number of bits written.
writer.write_bits(10, n=5)         # write 10 as 5 raw bits
writer.write_unary(100)
writer.write_gamma(10)
writer.write_delta(2)
writer.write_omega(7)
writer.write_rice(3, k=4)
writer.write_golomb(4, b=10)
writer.write_zeta(10, k=3)
writer.write_pi(42, k=2)
writer.write_exp_golomb(100, k=3)
writer.write_minimal_binary(10, max=100)
writer.flush()

reader = BitReaderLittleEndian("./bitstream.bin")
assert reader.read_bits(n=5) == 10
assert reader.read_unary() == 100
assert reader.read_gamma() == 10
assert reader.read_delta() == 2
assert reader.read_omega() == 7
assert reader.read_rice(k=4) == 3
assert reader.read_golomb(b=10) == 4
assert reader.read_zeta(k=3) == 10
assert reader.read_pi(k=2) == 42
assert reader.read_exp_golomb(k=3) == 100
assert reader.read_minimal_binary(max=100) == 10

Seeking is supported on the reader:

pos = reader.bit_pos()   # bits from the start of the file
reader.set_bit_pos(pos)  # seek back

Big-endian variants are available as BitReaderBigEndian / BitWriterBigEndian.

Analyzing codes with CodesStats

CodesStats records a stream of non-negative integers and computes the total bit cost for every supported code, so you can pick the most compact one:

from dsi_bitstream import CodesStats

stats = CodesStats()
for value in data:
    stats.update(value)

# Best code and its total bit cost.
code, bits = stats.best_code()   # e.g. ("Zeta(3)", 48120)

# Full ranking, cheapest first.
for code, bits in stats.get_codes():
    print(f"{code:>20s}: {bits} bits")

# Query a specific code.
bits = stats.bits_for("Delta")   # returns None if out of tracked range

# Merge stats from parallel workers.
combined = stats_a + stats_b

Field-level access is available via properties: total, unary, gamma, delta, omega, vbyte, zeta, golomb, exp_golomb, rice, pi. The array properties (zeta, golomb, etc.) return a list of bit costs, one per parameter value.

Dynamic code dispatch with Code

Code wraps the Rust Codes enum, letting you select a code at runtime and use it to read, write, or compute bit lengths:

from dsi_bitstream import Code, BitWriterBigEndian, BitReaderBigEndian

code = Code.zeta(3)

w = BitWriterBigEndian("out.bin")
bits = code.write(w, 42)  # returns number of bits written
w.flush()

r = BitReaderBigEndian("out.bin")
val = code.read(r)        # returns 42

Available constructors: Code.unary(), Code.gamma(), Code.delta(), Code.omega(), Code.vbyte_le(), Code.vbyte_be(), Code.zeta(k), Code.pi(k), Code.golomb(b), Code.exp_golomb(k), Code.rice(log2_b).

Parse from strings: Code.parse("Zeta(3)"). Equivalent codes compare equal: Code.zeta(1) == Code.gamma(). Use code.canonicalize() to normalize.

Code length functions

Compute the bit length of a code for a given value without writing to a stream:

from dsi_bitstream import len_gamma, len_zeta, len_delta

len_gamma(42)      # 11
len_zeta(100, 3)   # 11
len_delta(7)       # 8

Also available: len_unary, len_omega, len_pi, len_rice, len_golomb, len_exp_golomb, len_minimal_binary.

The same is available via Code.len():

code = Code.zeta(3)
code.len(100)  # 11 -- same as len_zeta(100, 3)

Building

With Nix (recommended)

The repository includes a flake.nix with two package outputs:

# Native wheel (linux tag, for local use)
nix build .#default.dist

# manylinux2014 wheel (PyPI-uploadable, uses zig as linker)
nix build .#manylinux.dist

# in either cases the wheel will be in:
ls result-dist/dsi_bitstream-*.whl

The manylinux wheel is built with maturin --zig, which links against glibc 2.17 headers shipped by zig, and verified with auditwheel during the build.

A dev shell is also available:

nix develop
maturin develop  # build & install in-place for development

Without Nix

pip install maturin
maturin develop          # development build
maturin build --release  # release wheel

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dsi_bitstream-0.3.0.tar.gz (26.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

dsi_bitstream-0.3.0-cp37-abi3-win_amd64.whl (241.1 kB view details)

Uploaded CPython 3.7+Windows x86-64

dsi_bitstream-0.3.0-cp37-abi3-win32.whl (251.8 kB view details)

Uploaded CPython 3.7+Windows x86

dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_x86_64.whl (595.5 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.1+ x86-64

dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_i686.whl (651.3 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.1+ i686

dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_aarch64.whl (555.2 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.1+ ARM64

dsi_bitstream-0.3.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (349.7 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ x86-64

dsi_bitstream-0.3.0-cp37-abi3-macosx_11_0_arm64.whl (339.2 kB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

dsi_bitstream-0.3.0-cp37-abi3-macosx_10_12_x86_64.whl (356.5 kB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

Details for the file dsi_bitstream-0.3.0.tar.gz.

File metadata

  • Download URL: dsi_bitstream-0.3.0.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for dsi_bitstream-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4e46c6016d84323837e3390f62f519119133e83db8ba7ddfbe26eb0878e20ec4
MD5 6f64a38503a846fddeb9199f83854c5f
BLAKE2b-256 7f0fedd5e379e54d0104160a13d3ca72622e35bff6ff0b628be79ece7995359f

See more details on using hashes here.

File details

Details for the file dsi_bitstream-0.3.0-cp37-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for dsi_bitstream-0.3.0-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6a6ac7247421b235bc4aee72e16daf4b5c83d435f5613696554217d161ccc20c
MD5 96541774413be4e50d4ab15aaa760d14
BLAKE2b-256 c45f694c3e7b49a2acb3db51fce48885c36a2138bd7dd4194f3842fb2c241ef2

See more details on using hashes here.

File details

Details for the file dsi_bitstream-0.3.0-cp37-abi3-win32.whl.

File metadata

  • Download URL: dsi_bitstream-0.3.0-cp37-abi3-win32.whl
  • Upload date:
  • Size: 251.8 kB
  • Tags: CPython 3.7+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for dsi_bitstream-0.3.0-cp37-abi3-win32.whl
Algorithm Hash digest
SHA256 0fc44d804efad9f9a6bacfb7458d9d194199b2fe5ac9283b6d00ce0f507c2ec6
MD5 9050c6d27b883e5f783ef7d55fa285f4
BLAKE2b-256 9c0061ea17457acc3e6cb8999bc600ef1a3f5c3c78ad5ee57bf588d80d9bd693

See more details on using hashes here.

File details

Details for the file dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 81aa13dbb6f45dd5e1bf04e6b2cc918ee2e309b579ef915cb421b83870b59c0a
MD5 6ff82758bad688b8532d01e8c716e838
BLAKE2b-256 e2b380ac478967e6a3c9ab0b893c3d8238f2cffd7409f40785ee5a97317ccd96

See more details on using hashes here.

File details

Details for the file dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_i686.whl.

File metadata

File hashes

Hashes for dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_i686.whl
Algorithm Hash digest
SHA256 cee81b95413b8090dc1c34ad06cf7afe0ecc02a1a33f01ddfe6a709c23a5e6f6
MD5 5069c884de7f8ddb818c01eb285001d2
BLAKE2b-256 69cd6393b8db574d5d5bf3cf47560eebe366feb8a8634f7d046af0f84419671b

See more details on using hashes here.

File details

Details for the file dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_aarch64.whl.

File metadata

File hashes

Hashes for dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 b9325a4c393f2c6847e15e35f0cee677683a83f599ca72ec137cb737c7b21da4
MD5 8a5fae4bb8bc7229b8dbc8d1fb32dde8
BLAKE2b-256 b48c881e57860d295ba88dc3bcefc3f1907a4d1abcec43df49d01c9c6b819a4b

See more details on using hashes here.

File details

Details for the file dsi_bitstream-0.3.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dsi_bitstream-0.3.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 61028ad82ffefcae8f9387cc0a03c8046f0d89b72259fc0c01caa9d100f0ea7d
MD5 251e214c267790e0128a88f4af2c91e1
BLAKE2b-256 9ca45e3623028521013d9f71300d5f8e9be58d2aecde96ae21625b2dabde2e91

See more details on using hashes here.

File details

Details for the file dsi_bitstream-0.3.0-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dsi_bitstream-0.3.0-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f9934c295012aa7bebead5e798d37d34d603864a36bbb141b6a1ed9695be9f57
MD5 2e537cd3b1e41f57f81a32d38b2227d6
BLAKE2b-256 f950fa4d39d4144e8b36a588659bd2d7b5b92f67c0b4d192671bcb9a93f9b7ca

See more details on using hashes here.

File details

Details for the file dsi_bitstream-0.3.0-cp37-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dsi_bitstream-0.3.0-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 45b04fe0f9eb16bc5a142d17465ccf6305cb16a16fc070e5e90470457b3d798e
MD5 9c542774c7185d6337c637d46133936f
BLAKE2b-256 1cb33d723bcb5772eda146582a91de2475137d6b12e44a073e49e4ace8c497e0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page