Skip to content

sonolus.script.array_like

ArrayLike

Mixin for array-like objects.

Inheritors must implement __len__, __getitem__, and __setitem__.

Usage
class MyArrayLike[T](Record, ArrayLike[T]):
    def __len__(self) -> int:
        ...

    def __getitem__(self, index: Num) -> T:
        ...

    def __setitem__(self, index: Num, value: T):
        ...

__contains__(value)

Return whether any element in the array is equal to the given value.

Parameters:

Name Type Description Default
value Any

The value to check for.

required

__getitem__(index) abstractmethod

Return the item at the given index.

Parameters:

Name Type Description Default
index Num

The index of the item. Must be an integer between 0 and len(self) - 1.

required

__iter__()

Return an iterator over the array.

__len__() abstractmethod

Return the length of the array.

__reversed__()

Return a reversed view of the array.

__setitem__(index, value) abstractmethod

Set the value of the item at the given index.

Parameters:

Name Type Description Default
index Num

The index of the item. Must be an integer between 0 and len(self) - 1.

required
value T

The value to set.

required

count(value)

Return the number of elements in the array equal to the given value.

Parameters:

Name Type Description Default
value T

The value to count.

required

index(value, start=0, stop=None)

Return the index of the value in the array equal to the given value.

Parameters:

Name Type Description Default
value T

The value to search for.

required
start Num

The index to start searching from.

0
stop Num | None

The index to stop searching at. If None, search to the end of the array.

None

index_of_max(*, key=None)

Return the index of the maximum value in the array.

Parameters:

Name Type Description Default
key Callable[T, Any] | None

A one-argument ordering function to use for comparison like the one used in max().

None

index_of_min(*, key=None)

Return the index of the minimum value in the array.

Parameters:

Name Type Description Default
key Callable[T, Any] | None

A one-argument ordering function to use for comparison like the one used in min().

None

last_index(value)

Return the last index of the value in the array equal to the given value.

Parameters:

Name Type Description Default
value T

The value to search for.

required

reverse()

Reverse the values in the array in place.

shuffle()

Shuffle the values in the array in place.

sort(*, key=None, reverse=False)

Sort the values in the array in place.

Parameters:

Name Type Description Default
key Callable[T, Any] | None

A one-argument ordering function to use for comparison.

None
reverse bool

If True, sort in descending order, otherwise sort in ascending order.

False

swap(i, j)

Swap the values at the given indices.

Parameters:

Name Type Description Default
i Num

The first index.

required
j Num

The second index.

required