Skip to main content

Python implementation of the DAG-CBOR codec.

Project description

dag-cbor: A Python implementation of DAG-CBOR

Generic badge PyPI version shields.io PyPI status Checked with Mypy Python package

This is a fully compliant Python implementation of the DAG-CBOR codec, a subset of the Concise Binary Object Representation (CBOR) supporting the IPLD Data Model and enforcing a unique (strict) encoded representation of items.

You can install this package with pip:

pip install dag-cbor

The documentation for this package is automatically generated by pdoc.

Basic usage

The core functionality of the library is performed by the encode and decode functions:

>>> import dag_cbor
>>> dag_cbor.encode({'a': 12, 'b': 'hello!'})
b'\xa2aa\x0cabfhello!'
>>> dag_cbor.decode(b'\xa2aa\x0cabfhello!')
{'a': 12, 'b': 'hello!'}

A buffered binary stream (i.e. an instance of io.BufferedIOBase) can be passed to the encode function using the optional keyword argument stream, in which case the encoded bytes are written to the stream rather than returned:

>>> from io import BytesIO
>>> mystream = BytesIO()
>>> dag_cbor.encode({'a': 12, 'b': 'hello!'}, stream=mystream)
>>> mystream.getvalue()
b'\xa2aa\x0cabfhello!'

A buffered binary stream can be passed to the decode function instead of a bytes object, in which case the contents of the stream are read in their entirety and decoded:

>>> mystream = BytesIO(b'\xa2aa\x0cabfhello!')
>>> dag_cbor.decode(mystream)
{'a': 12, 'b': 'hello!'}

Random data

The random module contains functions to generate random data compatible with DAG-CBOR encoding. The functions are named rand_X, where X is one of int, bytes, str, bool (for False and True), bool_none (for False, True or None), list, dict, or cid (for CID data, encoded as an instance of BaseCID from the py-cid package). The function call rand_X(n) returns an iterator yielding a stream of n random values of type X:

>>> import pprint
>>> from dag_cbor.random import rand_options, rand_dict
>>> options = {
... 	"min_codepoint": 0x30,
...     "max_codepoint": 0x7a,
...     "max_chars": 4,
...     "max_bytes": 8,
...		"include_cid": False
... }
>>> with rand_options(**options):
...     for d in rand_dict(3):
...             pprint.pp(d)
...
{'': False,
 'biq': b'\x9b\xf4\xb7oG\x90',
 'ctz': b'{\x94^`_',
 'rg': 1.0563551621961306e+308,
 'y': ['ejdx',
       [None,
        8009056699280396679,
        3.6758824653722026e+307,
        -1.5555617166832836e+308,
        ''],
       1.6466769889093459e+308,
       [-12301485475384051926,
        '',
        b'zH\xe5',
        18321703892302698705,
        8.795874874880185e+307,
        'jwdr']]}
{'': -1.2571678042268863e+308, 'vmwq': -3474837013640509145}
{'h': [['k', False, -14411084192553465082],
       1.6359295422392278e+308,
       'vcad',
       False],
 'nsio': b'*\xa6;\xf9\xab',
 'sdmc': -359404367281466347}

For the full list of functions and options, please refer to the dag_cbor.random documentation.

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

dag-cbor-0.0.4.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

dag_cbor-0.0.4-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file dag-cbor-0.0.4.tar.gz.

File metadata

  • Download URL: dag-cbor-0.0.4.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.7

File hashes

Hashes for dag-cbor-0.0.4.tar.gz
Algorithm Hash digest
SHA256 5febe36c3f87613cc38297c130991d5871175d81265de36b4f0a2417fab08878
MD5 8f97f7f0fa3d786f91ab208aca8cc1da
BLAKE2b-256 c877923a8f398c5a3034c73e35623bc84490561bb7eed79bccdedbb129699f49

See more details on using hashes here.

File details

Details for the file dag_cbor-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: dag_cbor-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.7

File hashes

Hashes for dag_cbor-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 26cc7f2fff919cc044256634afc44d74bc4fa8fd2f7f619a161a5bb166e1f58e
MD5 50e1455642068fa35ed905237ef92d31
BLAKE2b-256 8c30a8545679f62b2181cba624c95fe6f31a638447704ae6f693c6893748e81a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page