No project description provided
Project description
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.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dsi_bitstream-0.2.0.tar.gz.
File metadata
- Download URL: dsi_bitstream-0.2.0.tar.gz
- Upload date:
- Size: 23.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6a1d2ef2ce23d818e9a0ec8730de5ca479fcc2e289473f8b87898d876cc88bf
|
|
| MD5 |
2b198079970948494d2849efc37d3579
|
|
| BLAKE2b-256 |
4686374250a5e38659830a927e8b0ab333405c2afea6e8971e45b2e808c67775
|
File details
Details for the file dsi_bitstream-0.2.0-cp37-abi3-win_amd64.whl.
File metadata
- Download URL: dsi_bitstream-0.2.0-cp37-abi3-win_amd64.whl
- Upload date:
- Size: 207.3 kB
- Tags: CPython 3.7+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcb362c7897bbad7afb9515e0a014c7a633939c582b565dec876269debf2a34b
|
|
| MD5 |
54dfd483b01e022517b30a611897cc59
|
|
| BLAKE2b-256 |
66d90add646ecad6e368f0877bf90458ce5d93c6758012bc9299cfd89ea20130
|
File details
Details for the file dsi_bitstream-0.2.0-cp37-abi3-win32.whl.
File metadata
- Download URL: dsi_bitstream-0.2.0-cp37-abi3-win32.whl
- Upload date:
- Size: 211.6 kB
- Tags: CPython 3.7+, Windows x86
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d62787aacd84edb0b27b6af029bd1e710b081090b38f9cc4b0e4904c32d6179
|
|
| MD5 |
b618a37d2594b99f6dc54c77be72c851
|
|
| BLAKE2b-256 |
1e276aa092120d320ef93ee0eaaa4dcd507e6343b5016a8f49b7f1487abe078d
|
File details
Details for the file dsi_bitstream-0.2.0-cp37-abi3-musllinux_1_1_x86_64.whl.
File metadata
- Download URL: dsi_bitstream-0.2.0-cp37-abi3-musllinux_1_1_x86_64.whl
- Upload date:
- Size: 559.2 kB
- Tags: CPython 3.7+, musllinux: musl 1.1+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
489c0bcfabc4d6cf9d1f9213f275c0b8f2895db5deb7af6a6151d370a82be3bd
|
|
| MD5 |
3b5137ecd01a422cf313ade41970bb0f
|
|
| BLAKE2b-256 |
25339a7c0d03c666a5fcf58684fce4e67411a10cce2d2cc63d43bee87056e308
|
File details
Details for the file dsi_bitstream-0.2.0-cp37-abi3-musllinux_1_1_i686.whl.
File metadata
- Download URL: dsi_bitstream-0.2.0-cp37-abi3-musllinux_1_1_i686.whl
- Upload date:
- Size: 606.1 kB
- Tags: CPython 3.7+, musllinux: musl 1.1+ i686
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca6d6d8a8555fa27eb89b365b1e104ab65c4d3c198a4e3cedbcebf05a70428b1
|
|
| MD5 |
350131d25bf336274d0d405e51eb795c
|
|
| BLAKE2b-256 |
7a82595c92504007ccaacab5c8b5e7b64ea5728113aadb063fa1e2314a4d2ddf
|
File details
Details for the file dsi_bitstream-0.2.0-cp37-abi3-musllinux_1_1_aarch64.whl.
File metadata
- Download URL: dsi_bitstream-0.2.0-cp37-abi3-musllinux_1_1_aarch64.whl
- Upload date:
- Size: 518.8 kB
- Tags: CPython 3.7+, musllinux: musl 1.1+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40a2cb3156b1aeef4cecbe5f49234eb9efe84dce30db05512094d07ddbc3cb1e
|
|
| MD5 |
a07ab2537004f7f55cbb009bffc3a7d7
|
|
| BLAKE2b-256 |
b64ab2cb4634f181b06db34d77d15658ab46a42d5e7f331b40e11a0b68dbbf18
|
File details
Details for the file dsi_bitstream-0.2.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: dsi_bitstream-0.2.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 317.8 kB
- Tags: CPython 3.7+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a79b46d19faeb7987c3c1d56159045eae6631b1c05ea456092c857d7fcd004a
|
|
| MD5 |
b304430b2bbb5a63ce2dd5b0d1f37f60
|
|
| BLAKE2b-256 |
dce4bed76086c8107f332a6aa2fd4fbff6f371c791198565a144777a4b16700a
|
File details
Details for the file dsi_bitstream-0.2.0-cp37-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: dsi_bitstream-0.2.0-cp37-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 306.3 kB
- Tags: CPython 3.7+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13328b9e05d167b4ff8fb2f7c56c52df51a4bd01301595865bcdf777e01809ff
|
|
| MD5 |
b6b0e03946381bb8a78c253d8d966d24
|
|
| BLAKE2b-256 |
61c98ef99d21c457093f9e793f510bd5bc53f022181672bda2dd360ec7213a7b
|
File details
Details for the file dsi_bitstream-0.2.0-cp37-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: dsi_bitstream-0.2.0-cp37-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 320.2 kB
- Tags: CPython 3.7+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2d5d31463e22efd4247149e2c4a560456a3748e183cdd9475c92df8d18e1315
|
|
| MD5 |
94398247dafefa0e291f383de557bee0
|
|
| BLAKE2b-256 |
9f273cd85ad14e69e19efa1d591d905f18fe84664b52b9782ebe75107356d64e
|