dag_cbor.encoding
Encoding functions for DAG-CBOR codec.
canonical_order_dict
check_key_compliance
encode
- encode(data, stream=None, *, include_multicodec=False, normalize_strings=None)[source]
Encodes the given data with the DAG-CBOR codec.
By default, the encoded data is written to an internal stream and the bytes are returned at the end (as a bytes object).
def encode(data: IPLDKind, stream: None = None) -> bytes: ...
Example usage:
>>> dag_cbor.encode({'a': 12, 'b': 'hello!'}) b'\xa2aa\x0cabfhello!'
If a
streamis given, the encoded data is written to the stream and the number of bytes written is returned:def encode(data: IPLDKind, stream: BufferedIOBase) -> int: ...
Example usage with a stream:
>>> from io import BytesIO >>> stream = BytesIO() >>> dag_cbor.encode({'a': 12, 'b': 'hello!'}, stream=stream) 13 >>> stream.getvalue() b'\xa2aa\x0cabfhello!'
- Parameters:
data (
IPLDKind) – the DAG data to be encodedstream (
Optional[BufferedIOBase]) – an optional stream into which the encoded data should be writteninclude_multicodec (
bool; default =False) – ifTrue, the encoded data is prefixed by the multicodec code for'dag-cbor'(see multicodec.wrap).normalize_strings (
Optional[Literal['NFC','NFKC','NFD','NFKD']]) – whether strings should be normalised prior to encoding
- Raises:
CBOREncodingError – if an
intoutside ofrange(-2**64, 2**64)is encounteredDAGCBOREncodingError – if a value of type other than
None,bool,int,float,str,bytes,list,dict, orCIDis encounteredDAGCBOREncodingError – if attempting to encode the special
floatvaluesNaN,Infinityand-InfinityDAGCBOREncodingError – if a key of a dictionary is not a string
- Return type:
dag_cbor.encoding.__all__
The following members were explicitly reexported using __all__: