Skip to main content

A sleek Python library for binary data.

Project description

tibs

A sleek Python library for your binary data

PyPI - Version CI badge Docs PyPI - License     Pepy Total Downlods


[!NOTE] The project is currently in alpha. This documentation is incomplete.

Documentation

The full documentation is available on Read the Docs.

Getting started

To install use

pip install tibs

There are pre-built wheels for most configurations - if there are issues then please let me know. Tibs works with Python 3.8 and later.

One way to get to know the library is to start a Python interactive session, import the two main classes, and experiment with some of the example code in the rest of this document.

>>> from tibs import Tibs, Mutibs

A quick tour

tibs is a simple but powerful Python library for creating, interpreting and manipulating binary data. It is 100% written in Rust, and from the same author as the bitstring library.

The two main classes are:

  • Tibs: An immutable sequence of bits.
  • Mutibs: A mutable sequence of bits (pronounced 'mew-tibs').

They are created by class methods starting with from_, for example

>>> a = Tibs.from_bin('0110')
>>> b = Tibs.from_hex('abc')
>>> c = Tibs.from_string('0xfee, 0b11001')
>>> d = Tibs.from_bytes(b'some_byte_data')
>>> e = Tibs.from_random(1000)  # 1000 random bits
>>> f = Tibs.from_u(76, 25)  # Unsigned int stored in 25 bits
>>> g = Tibs.from_f(-0.125, 16)  # A float stored in 16 bits
>>> h = Tibs.from_bools([1, 0, 0])
>>> i = Tibs.from_joined([a, b, c, d, e, f, g, h])

Once created they are just binary data, stored efficiently, and they don't retain any information about how they were created.

The Tibs constructor can also be used to create new instances, and it will delegate to from_string, from_bytes or from_bools. This is often more convenient:

>>> a = Tibs('0b0110')
>>> b = Tibs('0xabc')
>>> c = Tibs('0xfee, 0b11001')
>>> d = Tibs(b'some_byte_data')
>>> h = Tibs([1, 0, 0])

Anything that works in the constructor can also be used in other places where a Tibs is needed. For example, instead of writing

x = b & Tibs.from_hex('0xff0')
if x.starts_with(Tibs.from_bin('0b11')):
    x += Tibs.from_bools([0, 1, 1])

you can write just

x = b & '0xff0'
if x.starts_with('0b11'):
    x += [0, 1, 1]

Note that the binary and hex strings need the 0b and 0x prefixes when not called via from_bin and from_hex.

To get the data out of the Tibs there are similar methods starting with to_

>>> a.to_bin()
'0110'
>>> b.to_hex()
'abc'
>>> d.to_bytes()
b'some_byte_data'
>>> f.to_u()
76
>>> g.to_f()
-0.125

There isn't a to_bools method, but creating a list from the Tibs instance will have the same effect. You can also use Tibs instances as iterators of bits.

Instances of Tibs are immutable. Once created they can't change in value, much like the Python bytes and str types. This allows them to be hashed, stored in sets, used as dictionary keys etc., and also allows various optimizations to be used to make them more efficient. They should be used by default if values don't need to be changed.

This does mean that the standard pieces of advice for working with things like Python strings does apply, and why something like this line:

i = Tibs()
for t in [a, b, c, d, e, f, g, h]:
    i += t  # NOT RECOMMENDED!

is an anti-pattern to avoid as it will create a new instance every time it appends. Use from_joined instead.

For the times when you do need to a mutable container use Mutibs. This can do everything that Tibs can do, except that it's not hashable, so can't be used as a dictionary key, in sets etc. It also has several extra methods that will mutate the value in-place.

>>> m = Mutibs()
>>> m.extend('0xabde')
Mutibs('0xabde')
>>> m.replace([1], [0, 1, 0])
Mutibs('0b01000100010001001001001000100100100100')

Note that mutating methods like extend and replace also return the modified Mutibs instance. This perhaps isn't the most Pythonic of interfaces, but it allows methods to be chained:

