CborWriter

class cometa.cbor.cbor_writer.CborWriter(ptr=None)[source]

Bases: object

A simple writer for Concise Binary Object Representation (CBOR) encoded data.

This class facilitates encoding data into the CBOR format. It abstracts the complexities involved in CBOR encoding, providing a simple interface for creating CBOR data streams.

__init__(ptr=None)[source]

Creates and initializes a new instance of a CBOR writer.

Return type:

None

property refcount: int

Returns the number of active references to the underlying C object.

property encoded_size: int

Returns the current size of the encoded data in bytes.

property last_error: str

Returns the last error message recorded for this writer.

reset()[source]

Resets the writer, clearing all written data. The writer can be reused for new data without creating a new instance.

Return type:

None

encode()[source]

Finalizes the CBOR encoding and returns the data as bytes.

Return type:

bytes

to_hex()[source]

Returns the encoded data as a hexadecimal string.

Return type:

str

write_int(value)[source]

Encodes and writes an integer value.

This method automatically selects the appropriate CBOR encoding: - Major type 0 for positive integers fitting in uint64. - Major type 1 for negative integers fitting in int64. - Major type 6 (Tag 2/3) for arbitrary precision integers (BigInts).

Parameters:

value (int | BigInt) – The integer to write.

Return type:

None

write_bool(value)[source]

Writes a boolean value.

Parameters:

value (bool)

Return type:

None

write_bytes(data)[source]

Writes a byte string (major type 2).

Parameters:

data (bytes) – The raw bytes to encode.

Return type:

None

write_str(text)[source]

Writes a UTF-8 text string (major type 3).

Parameters:

text (str) – The string to encode.

Return type:

None

write_start_array(length=None)[source]

Initiates the writing of an array.

Parameters:

length (int, optional) – The number of elements. If None, starts an indefinite-length array.

Return type:

None

write_end_array()[source]

Concludes an indefinite-length array.

Return type:

None

write_start_map(length=None)[source]

Initiates the writing of a map.

Parameters:

length (int, optional) – The number of key-value pairs. If None, starts an indefinite-length map.

Return type:

None

write_end_map()[source]

Concludes an indefinite-length map.

Return type:

None

write_tag(tag)[source]

Assigns a semantic tag (major type 6) to the next data item.

Parameters:

tag (int | CborTag) – The tag value.

Return type:

None

write_null()[source]

Writes a null value (major type 7).

Return type:

None

write_undefined()[source]

Writes an undefined value (major type 7).

Return type:

None

write_encoded(data)[source]

Writes pre-encoded CBOR data directly to the stream.

This is useful if you already have a valid CBOR byte fragment and want to embed it without re-encoding.

Parameters:

data (bytes) – The pre-encoded CBOR bytes.

Return type:

None

__enter__()[source]

Enters the runtime context related to this object.

Return type:

CborWriter

__exit__(exc_type, exc_val, exc_tb)[source]

Exits the runtime context related to this object.

Return type:

None

__repr__()[source]

Returns a string representation of the CborWriter.

Return type:

str