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.3.1.tar.gz (3.1 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.3.1-cp314-cp314t-win_amd64.whl (279.7 kB view details)

Uploaded CPython 3.14tWindows x86-64

tibs-0.3.1-cp314-cp314t-win32.whl (266.3 kB view details)

Uploaded CPython 3.14tWindows x86

tibs-0.3.1-cp38-abi3-win_amd64.whl (293.1 kB view details)

Uploaded CPython 3.8+Windows x86-64

tibs-0.3.1-cp38-abi3-win32.whl (277.7 kB view details)

Uploaded CPython 3.8+Windows x86

tibs-0.3.1-cp38-abi3-musllinux_1_2_x86_64.whl (486.8 kB view details)

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

tibs-0.3.1-cp38-abi3-musllinux_1_2_ppc64le.whl (473.2 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ppc64le

tibs-0.3.1-cp38-abi3-musllinux_1_2_aarch64.whl (461.0 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

tibs-0.3.1-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (405.2 kB view details)

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

tibs-0.3.1-cp38-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl (432.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ppc64le

tibs-0.3.1-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (395.5 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

tibs-0.3.1-cp38-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl (430.3 kB view details)

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

tibs-0.3.1-cp38-abi3-macosx_11_0_arm64.whl (363.8 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for tibs-0.3.1.tar.gz
Algorithm Hash digest
SHA256 403dbb62f786a88dc3d96fa68b36351a3e4b7ad71293c7fddfbfb30455d3c3a3
MD5 fd37cf781397e698aa517f4050bb688e
BLAKE2b-256 ec727491bb8d1b78c1dd22adb0af9922584486bd6487b7891255326474e43110

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.3.1-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 279.7 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.3.1-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 a19a5458f030fe9c6d907d59e7683222cdeeaa62485e258fdcd9cd221699fbd9
MD5 d795ad8374eddb5bb8e2847c0b0f3d30
BLAKE2b-256 ccbfe5d65029fff016d2fd48d4102efcbbff4f37c047f4a143676a6187c5aba9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.3.1-cp314-cp314t-win32.whl
  • Upload date:
  • Size: 266.3 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.3.1-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 7cd850e26a63526df9fac084b8ae6b2d0d88e72052c99993a3adaca3276e1d32
MD5 e59cb97a4161b1618043795ea359dad1
BLAKE2b-256 4b841515df2b4afaae646490c5a5502e3f05fb1a877d5949987007dcbff8abbb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.3.1-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 293.1 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.3.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 fcdda94e3992031fa431c9cf3e0f65cc7ea8902e814c9482b1937f48dce6c7f5
MD5 d91f55c0a494de5e25777f92af31e6fe
BLAKE2b-256 ffc18aa97826c21a726516b06fa2f537b242286dc3dafb66146cad8dcf137152

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.3.1-cp38-abi3-win32.whl
  • Upload date:
  • Size: 277.7 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.3.1-cp38-abi3-win32.whl
Algorithm Hash digest
SHA256 d9d9d0f3c0e995c4f38f909bd880890d65845da6eba5a1856784cead1b7bfb39
MD5 bb07fe2f2b0581ddbcb642077a373d8e
BLAKE2b-256 5607e2434557eb14dd15a85a8905e9e129d828deda5a9284dbb3c300b94a910c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.3.1-cp38-abi3-musllinux_1_2_x86_64.whl
  • Upload date:
  • Size: 486.8 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.3.1-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6aaf57e983599d4e1fb8064ffd8e236eded82b0a905147aa748362ded9d9ee76
MD5 64b562d5fc4288745f05a7603ebf38e2
BLAKE2b-256 7ff319a188827d94c27eed1599c199f05890e82674ffba54d3911502153f9087

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.3.1-cp38-abi3-musllinux_1_2_ppc64le.whl
  • Upload date:
  • Size: 473.2 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.3.1-cp38-abi3-musllinux_1_2_ppc64le.whl
Algorithm Hash digest
SHA256 1defc717a7719f1257485dbe8be933d9323256c8b5ae83546a50da5ae9f995b9
MD5 c801d5493feff8dbd0e3f31461236272
BLAKE2b-256 af348034ba37eb63325f2956afa94d7a6a11519dc72870df95ff46345c9f293d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tibs-0.3.1-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 41d76ec82e8f1ac8261d54b2ecb3b2a0daab6d2b98b98a5fc7c3e9865c69ce07
MD5 0d7aa92165c2d8218f88b3677330777c
BLAKE2b-256 40ce60a7e288c2ce292e744bc68ab68c58a6b8fedb78142062023910857a7be7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tibs-0.3.1-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 a555bff3c57221a449e0854d452b70c7a773e5a0cffc4a6b82e33be015bb8ae8
MD5 24f9443abe348b95c68b15f2bea0da21
BLAKE2b-256 6ee30331d92ad533dbd8bcd2c62b972b1d0172817bd8ce727014ee2603ea918d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tibs-0.3.1-cp38-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl
Algorithm Hash digest
SHA256 e7678fc668435c35c495578631d8725cfcab5b5b5739ce383744d3cb13aebb89
MD5 9aea460bd08654838acc1e973a56857f
BLAKE2b-256 0995e0a52e08e04037428aa57e4d9a6c0448aa146b8360c3d1d147be7fe0ee0c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tibs-0.3.1-cp38-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 3ec86d48e4b231e3e1522e8252b4c0b27a5e298f1c62d9af7e86a4f519f153ad
MD5 b395ee659844e825571808691796c8ec
BLAKE2b-256 a8f0e1f0a1652cc1fc7b2860e28308b9ec988566d3dc3c0160381a86fd44888d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tibs-0.3.1-cp38-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl
Algorithm Hash digest
SHA256 986ef3dfc9454ca35be08dc5ca56f8ba9828c39e40ae6728de65d46b01e5f860
MD5 25e81e1a710b922a634bf9be38f9ac80
BLAKE2b-256 c61e2dbef7441b4c8531c29369641a38d1c6e588bbbdb9f7839bb82befb501af

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tibs-0.3.1-cp38-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 363.8 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.3.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d93df50cb2b4114f3891bec445456f13e5439f496c71cb6938da96453b5b7ac8
MD5 365dc71f9bb7a9d6030c3c2c7e4cc772
BLAKE2b-256 872692e0797c76a13e9792893dfad31eedd3747c7404d6a9525bdff2b532ccdd

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