Skip to main content

B3 is a binary serializer which is easy like json, compact like msgpack, powerful like protobuf, and handles datetimes in python

Project description

B3 = Better Binary Buffers

B3 is a data serializer, it packs data structures to bytes & vice versa. It has:

  • The schema power of protobuf, without the setup/compiler pain,
  • The quick-start ease of json.dumps, but with support for datetimes,
  • The compactness of msgpack, but without a large zoo of data types.

With B3 you can fast-start with schema-less data (like json), and move to schemas (like protobuf) later & stay compatible. Or have ad-hoc json-like clients talk to rigorous protobuf-like servers without pain & suffering.

The small number of lovingly-handcrafted data types means often the only choice you need make is between Fast or Compact.

This version is pure python, no dependencies apart from Six (and pytest for the tests). Tested working in python 3.8 & 2.7 on windows & linux.

Version

B3 is now version 1.x, it is out of beta.

The wire format and existing core data types are now frozen and will not change.

  • Except for the unused core types 10,11,12 which may have a type assigned in future, and
  • Except for SCHEDs unfinished named-timezone support, which needs py3.10+)

(v1.x is not backward compatible with beta 0.9.x versions)

Installing

pip install b3buf

>>> import b3

Getting Started

You can pack lists of things (like json.dumps):

import b3
list_data = [ None,  b"foo",  u"bar",  True,  -69,  2.318,  46j,  [1,2,3],  {4:5, 6:7},
              decimal.Decimal("13.37"), datetime.datetime.now() ]

list_buf = b3.pack(list_data)

out_list = b3.unpack(list_buf)

Complex numbers, decimal numbers, and dates and times all work.

You can pack dicts of things:

dict_data = { 1:1, u"2":u"2", b"3":b"3" }

dict_buf = b3.pack(dict_data)

out_dict = b3.unpack(dict_buf, 0)

Byte keys are supported as well as string and number keys

You can save on slicing when unpacking by giving unpack a start index

Schema Packing

You can make messages using a "type, name, tag_number" schema (like protobuf)

SCHEMA = (
    (b3.B3_BYTES,   "bytes1",  1),
    (b3.B3_UVARINT, "number1", 2),
    )

Schema packing/unpacking is to and from python Dicts.

sch_data = dict(bytes1=b"foo", number1=69)

sch_buf = b3.schema_pack(SCHEMA, sch_data)

out_sch = b3.schema_unpack(SCHEMA, sch_buf)

Tests

B3 ships with an extensive test suite, using pytest.

pip install pytest
cd /your/site-packages/b3
pytest 

More Info

See the tests, and examples.py in the tests folder for more examples (including how to nest schemas)

See datatypes.py for the available data types.

See wire_format.md for an overview of the wire format.

Licensing

The code in this project is licensed under MIT license. See LICENSE.txt.

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

b3buf-1.0.10.tar.gz (40.0 kB view details)

Uploaded Source

Built Distribution

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

b3buf-1.0.10-py2.py3-none-any.whl (45.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file b3buf-1.0.10.tar.gz.

File metadata

  • Download URL: b3buf-1.0.10.tar.gz
  • Upload date:
  • Size: 40.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for b3buf-1.0.10.tar.gz
Algorithm Hash digest
SHA256 463e11c444f7fd7e6edbd4164e118d10f558e4a7b9bbbf0690b0efec18e53a52
MD5 82aeacc883e0e01b6edc179dbaaf6c56
BLAKE2b-256 59cb36e57f0bc9d04e3bfeb81b540a10bfbb9f108c6df3a69d4f009ea9378dc8

See more details on using hashes here.

File details

Details for the file b3buf-1.0.10-py2.py3-none-any.whl.

File metadata

  • Download URL: b3buf-1.0.10-py2.py3-none-any.whl
  • Upload date:
  • Size: 45.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for b3buf-1.0.10-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d5b0d2e6404b2d4b1e922dca4ca1fc0c424fa49d4da92fe57e545b182b43dc5e
MD5 44f1cdc4f62c66f5956d8388a8b25961
BLAKE2b-256 c08d5fb5e44043ad63b3bbefca98e2c454f3e56a4fcd3f39f63bb32c2b8e91d2

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