Welcome to Python-flavored Magnum! Please note that, while already being rather stable, this functionality is still considered experimental and some APIs might get changed without preserving full backwards compatibility.

corrade.containers.StridedArrayView1D class

One-dimensional array view with stride information

Provides a typed one-dimensional read-only view on a memory range with custom stride values. Convertible both to and from Python objects supporting the Buffer Protocol, preserving the dimensionality, type and stride information:

>>> a = array.array('f', [2.5, 3.14, -1.75, 53.2])
>>> b = containers.StridedArrayView1D(memoryview(a)[::2])
>>> b[0]
2.5
>>> b[1]
-1.75

See StridedArrayView2D, StridedArrayView3D, StridedArrayView4D, MutableStridedArrayView1D and others for multi-dimensional and mutable equivalents.

Memory ownership and reference counting

Similarly to ArrayView, the view keeps a reference to the original memory owner object in the owner field. Slicing a view creates a new view referencing the same original object, without any dependency on the previous view. The owner is None if the view is empty.

Comparison to Python’s memoryview

The StridedArrayView1D and its multi-dimensional variants are equivalent to any memoryview, but additionally supporting multi-dimensional slicing as well (which raises NotImplementedError in Py3.7 memoryview).

Methods

def broadcasted(self, dimension: int, size: int) -> StridedArrayView1D
Broadcast a dimension
def expanded(self, dimension: int, size: typing.Tuple[int, int]) -> StridedArrayView2D
Expand a dimension
def expanded(self, dimension: int, size: typing.Tuple[int, int, int]) -> StridedArrayView3D
Expand a dimension
def expanded(self, dimension: int, size: typing.Tuple[int, int, int, int]) -> StridedArrayView4D
Expand a dimension
def flipped(self, dimension: int) -> StridedArrayView1D
Flip a dimension
def slice_bit(self, index: int) -> StridedBitArrayView1D
Slice to a bit

Special methods

def __bytes__(self, /) -> bytes
Convert to bytes
def __getitem__(self, slice: slice) -> StridedArrayView1D
Slice the view
def __getitem__(self, i: int) -> object
Value at given position
def __init__(self, /) -> None
Default constructor
def __init__(self, arg0: buffer, /) -> None
Construct from a buffer
def __len__(self, /) -> int
View size in the top-level dimension

Properties

dimensions: int get
Dimension count
format: str get
Format of each item
owner: object get
Memory owner object
size: typing.Tuple[int] get
View size in each dimension
stride: typing.Tuple[int] get
View stride in each dimension

Method documentation

def corrade.containers.StridedArrayView1D.broadcasted(self, dimension: int, size: int) -> StridedArrayView1D

Broadcast a dimension

Exceptions
IndexError If dimension is not 0

def corrade.containers.StridedArrayView1D.expanded(self, dimension: int, size: typing.Tuple[int, int]) -> StridedArrayView2D

Expand a dimension

Exceptions
IndexError If dimension is not 0
ValueError If product of size is not equal to size in dimension

def corrade.containers.StridedArrayView1D.expanded(self, dimension: int, size: typing.Tuple[int, int, int]) -> StridedArrayView3D

Expand a dimension

Exceptions
IndexError If dimension is not 0
ValueError If product of size is not equal to size in dimension

def corrade.containers.StridedArrayView1D.expanded(self, dimension: int, size: typing.Tuple[int, int, int, int]) -> StridedArrayView4D

Expand a dimension

Exceptions
IndexError If dimension is not 0
ValueError If product of size is not equal to size in dimension

def corrade.containers.StridedArrayView1D.flipped(self, dimension: int) -> StridedArrayView1D

Flip a dimension

Exceptions
IndexError If dimension is not 0

def corrade.containers.StridedArrayView1D.__getitem__(self, i: int) -> object

Value at given position

Exceptions
IndexError If i is out of range
NotImplementedError If the view was created from a buffer and format is not one of 'b', 'B', 'h', 'H', 'i', 'I', 'q', 'Q', 'f' or 'd'