Skip to main content

A Python bit manipulation library written in Rust.

Project description

tibs

A sleek Python library for your binary data

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


[!NOTE] This library is currently pre-alpha. This documentation is mostly reality but still part planning.

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.append('0xabde')
Mutibs('0xabde')
>>> m.replace([1], [0, 1, 0])
Mutibs('0b01000100010001001001001000100100100100')

Note that mutating methods like append 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.2.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.2.0-cp314-cp314t-win_amd64.whl (288.6 kB view details)

Uploaded CPython 3.14tWindows x86-64

tibs-0.2.0-cp314-cp314t-win32.whl (272.9 kB view details)

Uploaded CPython 3.14tWindows x86

tibs-0.2.0-cp38-abi3-win_amd64.whl (304.7 kB view details)

Uploaded CPython 3.8+Windows x86-64

tibs-0.2.0-cp38-abi3-win32.whl (287.0 kB view details)

Uploaded CPython 3.8+Windows x86

tibs-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl (498.6 kB view details)

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

tibs-0.2.0-cp38-abi3-musllinux_1_2_ppc64le.whl (484.4 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ppc64le

tibs-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl (471.9 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

tibs-0.2.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (416.6 kB view details)

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

tibs-0.2.0-cp38-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl (442.5 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

tibs-0.2.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (406.1 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

tibs-0.2.0-cp38-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl (440.7 kB view details)

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

tibs-0.2.0-cp38-abi3-macosx_11_0_arm64.whl (376.2 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

tibs-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl (403.3 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: tibs-0.2.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.2.0.tar.gz
Algorithm Hash digest
SHA256 658ef791d492514a0422dadf1955863a93f5497091b4c30eaa5c0298ff27a21c
MD5 265f478a872fca5add8c9ed512d3c02a
BLAKE2b-256 9c35be85dd0311868ca22c445bd59b45e2883d162c677b56a869d13ccec682fb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.2.0-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 288.6 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.2.0-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 54b024c871fedba96af0f0565a1556c92efb38897c1e068f746e8c495f2eec7b
MD5 1b65f933bc829943d71aca818f22a9ac
BLAKE2b-256 986379f5075bc5df7bdd7fe57108cf697027d2b064075b91830c41d68d174923

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.2.0-cp314-cp314t-win32.whl
  • Upload date:
  • Size: 272.9 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.2.0-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 0d032927f1647904248888479dda5481bcc363053bc361c361127dacc8f29a86
MD5 f9aee9e6423ed360ae018ba3615b40d5
BLAKE2b-256 378317e85721cb0ba62a11c97c0996de4f864cf5a941961d3976b6034f9a0f78

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.2.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 304.7 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.2.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 16ab6a6dcfb29143d7a74db13af6f8bf8687361d4241552add1ff8203481932e
MD5 20f305ee99fa3bee40b777bc2883c1fb
BLAKE2b-256 8898ea8e2acde9623b9640439b7be773aa252dbe869e7373eb41af00263a5b02

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.2.0-cp38-abi3-win32.whl
  • Upload date:
  • Size: 287.0 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.2.0-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 3cbefdd3ddbba778eee6e65e927740768671d91822d6c30e6b57400a1708ac01
MD5 d6fa8956309bc90f929517297f8682be
BLAKE2b-256 270ecec4cb08a251f1750ef518732cac33bdf4f0098936e7ec2834077fd78433

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.2.0-cp38-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 498.6 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.2.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9f3facf222309b82ee6d2f79d674f2eb30c5110460ea42435803eaaf85145860
MD5 bc8d891a2d294c6a94ed913f8d08f89f
BLAKE2b-256 f9fa98a7cea38d427b54e99029b2e871714a04e1cc03ae9ba202a96377d469b5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.2.0-cp38-abi3-musllinux_1_2_ppc64le.whl
  • Upload date:
  • Size: 484.4 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.2.0-cp38-abi3-musllinux_1_2_ppc64le.whl
Algorithm Hash digest
SHA256 96b1a298569ef1a3ea2cf6eaa23d54eeea4bd71a3faca2e7c5f6070b5d81a371
MD5 d64dc407385b777f8a315775159584a8
BLAKE2b-256 05a82dc1b5e3d3b8894746fd945e3050e6cbf6092924048ea15e4378d45dbbac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tibs-0.2.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 04c3c985e17c216a2836e1a8a29bf263eb392faee08a285086d973ee6b4e7c7b
MD5 654b039ae3d2f20570bf5426bc23fc03
BLAKE2b-256 774787d7a8ec4b07a6d6151e5fa505513730fec25bdc2b979ec115255f055389

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tibs-0.2.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 e0a4c3e565d0ff27ee53b859d902e1452ab06dcc98924fd340dbbf4bdf0b8fd5
MD5 9778621276512e8b4aeab536367e611d
BLAKE2b-256 3bb9bd93d9ce669b6153866df225ae72c4adbdd233bddd95fa3b54ca6a468b19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tibs-0.2.0-cp38-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl
Algorithm Hash digest
SHA256 eb6287d411c903f63004bd4d3176422437a85b71f5e27523419242bfa87e7868
MD5 1a81148ed86492e1204cd7dc9e555905
BLAKE2b-256 444a66e49427146da6ce76f4e75223e1bfacb39e3597f1774f853b6d88c03462

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tibs-0.2.0-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 14f06a21eb583065c29f2c8f3520894778edcb227aa30c3c805aad01ea339f22
MD5 6336e19dd8e9b21f7d326d78170a6e7f
BLAKE2b-256 015a232c5b2fc23a60bcefd9a5ad49d89a6da9e932919779a0081c4501656dff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tibs-0.2.0-cp38-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl
Algorithm Hash digest
SHA256 257eaf28c37f7ad1df771469168811e848e931576bf8bcd6639e9a67462598de
MD5 21ee42ec3d4c7621f2fbd5c73d13d186
BLAKE2b-256 e12a267b7d362eaa596a8d93658d0a008e912c10233c03fb889dd5326b8a0162

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.2.0-cp38-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 376.2 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.2.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1ec466ca251c7f94a28f12dee0ce16688076d02d28da7c0a6badef6cf352b755
MD5 539641c7e863149b1ab081c1df63ac25
BLAKE2b-256 91a1983d57479868f08621f4333e2e2f6a0e08755e15ef74e01809df134959b8

See more details on using hashes here.

File details

Details for the file tibs-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tibs-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 286261f8da8a58e0b33e12048adee8ced7b9480c5eef4ea0133268774bbec906
MD5 590a007960d83809112a68ed298b965b
BLAKE2b-256 dcef9c098624ca038e3b7e5aca04384f649d7f520f3dc0195bce46ba55d64b2a

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