Skip to main content

attrs2bin

attrs2bin is a Python library that lets you serialize/deserialize your attrs-based classes to/from a byte stream. It is compatible with Rust's bincode, so you can seralize objects in Python, send the resulting byte stream through a socket or any other transport and deserialize it back to a Rust object. It can also deserialize objects from a socket (see below).

Example

attrs2bin provides just two simple funcions: serialize(), which takes an object and returns a byte stream, and deserialize(), that takes a byte stream and an attrs-based class and returns an object.

import attrs2bin

@attr.s(auto_attribs=True)
class Sprite:
    name: str
    x: int
    y: int

my_sprite = Sprite("My sprite", 35, 70)
serialized = attrs2bin.serialize(my_sprite)
deserialized = attrs2bin.deserialize(serialized, Sprite)
assert my_sprite == deserialized

What can be serialized?

Objects of any attrs-based class can be serialized, as long as all their fields have a type (using type annotations or attr.ib(type=...)). attrs2bin ships with serializer for the following types:

  • int
  • float
  • bytes
  • str
  • bool

You can create and register your own serializers for specific types by creating a class that implements attrs2bin.interfaces.ITypeSerializer and calling attrs2bin.register_serializer().

Deserializing from a socket

Instead of deserialize(bytes, cls), you can use deserialize_from_socket(sck, cls), which will read the necessary bytes from a socket and return a Python object. sck must be any object that implements attrs2bin.interfaces.IReadableSocket.

Rust compatibility

The serializers that ships with attrs2bin are all compatible with Rust's bincode library. Keep under your pillow the following table in order to create compatible types between Python and Rust:

Python type Rust type
int / attrs2bin.SignedInt i64
attrs2bin.UnsignedInt u64
float / attrs2bin.Float64 f64
attrs2bin.Float32 f32
bytes Vec<u8>
str String
bool bool

Release files for attrs2bin 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for attrs2bin 0.0.1
File Size Uploaded
attrs2bin-0.0.1.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for attrs2bin 0.0.1
File Interpreter ABI Platform
attrs2bin-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.0 kB

Release files / attrs2bin-0.0.1.tar.gz

Download URL attrs2bin-0.0.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
aef44ab25ef4f565d8a1eeddcdbe44d5d02ab21b5c42d9cfe754c0ff215b57f8
BLAKE2b-256 checksum
How to use checksums
15dd2cad1e314aad787d907cd3b6e6b114bb99589b368d6643e0fc6eb5c6859b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.1.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

Release files / attrs2bin-0.0.1-py3-none-any.whl

Download URL attrs2bin-0.0.1-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ba2bab66439ca69ced30421eca76338a18a72d904eaf30f445b3917cce958ec
BLAKE2b-256 checksum
How to use checksums
febd86cc63c7d38f23b16abc0117d9a4db161cc6bb252c187749fc2bc6b9d785
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.1.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page