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!'}

Usage with binary streams

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 for uniformly distributed integers
  • float for uniformly distributed floats, with fixed decimals
  • bytes for byte-strings of uniformly distributed length, with uniformly distributed bytes
  • str for strings of uniformly distributed length, with uniformly distributed codepoints (all valid UTF-8 strings, by rejection sampling)
  • bool for False or True (50% each)
  • bool_none for False, True or None (33.3% each)
  • list for lists of uniformly distributed length, with random elements of any type
  • dict for dictionaries of uniformly distributed length, with distinct random string keys and random values of any type
  • cid for CID data (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
>>> import dag_cbor
>>> options = dict(min_codepoint=0x41, max_codepoint=0x5a, include_cid=False)
>>> with dag_cbor.random.rand_options(**options):
...     for d in dag_cbor.random.rand_dict(3):
...             pprint.pp(d)
...
{'BIQPMZ': b'\x85\x1f\x07/\xcc\x00\xfc\xaa',
 'EJEYDTZI': {},
 'PLSG': {'G': 'JFG',
          'HZE': -61.278,
          'JWDRKRGZ': b'-',
          'OCCKQPDJ': True,
          'SJOCTZMK': False},
 'PRDLN': 39.129,
 'TUGRP': None,
 'WZTEJDXC': -69.933}
{'GHAXI': 39.12,
 'PVUWZLC': 4.523,
 'TDPSU': 'TVCADUGT',
 'ZHGVSNSI': [-57, 9, -78.312]}
{'': 11, 'B': True, 'FWD': {}, 'GXZBVAR': 'BTDWMGI', 'TDICHC': 87}

The function call rand_X(), without the positional argument n, would instead yield an infinite stream of random values. The rand_options(**options) context manager is used to set options temporarily: in the example above, we set string characters to be uppercase alphabetic (codepoints 0x41-0x5a) and we excluded CID values from being generated. 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.5.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

dag_cbor-0.0.5-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dag-cbor-0.0.5.tar.gz
  • Upload date:
  • Size: 14.2 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.5.tar.gz
Algorithm Hash digest
SHA256 24a75f95ff98c58c841ea9e7d404707e81175e8fc0a0d15b9fb0bcc2d4935ee5
MD5 50e5c6dcbef5ba6c71a8303ba988c861
BLAKE2b-256 a3fbc5b37f8115e95087b57202cd3e3b035d00073e0595dc730131533a0fcb00

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dag_cbor-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 13.1 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2720f075edaf48bdf97b9fe1fe7b554e80bb80a498b04e0a91e8ad97cb56072a
MD5 94be498950c70d6529aabd39c04c0979
BLAKE2b-256 9ac689d8d27adeea3da89accc01dc76eaf22b95c8f3876606cf912d2c412c4e7

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