cerial
Fast, simple, and more pythonic binary serializer library.
Cerial combines Python 3 features, Cython, and memory views to create a fast and easy library for packing and unpacking binary structures.
Removes a lot of the boilerplate that sometimes ends up surrounding struct when used for serialization.
Example
Here’s a quick example of how to use cerial to handle a UDP packet. First we define the structure:
>>> UDP = Struct('UDP',
... ('sport', 'uint16'),
... ('dport', 'uint16'),
... ('len', 'uint16'),
... ('csum', 'uint16'),
... endianness='!'
...)
Unlike the builtin struct library, cerial needs the fields to be bound to a name. This is because we expose the binary structure with dictionary interface instead of as a tuple of values.
Now that we have a structure defined, we can use to parse data:
>>> data = bytearray(b'\x08R\x02\x02\x00w\xa9k')
>>> hdr = UDP.parse(data)
>>> hdr
<UDP b'\x08R\x02\x02\x00w\xa9k'>
We can now inspect the binary structure:
>>> hdr['csum']
43371
>>> dict(hdr)
{'csum': 43371, 'len': 119, 'sport': 2130, 'dport': 514}
>>> bytes(hdr)
b'\x08R\x02\x02\x00w\xa9k'
And we can also update individual fields, which will directly write to the underlying memory view, no rendering step needed.
>>> hdr['csum'] = 0xffff
>>> data
bytearray(b'\x08R\x02\x02\x00w\xff\xff')
>>> bytes(hdr)
b'\x08R\x02\x02\x00w\xff\xff'
TODO
Feature parity with the various primitives the struct library supports
First class support for bit fields
Improve performance
Support readonly memoryviews (e.g. support bytes). An upstream Cython issue.
Performance
Currently slightly slower than the struct module, but as far as I can tell, significantly faster than other similar libraries like pypacker or scrapy at parsing packets.
Metadata
Release files for cerial 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cerial-0.0.1.tar.gz | 471.9 kB | Without provenance | Details for cerial-0.0.1.tar.gz |
Release files / cerial-0.0.1.tar.gz
| Download URL | cerial-0.0.1.tar.gz |
|---|---|
| Size | 471.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e1ebf09b520c8d56c7a3b6079720ea3ee7101cdfccdf0257f590b3ba0a1702a4
|
|
BLAKE2b-256 checksum How to use checksums |
cb0580303e683fb34ab1161d46c32e2f446335c29d9abe7a5c1fe86072a510ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for cerial 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.