MetadatumMap

class cometa.auxiliary_data.metadatum_map.MetadatumMap(ptr=None)[source]

Bases: Mapping[Metadatum, Metadatum]

Represents a map of metadatum keys to metadatum values.

Both keys and values can be any valid metadatum type. Accepts Python primitives (int, str, bytes) directly for keys and values.

Example

>>> meta_map = MetadatumMap()
>>> meta_map["name"] = "Alice"        # str key and value
>>> meta_map["age"] = 30              # str key, int value
>>> meta_map[1] = b"\xde\xad"         # int key, bytes value
>>> len(meta_map)
3
>>> "name" in meta_map
True
__init__(ptr=None)[source]

Initializes a new MetadatumMap.

Parameters:

ptr – Optional FFI pointer to an existing map. If None, creates a new empty map.

Raises:

CardanoError – If map creation fails or if ptr is NULL.

Return type:

None

__enter__()[source]

Enables use as a context manager.

Return type:

MetadatumMap

__exit__(exc_type, exc_val, exc_tb)[source]

Cleans up when exiting context manager.

Return type:

None

__repr__()[source]

Returns a string representation of the MetadatumMap.

Return type:

str

classmethod from_cbor(reader)[source]

Deserializes a MetadatumMap from CBOR data.

Parameters:

reader (CborReader) – A CborReader positioned at the map data.

Returns:

A new MetadatumMap deserialized from the CBOR data.

Raises:

CardanoError – If deserialization fails.

Return type:

MetadatumMap

insert(key, value)[source]

Inserts or updates a key-value pair.

Parameters:
  • key (Metadatum | int | str | bytes | bytearray) – The metadatum key. Can be a Metadatum or primitive (int, str, bytes).

  • value (Metadatum | int | str | bytes | bytearray) – The metadatum value. Can be a Metadatum or primitive (int, str, bytes).

Raises:
  • CardanoError – If insertion fails.

  • TypeError – If key or value cannot be converted to Metadatum.

Return type:

None

Example

>>> meta_map = MetadatumMap()
>>> meta_map.insert("name", "Alice")
>>> meta_map.insert(1, 42)
get(key, default=None)[source]

Retrieves the value for a given key.

Parameters:
  • key (Metadatum | int | str | bytes | bytearray) – The metadatum key to look up. Can be a Metadatum or primitive.

  • default (Metadatum | None) – Value to return if key is not found. Defaults to None.

Returns:

The Metadatum value associated with the key, or default if not found.

Raises:

TypeError – If key cannot be converted to Metadatum.

Return type:

Metadatum | None

get_at(index)[source]

Retrieves the key-value pair at a specific index.

Parameters:

index (int) – The index of the entry to retrieve.

Returns:

A tuple of (key, value) Metadatum objects at the specified index.

Raises:
  • CardanoError – If retrieval fails.

  • IndexError – If index is out of bounds.

Return type:

Tuple[Metadatum, Metadatum]

to_cbor(writer)[source]

Serializes the metadatum map to CBOR format.

Parameters:

writer (CborWriter) – A CborWriter to write the serialized data to.

Raises:

CardanoError – If serialization fails.

Return type:

None

__len__()[source]

Returns the number of entries in the map.

Return type:

int

__iter__()[source]

Iterates over all keys (like Python dict).

Return type:

Iterator[Metadatum]

__eq__(other)[source]

Checks equality with another MetadatumMap.

Parameters:

other (object)

Return type:

bool

__bool__()[source]

Returns True if the map is not empty.

Return type:

bool

__getitem__(key)[source]

Gets a value by key using bracket notation. Accepts primitives.

Parameters:

key (Metadatum | int | str | bytes | bytearray)

Return type:

Metadatum

__setitem__(key, value)[source]

Sets a value by key using bracket notation. Accepts primitives.

Parameters:
  • key (Metadatum | int | str | bytes | bytearray)

  • value (Metadatum | int | str | bytes | bytearray)

Return type:

None

__contains__(key)[source]

Checks if a key is in the map. Accepts primitives.

Parameters:

key (Metadatum | int | str | bytes | bytearray)

Return type:

bool

keys()[source]

Returns an iterator over keys (like Python dict).

Return type:

Iterator[Metadatum]

values()[source]

Returns an iterator over values (like Python dict).

Return type:

Iterator[Metadatum]

items()[source]

Returns an iterator over (key, value) pairs (like Python dict).

Return type:

Iterator[Tuple[Metadatum, Metadatum]]

get_keys()[source]

Retrieves all keys from the map.

Returns:

A MetadatumList containing all keys in the map.

Raises:

CardanoError – If retrieval fails.

Return type:

MetadatumList

get_values()[source]

Retrieves all values from the map.

Returns:

A MetadatumList containing all values in the map.

Raises:

CardanoError – If retrieval fails.

Return type:

MetadatumList

__hash__ = None
to_cip116_json(writer)[source]

Serializes this metadatum map to CIP-116 compliant JSON.

Parameters:

writer (JsonWriter) – The JsonWriter to write the JSON to.

Raises:

CardanoError – If serialization fails.

Return type:

None