jam.types.base.sequences.base module

class jam.types.base.sequences.base.BaseSequence(initial: Sequence[T] = [], codec: Codec | None = None)[source]

Bases: Codable[Sequence[T]], Sequence[T], JsonSerde, Generic[T]

Base class for sequence types.

Provides common functionality for sequence types that support codec operations. All elements must be instances of the same Codable type.

__init__(initial: Sequence[T] = [], codec: Codec | None = None)[source]

Initialize sequence.

Parameters:
  • initial – Initial values

  • codec – Optional codec

__len__() int[source]

Get number of elements.

__getitem__(index: int | slice) T | Sequence[T][source]

Get item at index.

__iter__()[source]

Iterate over elements.

__repr__() str[source]

Get string representation.

_validate_value(value: T) None[source]

Validate that a value is of the correct type.

Parameters:

value – Value to validate

Raises:

TypeError – If value is not of the correct type

__eq__(other: object) bool[source]

Compare for equality.

__gt__(other: object) bool[source]

Compare for greater than.

__lt__(other: object) bool[source]

Compare for less than.

__ge__(other: object) bool[source]

Compare for greater than or equal to.

__le__(other: object) bool[source]

Compare for less than or equal to.

property element_type: Type[T] | None

Get the type of elements in this sequence.

__setitem__(index: int, value: T) None[source]

Set item at index.

append(value: T) None[source]

Append value to end of vector.

Parameters:

value – Value to append. Must be instance of the same type as other elements.

Raises:

TypeError – If value is not of the correct type

pop(index: int = -1) T[source]

Remove and return item at index.

Parameters:

index – Index of item to remove

Returns:

Removed item

Raises:

IndexError – If index out of range

insert(index: int, value: T) None[source]

Insert value at index.

Parameters:
  • index – Index to insert at

  • value – Value to insert. Must be instance of the same type as other elements.

Raises:

TypeError – If value is not of the correct type

remove(value: T) None[source]

Remove first occurrence of value.

Parameters:

value – Value to remove

Raises:

ValueError – If value not found

clear() None[source]

Clear all elements.

count(value: T) int[source]

Return number of occurrences of value.

Parameters:

value – Value to count

Returns:

Number of occurrences

index(value: T, start: int = 0, stop: int | None = None) int[source]

Return first index of value.

Parameters:
  • value – Value to find

  • start – Start index for search

  • stop – Stop index for search

Returns:

Index of value

Raises:

ValueError – If value not found

reverse() BaseSequence[T][source]

Reverse the vector in place.

extend(values: Sequence[T]) None[source]

Extend vector with values.

Parameters:

values – Values to add. Must all be instances of the same type as existing elements.

Raises:

TypeError – If values are not all of the correct type

__add__(other: BaseSequence[T] | Sequence[T]) BaseSequence[T][source]

Add two sequences together, returning a new sequence.

__iadd__(other: BaseSequence[T] | Sequence[T]) BaseSequence[T][source]

In-place addition of sequences.

__mul__(n: int) BaseSequence[T][source]

Multiply sequence by an integer, returning a new sequence.

__imul__(n: int) BaseSequence[T][source]

In-place multiplication of sequence.

__contains__(item: T) bool[source]

Check if item is in sequence.

copy() BaseSequence[T][source]

Return a shallow copy of the sequence.

sort(*, key=None, reverse=False) None[source]

Sort the sequence in place.

Parameters:
  • key – Function of one argument that is used to extract a comparison key

  • reverse – If True, sort in descending order

__reversed__()[source]

Return a reverse iterator over the sequence.

__delitem__(index: int | slice) None[source]

Delete item at index.

__rmul__(n: int) BaseSequence[T][source]

Right multiplication (n * sequence).

__hash__() None[source]

Sequences are mutable, so they should not be hashable.

startswith(prefix: Sequence[T]) bool[source]

Check if sequence starts with prefix.

endswith(suffix: Sequence[T]) bool[source]

Check if sequence ends with suffix.

classmethod from_json(data: Any) BaseSequence[T][source]

Deserialize from JSON.