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
- __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:
- __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:
- 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:
key (PlutusData) – The PlutusData key.
value (PlutusData) – The PlutusData value.
- 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:
- get_values()[source]
Retrieves all values from the map.
- Returns:
A PlutusList containing all values.
- Raises:
CardanoError – If retrieval fails.
- Return type:
- 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:
- copy()[source]
Creates a shallow copy of this map.
- Returns:
A new PlutusMap with the same key-value pairs.
- Return type:
- __hash__ = None