Skip to main content

A declarative implementation of BARE for Python

Project description

PyBARE

builds.sr.ht status

An declarative implementation of the BARE message format for Python 3.11+


pybare is a general purpose library for strongly typed primitives in Python that supports serializing to and from BARE messages.

pip install pybare

Goals

  • Provide a declarative structure for defining types
  • Validation on value updates
  • Support streaming messages

Status

pybare fully implements all BARE types for both encoding and decoding. This includes reading multiple messages from the same BinaryIO stream.

TODO

  • Codegen based on .schema files
  • Better documentation
  • More tests
  • Fast C implementation for encoding

Examples

pybare currently requires you define your structures by hand. Examples can be found in the tests.

Quickstart

from bare import Struct, map, Str, UInt, optional, data, array, Void

# Alternatively, class Data(size=64): ...
PubKey = data(64) # 512 bits

class User(Struct):
    username = Filed(Str)
    userid = Field(Int)
    email = Field(optional(Str))
    keys = Field(map(Str, PubKey))
    repos = Field(array(Str)) # variable length array


noah = User(username="chiefnoah", userid=1, email=Void(), keys={}, repos=[])
noah.username == 'chiefnoah'
noah.username = 'someoneelse'
noah.username == 'someoneelse'
noah.userid == 1 # True
noah.username = 1 # raise: bare.ValidationError
noah.keys # {} (empty dict)
noah.keys['my key'] = bytes(64) #\x00\x00...
noah.keys['oops'] = bytes(1) # raise: bare.ValidationError
noah.email == Void() # True
noah.email = 12345 # raise: bare.ValidationError
noah.pack() # \x00\x01 ... (binary data)

Note, you must wrap the desired type in a Field to get its 'magic' behavior. Class or instance fields that are not wrapped in a Field will be ignored by the pack and unpack methods.


To contribute, send patches to ~chiefnoah/inbox@lists.sr.ht

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

pybare-1.0.0.tar.gz (14.5 kB view hashes)

Uploaded Source

Built Distribution

pybare-1.0.0-py3-none-any.whl (17.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page