Core Library0.xAPI Reference

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

class libtmux latest

SparseArray

Support non-sequential indexes while maintaining list-like behavior.

Base types dict[int, T]t.Generic[T]
Source and package
Qualified name
libtmux._internal.sparse_array.SparseArray
Module
libtmux._internal.sparse_array
Package
libtmux
Source
src/libtmux/_internal/sparse_array.py
class libtmux._internal.sparse_array.SparseArray

Description#

A normal list would raise IndexError.

There are no native sparse arrays in python that contain non-sequential indexes and maintain list-like behavior. This is useful for handling libtmux options and hooks:

command-alias[1] split-pane=split-window to {'command-alias[1]': {'split-pane=split-window'}}

list would lose indice info, and dict would lose list-like behavior.

Examples#

Create a sparse array and add values at non-sequential indices:

>>> from libtmux._internal.sparse_array import SparseArray
>>> arr: SparseArray[str] = SparseArray()
>>> arr.add(0, "first hook command")
>>> arr.add(5, "fifth hook command")
>>> arr.add(2, "second hook command")

Access values by index (dict-style):

>>> arr[0]
'first hook command'
>>> arr[5]
'fifth hook command'

Check index existence:

>>> 0 in arr
True
>>> 3 in arr
False

Iterate values in sorted index order:

>>> list(arr.iter_values())
['first hook command', 'second hook command', 'fifth hook command']

Convert to a list (values only, sorted by index):

>>> arr.as_list()
['first hook command', 'second hook command', 'fifth hook command']

Append adds at max index + 1:

>>> arr.append("appended command")
>>> arr[6]
'appended command'

Access raw indices:

>>> sorted(arr.keys())
[0, 2, 5, 6]
Type parameters

Members4#

  • add method Add a value at a specific index.
  • append method Append a value at the next available index (max + 1).
  • as_list method Return values as a list in sorted index order.
  • iter_values method Iterate over values in sorted index order.
Esc

Type to search.

Reference previewOpen reference