dsi-bitstream-py
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
Release files for dsi-bitstream 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dsi_bitstream-0.3.0.tar.gz | 26.5 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| dsi_bitstream-0.3.0-cp37-abi3-win_amd64.whl | CPython 3.7 | abi3 | Windows x86-64 | Details |
| dsi_bitstream-0.3.0-cp37-abi3-win32.whl | CPython 3.7 | abi3 | Windows x86-32 | Details |
| dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_x86_64.whl | CPython 3.7 | abi3 | Linux musl 1.1+ x86-64 | Details |
| dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_i686.whl | CPython 3.7 | abi3 | Linux musl 1.1+ x86-32 | Details |
| dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_aarch64.whl | CPython 3.7 | abi3 | Linux musl 1.1+ ARM64 | Details |
| dsi_bitstream-0.3.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.7 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| dsi_bitstream-0.3.0-cp37-abi3-macosx_11_0_arm64.whl | CPython 3.7 | abi3 | macOS 11.0+ ARM64 | Details |
| dsi_bitstream-0.3.0-cp37-abi3-macosx_10_12_x86_64.whl | CPython 3.7 | abi3 | macOS 10.12+ x86-64 | Details |
Total release size: 3.4 MB
Release files / dsi_bitstream-0.3.0.tar.gz
| Download URL | dsi_bitstream-0.3.0.tar.gz |
|---|---|
| Size | 26.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4e46c6016d84323837e3390f62f519119133e83db8ba7ddfbe26eb0878e20ec4
|
|
BLAKE2b-256 checksum How to use checksums |
7f0fedd5e379e54d0104160a13d3ca72622e35bff6ff0b628be79ece7995359f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / dsi_bitstream-0.3.0-cp37-abi3-win_amd64.whl
| Download URL | dsi_bitstream-0.3.0-cp37-abi3-win_amd64.whl |
|---|---|
| Size | 241.1 kB |
| Tags | CPython 3.7 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
6a6ac7247421b235bc4aee72e16daf4b5c83d435f5613696554217d161ccc20c
|
|
BLAKE2b-256 checksum How to use checksums |
c45f694c3e7b49a2acb3db51fce48885c36a2138bd7dd4194f3842fb2c241ef2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / dsi_bitstream-0.3.0-cp37-abi3-win32.whl
| Download URL | dsi_bitstream-0.3.0-cp37-abi3-win32.whl |
|---|---|
| Size | 251.8 kB |
| Tags | CPython 3.7 Windows x86-32 abi3 |
|
SHA-256 checksum How to use checksums |
0fc44d804efad9f9a6bacfb7458d9d194199b2fe5ac9283b6d00ce0f507c2ec6
|
|
BLAKE2b-256 checksum How to use checksums |
9c0061ea17457acc3e6cb8999bc600ef1a3f5c3c78ad5ee57bf588d80d9bd693
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_x86_64.whl
| Download URL | dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_x86_64.whl |
|---|---|
| Size | 595.5 kB |
| Tags | CPython 3.7 Linux musl 1.1+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
81aa13dbb6f45dd5e1bf04e6b2cc918ee2e309b579ef915cb421b83870b59c0a
|
|
BLAKE2b-256 checksum How to use checksums |
e2b380ac478967e6a3c9ab0b893c3d8238f2cffd7409f40785ee5a97317ccd96
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_i686.whl
| Download URL | dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_i686.whl |
|---|---|
| Size | 651.3 kB |
| Tags | CPython 3.7 Linux musl 1.1+ x86-32 abi3 |
|
SHA-256 checksum How to use checksums |
cee81b95413b8090dc1c34ad06cf7afe0ecc02a1a33f01ddfe6a709c23a5e6f6
|
|
BLAKE2b-256 checksum How to use checksums |
69cd6393b8db574d5d5bf3cf47560eebe366feb8a8634f7d046af0f84419671b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_aarch64.whl
| Download URL | dsi_bitstream-0.3.0-cp37-abi3-musllinux_1_1_aarch64.whl |
|---|---|
| Size | 555.2 kB |
| Tags | CPython 3.7 Linux musl 1.1+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
b9325a4c393f2c6847e15e35f0cee677683a83f599ca72ec137cb737c7b21da4
|
|
BLAKE2b-256 checksum How to use checksums |
b48c881e57860d295ba88dc3bcefc3f1907a4d1abcec43df49d01c9c6b819a4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / dsi_bitstream-0.3.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | dsi_bitstream-0.3.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 349.7 kB |
| Tags | CPython 3.7 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
61028ad82ffefcae8f9387cc0a03c8046f0d89b72259fc0c01caa9d100f0ea7d
|
|
BLAKE2b-256 checksum How to use checksums |
9ca45e3623028521013d9f71300d5f8e9be58d2aecde96ae21625b2dabde2e91
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Release files / dsi_bitstream-0.3.0-cp37-abi3-macosx_11_0_arm64.whl
| Download URL | dsi_bitstream-0.3.0-cp37-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 339.2 kB |
| Tags | CPython 3.7 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
f9934c295012aa7bebead5e798d37d34d603864a36bbb141b6a1ed9695be9f57
|
|
BLAKE2b-256 checksum How to use checksums |
f950fa4d39d4144e8b36a588659bd2d7b5b92f67c0b4d192671bcb9a93f9b7ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|
Release files / dsi_bitstream-0.3.0-cp37-abi3-macosx_10_12_x86_64.whl
| Download URL | dsi_bitstream-0.3.0-cp37-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 356.5 kB |
| Tags | CPython 3.7 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
45b04fe0f9eb16bc5a142d17465ccf6305cb16a16fc070e5e90470457b3d798e
|
|
BLAKE2b-256 checksum How to use checksums |
1cb33d723bcb5772eda146582a91de2475137d6b12e44a073e49e4ace8c497e0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
maturin/1.12.6
|