Skip to main content

A BARE Encoding Library and Data Validation Library for Python.

Project description

PyBARE

builds.sr.ht status Static Badge

A declarative implementation of the BARE message format for Python 3.10+


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.

It is well tested and in use on production systems.

Gotchas

We use "Array" / "array" instead of "List" / "list" to avoid conflicts with Python's common builtin type of the same name.

Examples

An example that defines the types used as an example in the RFC may be found in example.py

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

Quickstart

The general convention is type identifiers start with a capital letter and anonymous generating functions begin with a lowercase. This follows general "pythonic" style for classes vs functions.

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

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

class User(Struct):
    username = Field(Str)
    userid = Field(Int)
    email = Field(optional(Str))
    keys = Field(map(Str, PubKey))
    repos = Field(array(Str)) # variable length array
    # anonymous array and struct
    friends = Field(array(struct(name=Str, age=U8)))


noah = User(username="chiefnoah", userid=1, email=Void(), keys={}, repos=[], friends=[])
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.2.5.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

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

pybare-1.2.5-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file pybare-1.2.5.tar.gz.

File metadata

  • Download URL: pybare-1.2.5.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pybare-1.2.5.tar.gz
Algorithm Hash digest
SHA256 efdb9ebed18b8734c7f31047df3e216b072f047c33860218afd25cc23a51adf6
MD5 f8dd1997751cdf0973b0003fc0b4d747
BLAKE2b-256 4c2f782dd3ba243decc90f8ce6c57bb161ecbc83c76ddaf8d2fd7426315b4c1d

See more details on using hashes here.

File details

Details for the file pybare-1.2.5-py3-none-any.whl.

File metadata

  • Download URL: pybare-1.2.5-py3-none-any.whl
  • Upload date:
  • Size: 25.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pybare-1.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 73d3f17cf91e5534666ca37bbfe89d002c13af7513e19d33f7aa48636353c175
MD5 177cf1a4626bc497504d63b63694fbc2
BLAKE2b-256 254cd968cc50aed37a1b32d560774c13efab0ce07307e022d4a392f3a90080e6

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