>>> m[:32].byte_swap().reverse().to_f()
2.1993814317305072e-18

You can do everything you'd expect with these classes - slicing, boolean operations, shifting, rotating, finding, replacing, setting, reversing etc.

But why is it called tibs?

Because Tibs is Bits backwards (almost), it's distinctive, and the name was available on PyPI. It's got nothing to do with Ethiopian stew.

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

tibs-0.4.0.tar.gz (3.2 MB view details)

Uploaded Source

Built Distributions

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

tibs-0.4.0-cp314-cp314t-win_amd64.whl (295.1 kB view details)

Uploaded CPython 3.14tWindows x86-64

tibs-0.4.0-cp314-cp314t-win32.whl (278.6 kB view details)

Uploaded CPython 3.14tWindows x86

tibs-0.4.0-cp38-abi3-win_amd64.whl (308.2 kB view details)

Uploaded CPython 3.8+Windows x86-64

tibs-0.4.0-cp38-abi3-win32.whl (290.5 kB view details)

Uploaded CPython 3.8+Windows x86

tibs-0.4.0-cp38-abi3-musllinux_1_2_x86_64.whl (501.3 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ x86-64

tibs-0.4.0-cp38-abi3-musllinux_1_2_ppc64le.whl (488.1 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ppc64le

tibs-0.4.0-cp38-abi3-musllinux_1_2_aarch64.whl (473.0 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

tibs-0.4.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (419.8 kB view details)

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

tibs-0.4.0-cp38-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl (446.9 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

tibs-0.4.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (408.0 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

tibs-0.4.0-cp38-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl (443.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ i686manylinux: glibc 2.5+ i686

tibs-0.4.0-cp38-abi3-macosx_11_0_arm64.whl (377.9 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file tibs-0.4.0.tar.gz.

File metadata

  • Download URL: tibs-0.4.0.tar.gz
  • Upload date:
  • Size: 3.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tibs-0.4.0.tar.gz
Algorithm Hash digest
SHA256 5a157eaf995302335c932963026aeebfc12a9d110e8b952f8c77bae306739caa
MD5 fb7a5d97359cceaf62e36d4f442ae45f
BLAKE2b-256 835c1bc45f4446b0af42bf70bb7eedb0abf7b5c010741afe70f404640e15f9e3

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp314-cp314t-win_amd64.whl.

File metadata

  • Download URL: tibs-0.4.0-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 295.1 kB
  • Tags: CPython 3.14t, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tibs-0.4.0-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 b701a55e84fdcb0cf78a71f3121d6767e56ee450df05f747d18039b08862753f
MD5 6bda2805ac48ae59f45772491578dd1f
BLAKE2b-256 44431ab9e9b95a839580be361046d19557cbb443b7950be5dc9c9e73e6cde973

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp314-cp314t-win32.whl.

File metadata

  • Download URL: tibs-0.4.0-cp314-cp314t-win32.whl
  • Upload date:
  • Size: 278.6 kB
  • Tags: CPython 3.14t, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tibs-0.4.0-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 792ad214cd836b62ace653539aa6e2a691d621f1cb368901cd9129cd52b720cd
MD5 fc04d5998864a77eafa7cec4da3774d4
BLAKE2b-256 d429ea89caefb8f47222bee477cfade4fa5f847a2b8aed0e9e7ad42fcc6401db

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: tibs-0.4.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 308.2 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tibs-0.4.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b0074a46d8cc0343210de97ed48677caa296d682fa1d8000e9cee648ecdda206
MD5 2131d4d395bcc2f57ee92aeaed930649
BLAKE2b-256 25204f3878ce4ba1716dd3b48b159cc06a29b603ef5ef2be3f7776cd4d92c523

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp38-abi3-win32.whl.

File metadata

  • Download URL: tibs-0.4.0-cp38-abi3-win32.whl
  • Upload date:
  • Size: 290.5 kB
  • Tags: CPython 3.8+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tibs-0.4.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 1fbc390baa5090e4cd72610630cce278a96b5bed4ef4e0a8e2a3b57c74ea919f
MD5 702c5ae0d7163479008a102d0899a7ce
BLAKE2b-256 2b8540ba0425f82a7569e35959494b4251f404252d2197976674bdd3b2fade03

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

  • Download URL: tibs-0.4.0-cp38-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 501.3 kB
  • Tags: CPython 3.8+, musllinux: musl 1.2+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tibs-0.4.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4e956d497a8bb408058f5ca4b50127405d5e35c8db3c0fa1cad07c1fac9394c2
MD5 f8d90550938326c703e78d587c60d81a
BLAKE2b-256 386100ce51e364d8ead5267daf13c642aa8df60480d2420422f45665adca35c2

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp38-abi3-musllinux_1_2_ppc64le.whl.

File metadata

  • Download URL: tibs-0.4.0-cp38-abi3-musllinux_1_2_ppc64le.whl
  • Upload date:
  • Size: 488.1 kB
  • Tags: CPython 3.8+, musllinux: musl 1.2+ ppc64le
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tibs-0.4.0-cp38-abi3-musllinux_1_2_ppc64le.whl
Algorithm Hash digest
SHA256 a0d5dada24a59e1082c1357e04425e1cc12c988f9229dba7489b5f91eb11d382
MD5 660c3c2b806e068d97a1ea3cc367c46a
BLAKE2b-256 60f615f089aac44248a9336ae7e431690e7119fba5537cb8aeef924a1d6816ed

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tibs-0.4.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 19458e32659acbc158414609bc089ba7c1dea60b68b8dd04534299b449b12a9f
MD5 345ea417d35c5010cc422182c90144ad
BLAKE2b-256 8525604524aab4db776e1d0e39bddfe1e75f6fddbe175ed937661819351e63f0

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for tibs-0.4.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 3b79a4fd47807413234710ff355d7c3fae13a09a54ea5f791748051afda41e58
MD5 6887297db7e47de6c78737eb26f85795
BLAKE2b-256 db868c77e21b4cbfc719d9d225fb9f806fd1280f1af37d5298f523af0277a34e

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp38-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl.

File metadata

File hashes

Hashes for tibs-0.4.0-cp38-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl
Algorithm Hash digest
SHA256 aff0dd46d8b7d28810443eb7ff2c3a77ae4f69a81880bc0944a7568ebac094fb
MD5 9be2cdca02e14dce3aed1d5a5196739e
BLAKE2b-256 b63cfb7e19f4b6c1f58877333147cc27b622066f4498b214baf3cf3195ecc792

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for tibs-0.4.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 302856ab388c95ec0904cd750fba1c01ec13014851c36a8fdf12e3f02f8b9094
MD5 d9a6fff326de3046d01c5bb96b259321
BLAKE2b-256 051b5256113cc1fd16c0cf19da7a761466d699c203e5c490ed9d261a9d08d62f

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp38-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl.

File metadata

File hashes

Hashes for tibs-0.4.0-cp38-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl
Algorithm Hash digest
SHA256 878d7fb67f1028dd05ed7667ad5473349b5f80dd13c21258cd2d523b13bac7e9
MD5 6010c86c8cae29adcf95624d25e64f96
BLAKE2b-256 1c110461e5d3deb3a854beb9e2b2eb4c621127ef87a61eb3f23df732a977b4b3

See more details on using hashes here.

File details

Details for the file tibs-0.4.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: tibs-0.4.0-cp38-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 377.9 kB
  • Tags: CPython 3.8+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tibs-0.4.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c8e3449ad79085f34be36bce5eb922a78acb9b82db7dec725544fb756d6b8da0
MD5 03c6a6f0ffe1366a9600cd20f2ca4714
BLAKE2b-256 d85a3c0aa3907ca8a54605b9bd18a7afcb604db45150d21bb9df294f18a1caa5

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