Stream API using cbor2 to read and write CBOR data.
Project description
pycborstream
Simple python API to handle CBOR files as streams. It provides lazy methods to read and write huge CBOR data files saving memory: data can be treated on the fly without having to load the entire structure.
Pycborstream mimics the C++ QCborStreamReader Class and QCborStreamWriter Class API to read and write CBOR data. It uses cbor2 under the hood, and combines it to also read/write directly python dictionnaries and arrays.
Simple usage: write a CBOR data file
from io import BytesIO
from pycborstream import CborStreamEnc
cbor_out = "data.cbor"
with open(cbor_out, "wb") as fp:
cbor_encoder = CborStreamEnc(fp)
cbor_encoder.startMap(2) # start a map containing 2 key/value pairs
cbor_encoder.append("first_key")
cbor_encoder.startArray(None) # start an array of undefined length
cbor_encoder.append(5)
cbor_encoder.append(6)
cbor_encoder.append(7)
cbor_encoder.endArray()
cbor_encoder.append("second_key")
cbor_encoder.append("could be a new map or whatever")
cbor_encoder.endMap()
Simple usage: read this CBOR data file
from io import BytesIO
from pycborstream import CborStreamDec
with open(cbor_out, "rb") as fp:
cbor_decoder = CborStreamDec(fp)
cbor_decoder.enter_container() # enters the map (containing 2 keys)
assert cbor_decoder.value() == "first_key"
assert cbor_decoder.is_array() # next value is an array
assert cbor_decoder.length == None # length of array is not known
cbor_decoder.enter_container()
while cbor_decoder.has_next(): # loop on elements in the array
print(cbor_decoder.value())
cbor_decoder.leave_container() # we have to leave container to proceed
assert cbor_decoder.value() == "second_key"
assert cbor_decoder.value() == "could be a new map or whatever"
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pycborstream-0.0.4.tar.gz.
File metadata
- Download URL: pycborstream-0.0.4.tar.gz
- Upload date:
- Size: 17.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
342e10f223644f738566469e445086ab08a3f30fcfd0fb4ea9d370a9728cd989
|
|
| MD5 |
281b21f51d8f29b5cb8c2091b249c8ca
|
|
| BLAKE2b-256 |
1e5ebdd4837fbe3ecfc766994a2bb23a42ee5b7d87f3ca6f0b4680c1aae17185
|
File details
Details for the file pycborstream-0.0.4-py3-none-any.whl.
File metadata
- Download URL: pycborstream-0.0.4-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a91904573d8ccb644b479b24a3e8898a790d702bf0922ee0f816908bb2991ee5
|
|
| MD5 |
105215b928b658b1b5da55d939b7d32c
|
|
| BLAKE2b-256 |
2ab6dcd7a847249851fe78c8e7a211fc06ab67809110a464a28b9eeb095d1466
|