PlutusMap

class cometa.plutus_data.plutus_map.PlutusMap(ptr=None)[source]

Bases: Mapping[PlutusData, PlutusData]

Represents a map of Plutus data to Plutus data.

This class provides Pythonic dict-like operations for working with Plutus data maps on the Cardano blockchain. It supports native Python types (int, str, bytes) which are automatically converted to PlutusData.

Example

>>> pmap = PlutusMap()
>>> pmap["key1"] = 42              # str key, int value
>>> pmap[1] = "value"              # int key, str value
>>> pmap[b"\x01"] = b"\x02"        # bytes key, bytes value
>>> len(pmap)
3
>>> # Dict-like operations
>>> for key in pmap:
...     print(key.kind)
>>> for key, value in pmap.items():
...     print(key, value)
>>> # Check membership
>>> "key1" in pmap
True
__init__(ptr=None)[source]
Return type:

None

__enter__()[source]
Return type:

PlutusMap

__exit__(exc_type, exc_val, exc_tb)[source]
Return type:

None

__repr__()[source]

Return repr(self).

Return type:

str

__len__()[source]

Returns the number of key-value pairs in the map.

Return type:

int

__bool__()[source]

Returns True if the map is not empty.

Return type:

bool

__eq__(other)[source]

Checks equality with another PlutusMap.

Parameters:

other (object)

Return type:

bool

__getitem__(key)[source]

Gets a value by key using bracket notation.

Parameters:

key (PlutusDataLike) – A PlutusData or native Python type (int, str, bytes).

Returns:

The PlutusData value associated with the key.

Raises:
  • KeyError – If the key is not found.

  • CardanoError – If retrieval fails.

Return type:

PlutusData

__setitem__(key, value)[source]

Sets a value by key using bracket notation.

Parameters:
  • key (PlutusDataLike) – A PlutusData or native Python type (int, str, bytes).

  • value (PlutusDataLike) – A PlutusData or native Python type (int, str, bytes).

Raises:

CardanoError – If the operation fails.

Return type:

None

__contains__(key)[source]

Checks if a key is in the map.

Parameters:

key (PlutusDataLike) – A PlutusData or native Python type (int, str, bytes).

Returns:

True if the key exists in the map.

Return type:

bool

__iter__()[source]

Iterates over all keys in the map (like Python dict).

Return type:

Iterator[‘PlutusData’]

classmethod from_cbor(reader)[source]

Deserializes a PlutusMap from CBOR data.

Parameters:

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

Returns:

A new PlutusMap deserialized from the CBOR data.

Raises:

CardanoError – If deserialization fails.

Return type:

PlutusMap

to_cbor(writer)[source]

Serializes the map to CBOR format.

Parameters:

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

Raises:

CardanoError – If serialization fails.

Return type:

None

clear_cbor_cache()[source]

Clears the cached CBOR representation.

Warning

Clearing the CBOR cache may change the binary representation when serialized, which can alter the data and invalidate existing signatures.

Return type:

None

to_cip116_json(writer)[source]

Serializes this object to CIP-116 compliant JSON.

Parameters:

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

Raises:

CardanoError – If serialization fails.

Return type:

None

insert(key, value)[source]

Inserts a key-value pair into the map.

Parameters:
Raises:

CardanoError – If insertion fails.

Return type:

None

get(key, default=None)[source]

Gets a value by key, returning a default if not found.

Parameters:
  • key (PlutusDataLike) – A PlutusData or native Python type (int, str, bytes).

  • default (Optional['PlutusData']) – Value to return if key is not found.

Returns:

The PlutusData value or the default.

Return type:

Optional[‘PlutusData’]

get_keys()[source]

Retrieves all keys from the map.

Returns:

A PlutusList containing all keys.

Raises:

CardanoError – If retrieval fails.

Return type:

PlutusList

get_values()[source]

Retrieves all values from the map.

Returns:

A PlutusList containing all values.

Raises:

CardanoError – If retrieval fails.

Return type:

PlutusList

keys()[source]

Returns an iterator over keys (like Python dict).

Returns:

An iterator over PlutusData keys.

Return type:

Iterator[‘PlutusData’]

values()[source]

Returns an iterator over values (like Python dict).

Returns:

An iterator over PlutusData values.

Return type:

Iterator[‘PlutusData’]

items()[source]

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

Returns:

An iterator over (PlutusData, PlutusData) tuples.

Return type:

Iterator[Tuple[‘PlutusData’, ‘PlutusData’]]

update(other)[source]

Updates the map with key-value pairs from another map or mapping.

Parameters:

other (Union[PlutusMap, Mapping[PlutusDataLike, PlutusDataLike]]) – Another PlutusMap or a mapping of PlutusData/native types.

Return type:

None

setdefault(key, default)[source]

Gets a value by key, inserting and returning default if not found.

Parameters:
  • key (PlutusDataLike) – A PlutusData or native Python type.

  • default (PlutusDataLike) – Value to insert and return if key is not found.

Returns:

The existing value or the default.

Return type:

PlutusData

copy()[source]

Creates a shallow copy of this map.

Returns:

A new PlutusMap with the same key-value pairs.

Return type:

PlutusMap

__hash__ = None
pop(key, *default)[source]

Note: Pop operation is not supported by the underlying C library. This method is provided for API completeness but will raise an error.

Raises:

NotImplementedError – Always raised as the C library doesn’t support removal.

Parameters:

key (PlutusDataLike)

Return type:

PlutusData