UtxoList

class cometa.common.utxo_list.UtxoList(ptr=None)[source]

Bases: Sequence[Utxo]

Represents a list of UTxOs (Unspent Transaction Outputs).

This class provides a collection interface for managing multiple UTxOs, supporting standard list operations like iteration, indexing, and slicing.

Example

>>> from cometa import UtxoList, Utxo
>>> utxo_list = UtxoList()
>>> utxo_list.add(utxo)
>>> print(len(utxo_list))
1
__init__(ptr=None)[source]
Return type:

None

__enter__()[source]
Return type:

UtxoList

__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 UTxOs in the list.

Return type:

int

__iter__()[source]

Iterates over all UTxOs in the list.

Return type:

Iterator[Utxo]

__getitem__(index)[source]

Gets a UTxO by index using bracket notation.

Parameters:

index (int)

Return type:

Utxo

__bool__()[source]

Returns True if the list is not empty.

Return type:

bool

classmethod from_list(utxos)[source]

Creates a UtxoList from a Python list of Utxo objects.

Parameters:

utxos (list[Utxo]) – A list of Utxo objects.

Returns:

A new UtxoList containing all the UTxOs.

Raises:

CardanoError – If creation fails.

Return type:

UtxoList

Example

>>> utxo_list = UtxoList.from_list([utxo1, utxo2, utxo3])
add(utxo)[source]

Adds a UTxO to the end of the list.

Parameters:

utxo (Utxo) – The Utxo to add.

Raises:

CardanoError – If addition fails.

Return type:

None

get(index)[source]

Retrieves a UTxO at the specified index.

Parameters:

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

Returns:

The Utxo at the specified index.

Raises:
  • CardanoError – If retrieval fails.

  • IndexError – If index is out of bounds.

Return type:

Utxo

remove(utxo)[source]

Removes a specific UTxO from the list.

Parameters:

utxo (Utxo) – The Utxo to remove.

Raises:

CardanoError – If removal fails or UTxO not found.

Return type:

None

clear()[source]

Removes all UTxOs from the list, leaving it empty.

Return type:

None

clone()[source]

Creates a shallow clone of this UTxO list.

The cloned list contains references to the same UTxO elements. The UTxO elements themselves are not duplicated.

Returns:

A new UtxoList containing the same elements.

Return type:

UtxoList

concat(other)[source]

Concatenates this list with another, returning a new list.

Parameters:

other (UtxoList) – The UtxoList to concatenate with.

Returns:

A new UtxoList containing elements from both lists.

Return type:

UtxoList

slice(start, end)[source]

Extracts a portion of the list between the given indices.

Parameters:
  • start (int) – Start index of the slice (inclusive).

  • end (int) – End index of the slice (exclusive).

Returns:

A new UtxoList containing the slice.

Return type:

UtxoList

erase(start, delete_count=1)[source]

Removes elements from the list starting at a given index.

This function removes delete_count elements from the list starting at the specified index and returns a new list containing the removed elements. The original list is modified in place.

Parameters:
  • start (int) – The index at which to start removing elements. Supports negative indices (e.g., -1 for the last element).

  • delete_count (int) – The number of elements to remove from the list starting at start. If delete_count exceeds the number of elements from start to the end, it will be adjusted to remove till the end. Defaults to 1.

Returns:

A new UtxoList containing the removed elements.

Raises:

CardanoError – If the erase operation fails.

Return type:

UtxoList

Example

>>> utxo_list = UtxoList.from_list([utxo1, utxo2, utxo3])
>>> removed = utxo_list.erase(1, 1)  # Remove second element
>>> print(len(utxo_list))  # Now has 2 elements
2
>>> print(len(removed))  # Removed list has 1 element
1
__add__(other)[source]

Concatenates two UtxoLists using the + operator.

Parameters:

other (UtxoList | list[Utxo])

Return type:

UtxoList

index(value, start=0, stop=None)[source]

Returns the index of the first occurrence of value.

Parameters:
  • value (Utxo) – The value to search for.

  • start (int) – Start searching from this index.

  • stop (int | None) – Stop searching at this index.

Returns:

The index of the first occurrence.

Raises:

ValueError – If the value is not found.

Return type:

int

count(value)[source]

Returns the number of occurrences of value.

Parameters:

value (Utxo) – The value to count.

Returns:

The number of occurrences.

Return type:

int