Documentation Index | From module structarray.

Array

Array(initial_data=[], size=0, default=0)

This is a single dimensional array.

initial_data can be used to populate the array. Array(range(10)) works as you would expect :)

size is the initial size of the array. (Unless the length of initial_data is bigger, in which case it is used.) If size is a tuple with more than one item, the array will be multidimensional. See the structarray module documentation form more on multidimensional arrays.

default is the default value for the array items.

Note that you can use both initial_data and size at the same time. If size larger than the length of initial_data, the rest of the array will be filled with default. Array(range(3), size=8, default=6) would result in [0, 1, 2, 6, 6, 6, 6, 6].

(The exception to this is if you are a multidimensional array, in which case initial_data will be truncated to fit, as multidimensional arrays cannot currently be resized.)

Array supports basic arithmetic operations: addition, subtraction, multiplication, division, and modulus. Each of these operations can be performed against a single number or another Array of the same length.

Arithmetic operations don't return an Array. Instead they return an array operation object. The operation will be carried out when you assign this object to another array. (You can also call it's copy() method to create a new array.)

For example:

>>> arg1 = Array(range(10))
>>> arg2 = Array(range(10,20))
>>> arg1 + arg2
<structarray.ArrayOpAdd object at 0xb7d4d66c>
>>> (arg1 + arg2).copy()
<Array [10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0, 26.0, 28.0]>
>>> arg1.assign_from(arg1 + arg2)
>>> print arg1
<Array [10.0, 12.0, 14.0, 16.0, 18.0, 20.0, 22.0, 24.0, 26.0, 28.0]>

Methods

append

append(item)

Appends an item to the array.

assign_from

assign_from(source)

Reassigns our value from another array.

source can be another Array instance, or any iterable of the correct length. It can also me a single number, in which case it will be assigned to each item.

assign_to

assign_to(target)

Assigns our value to another array.

target must be another Array instance. (Not a StructArray, but a StructArray attribute works fine.)

copy

copy()

Returns a new Array with a copy of our data. (The data is copied immediately; there's nothing lazy here.)

extend

extend(items)

Iterates over items, calling append() on each one.

get_data_addr

get_data_addr()

Gets the memory address of the data.

This, together with get_data_stride(), can be used with PyOpenGL or Pyglet for OpenGL vertex arrays. For example:

>>> array = StructArray(("x", "y"), size=100, defaults=(0,0))
>>> glVertexPointer(2, GL_FLOAT,
...     array.get_data_stride(),
...     array.get_data_addr())

get_data_stride

get_data_stride()

Gets the number of bytes between the begining of each array element.

get_dimensions

get_size()

Returns a tuple of this array's dimensional size.

If it is a single dimension array, a tuple containing the length will be returned.

repeat

repeat(times)

Returns a repeated copy of this array.

For example, Array([1,2,3]).repeat(3) will return Array([1,2,3,1,2,3,1,2,3]).

This operation is not lazy.

sanitize_index

sanitize_index(index) -> sanitized index

This method takes an index, such as passed to __getitem__, and returns an index that is between 0 and len(self). (So that it can be used with the underlying C array.) This includes handling negitive and/or multidimensional indexes.

Slices are currently not supported.

set_length

set_length(length)

Sets the length explicitly.

This is an alternative to using append and extend. (It should be a little faster.)

stretch

stretch(times)

Returns a stretched copy this array. Each item is duplicated times times.

For example, Array([1,2,3]).stretch(3) will return Array([1,1,1,2,2,2,3,3,3]).

This operation is not lazy.