An implementation of the CloudEvents spec using Pydantic V2
Project description
cloudevents-pydantic
This is an implementation of the CloudEvents spec using Pydantic V2 for high performance during validation and serialization.
Currently supported bindings:
Binding | Format | Single | Batch |
---|---|---|---|
HTTP | JSON | ✅ | ✅ |
HTTP | Binary | planned | planned |
KAFKA | JSON | planned | planned |
KAFKA | Binary | planned | planned |
How to use
pip install cloudevents-pydantic
from cloudevents_pydantic.bindings.http import HTTPHandler
from cloudevents_pydantic.events import CloudEvent
handler = HTTPHandler()
minimal_attributes = {
"type": "com.example.string",
"source": "https://example.com/event-producer",
}
# `CloudEvent` is a Pydantic model to handle validation and serialization
# `event_factory` is a helper method to autogenerate some of the mandatory
# such as id, time, specversion
event = CloudEvent.event_factory(**minimal_attributes)
# Single event HTTP serialization
headers, single_event_as_json = handler.to_json(event)
# Batch of events HTTP serialization
headers, batch_of_events_as_json = handler.to_json_batch([event])
# Parsing a JSON string for a single event
parsed_event = handler.from_json(single_event_as_json)
# Parsing a JSON string for a single event
parsed_event_list = handler.from_json(batch_of_events_as_json)
Refer to the docs for more advanced use cases and for details on how to create custom events.
Performance
Using pydantic gives a great performance boost if compared to the official SDK. (there's obviously some performance issue in the official serialization using pydantic)
These results come from a Macbook Pro M3 Max on python 3.12. Feel free to run the benchmark.py
script yourself.
Timings for HTTP JSON deserialization:
This package: 2.5353065830422565
Official SDK with pydantic model: 12.80780174996471
Official SDK with http model: 11.474249749968294
Timings for HTTP JSON serialization:
This package: 3.4850796660175547
Official SDK with pydantic model: 39.037468083028216
Official SDK with http model: 7.681282749981619
Commands for development
All the common commands used during development can be run using make targets:
make dev-dependencies
: Install dev requirementsmake update-dependencies
: Update dev requirementsmake fix
: Run code style and lint automatic fixes (where possible)make test
: Run test suite against system python versionmake check
: Run tests against all available python versions, code style and lint checksmake type
,make format
,make lint
,make bandit
: Run the relevant checkmake docs
: Render the mkdocs website locally
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for cloudevents_pydantic-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | be644882be25348b5d31b2d0a06bef84f491741d1d9703be212dd848fec8c147 |
|
MD5 | 1eb83b33622d0536e95d7be5c6ce6f04 |
|
BLAKE2b-256 | c5b6483fdb8678b2457c8d764208d6d561c35fd15a27532a6fd3eca59220deef |
Hashes for cloudevents_pydantic-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ac76017151d94e9fa545892fc3c3a993b6204589c444e9e4509f3e67a9a6170 |
|
MD5 | 2bba77bfe66fce42eb782e202be05f82 |
|
BLAKE2b-256 | 729a95bd003c8c7bcc365789b7a44d4f31a8dd051a8e6aa44f07c21c148ca765 |