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.3.0.tar.gz (20.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.3.0-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pybare-1.3.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for pybare-1.3.0.tar.gz
Algorithm Hash digest
SHA256 8fd5ac122fa4613a683336bbcc003ffb8e5919f11a44d2e8d5b7170fa36e0b16
MD5 72053abac8e4e1a2da97157d942ad556
BLAKE2b-256 971bb3b229a412327b4956e77eba5d026ca3e7f0be44575b62a0b31d2b2cafe6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pybare-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 26.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for pybare-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bff3ee8bd77832052cb41a94becead1d06fc209cdb091eb8456287a3aa3bec52
MD5 21f93cdfd75e77943130124f51be2c7d
BLAKE2b-256 0dd23d60ce93cd83d59d85df1b421133dc511a03732f4f3ba476e842caf31063

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