org.python.core

Class PyArray

public class PyArray extends PySequence implements Cloneable, ClassDictInit

A wrapper class around native java arrays. Instances of PyArray are created either by java functions or directly by the jarray module.

See also the jarray module.

Constructor Summary
PyArray(PyArray toCopy)
PyArray(Class type, Object data)
PyArray(Class type, int n)
Method Summary
voidappend(PyObject value)
Append new value x to the end of the array.
static PyArrayarray(PyObject seq, char typecode)
static PyArrayarray(PyObject init, Class ctype)
Create a PyArray storing ctype types and being initialised with initialiser.
voidbyteswap()
"Byteswap" all items of the array.
static Classchar2class(char type)
Converts a character code for the array type to a Java Class.

The following character codes and their native types are supported:

Type code native type
zboolean
cchar
bbyte
hshort
iint
llong
ffloat
ddouble

static voidclassDictInit(PyObject dict)
Initialised class dictionary
Objectclone()
Implementation of Cloneable interface.
PyObjectcount(PyObject value)
Return the number of occurrences of x in the array.
voidextend(PyObject iterable)
Append items from iterable to the end of the array.
voidfromfile(PyObject f, int count)
Read count items (as machine values) from the file object f and append them to the end of the array.
voidfromlist(PyObject obj)
Append items from the list.
voidfromstring(String input)
Appends items from the string, interpreting the string as an array of machine values (as if it had been read from a file using the {@link #fromfile(PyObject, int) fromfile()} method).
ObjectgetArray()
Return the internal Java array storage of the PyArray instance
intgetItemsize()
Getter for the storage size of the array's type.

The sizes returned by this method represent the number of bytes used to store the type.

StringgetTypecode()
Getter for the type code of the array.
PyObjectindex(PyObject value)
Return the smallest i such that i is the index of the first occurrence of value in the array.
voidinsert(int index, PyObject value)
Insert a new item with value value in the array before position index.
PyObjectpop()
Removes the item with the index index from the array and returns it.
PyObjectpop(int index)
Removes the item with the index index from the array and returns it.
voidremove(PyObject value)
Remove the first occurrence of value from the array.
voidreverse()
Reverse the elements in the array
voidtofile(PyObject f)
Write all items (as machine values) to the file object f.
PyObjecttolist()
Convert the array to an ordinary list with the same items.
Stringtostring()
Convert the array to an array of machine values and return the string representation (the same sequence of bytes that would be written to a file by the {@link #tofile(PyObject) tofile()} method.)
static PyArrayzeros(int n, char typecode)
static PyArrayzeros(int n, Class ctype)
PyObject__add__(PyObject other)
Adds (appends) two PyArrays together
PyObject__findattr__(String name)
Finds the attribute.
int__len__()
Length of the array
PyString__repr__()
String representation of PyArray
Object__tojava__(Class c)

Constructor Detail

PyArray

public PyArray(PyArray toCopy)

PyArray

public PyArray(Class type, Object data)

PyArray

public PyArray(Class type, int n)

Method Detail

append

public void append(PyObject value)
Append new value x to the end of the array.

Parameters: value item to be appended to the array

array

public static PyArray array(PyObject seq, char typecode)

array

public static PyArray array(PyObject init, Class ctype)
Create a PyArray storing ctype types and being initialised with initialiser.

Parameters: init an initialiser for the array - can be PyString or PySequence (including PyArray) or iterable type. ctype Class type of the elements stored in the array.

Returns: a new PyArray

byteswap

public void byteswap()
"Byteswap" all items of the array. This is only supported for values which are 1, 2, 4, or 8 bytes in size; for other types of values, RuntimeError is raised. It is useful when reading data from a file written on a machine with a different byte order.

char2class

public static Class char2class(char type)
Converts a character code for the array type to a Java Class.

The following character codes and their native types are supported:

Type code native type
zboolean
cchar
bbyte
hshort
iint
llong
ffloat
ddouble

Parameters: type character code for the array type

Returns: Class of the native type

classDictInit

public static void classDictInit(PyObject dict)
Initialised class dictionary

clone

public Object clone()
Implementation of Cloneable interface.

Returns: copy of current PyArray

count

public PyObject count(PyObject value)
Return the number of occurrences of x in the array.

Parameters: value instances of the value to be counted

Returns: number of time value was found in the array.

extend

public void extend(PyObject iterable)
Append items from iterable to the end of the array. If iterable is another array, it must have exactly the same type code; if not, TypeError will be raised. If iterable is not an array, it must be iterable and its elements must be the right type to be appended to the array. Changed in version 2.4: Formerly, the argument could only be another array.

Parameters: iterable iterable object used to extend the array

fromfile

public void fromfile(PyObject f, int count)
Read count items (as machine values) from the file object f and append them to the end of the array. If less than count items are available, EOFError is raised, but the items that were available are still inserted into the array. f must be a real built-in file object; something else with a read() method won't do.

Parameters: f Python builtin file object to retrieve data count number of array elements to read

fromlist

public void fromlist(PyObject obj)
Append items from the list. This is equivalent to "for x in list: a.append(x)"except that if there is a type error, the array is unchanged.

Parameters: obj input list object that will be appended to the array

fromstring

public void fromstring(String input)
Appends items from the string, interpreting the string as an array of machine values (as if it had been read from a file using the {@link #fromfile(PyObject, int) fromfile()} method).

Parameters: input string of bytes containing array data

getArray

public Object getArray()
Return the internal Java array storage of the PyArray instance

Returns: the Array store.

getItemsize

public int getItemsize()
Getter for the storage size of the array's type.

The sizes returned by this method represent the number of bytes used to store the type. In the case of streams, this is the number of bytes written to, or read from a stream. For memory this value is the minimum number of bytes required to store the type.

This method is used by other methods to define read/write quanta from strings and streams.

Values returned are:

Type Size
boolean 1
byte 1
char 1
short 2
int 4
long 8
float 4
double 8

Returns: number of bytes used to store array type.

getTypecode

public String getTypecode()
Getter for the type code of the array. {@link #char2class(char) char2class} describes the possible type codes and their meaning.

Returns: single character type code for the array

index

public PyObject index(PyObject value)
Return the smallest i such that i is the index of the first occurrence of value in the array.

Parameters: value value to find the index of

Returns: index of the first occurance of value

insert

public void insert(int index, PyObject value)
Insert a new item with value value in the array before position index. Negative values are treated as being relative to the end of the array.

Parameters: index insert position value value to be inserted into array

pop

public PyObject pop()
Removes the item with the index index from the array and returns it. The optional argument defaults to -1, so that by default the last item is removed and returned.

pop

public PyObject pop(int index)
Removes the item with the index index from the array and returns it. The optional argument defaults to -1, so that by default the last item is removed and returned.

Parameters: index array location to be popped from the array

Returns: array element popped from index

remove

public void remove(PyObject value)
Remove the first occurrence of value from the array.

Parameters: value array value to be removed

reverse

public void reverse()
Reverse the elements in the array

tofile

public void tofile(PyObject f)
Write all items (as machine values) to the file object f.

Parameters: f Python builtin file object to write data

tolist

public PyObject tolist()
Convert the array to an ordinary list with the same items.

Returns: array contents as a list

tostring

public String tostring()
Convert the array to an array of machine values and return the string representation (the same sequence of bytes that would be written to a file by the {@link #tofile(PyObject) tofile()} method.)

zeros

public static PyArray zeros(int n, char typecode)

zeros

public static PyArray zeros(int n, Class ctype)

__add__

public PyObject __add__(PyObject other)
Adds (appends) two PyArrays together

Parameters: other a PyArray to be added to the instance

Returns: the result of the addition as a new PyArray instance

__findattr__

public PyObject __findattr__(String name)
Finds the attribute.

Parameters: name the name of the attribute of interest

Returns: the value for the attribute of the specified name

__len__

public int __len__()
Length of the array

Returns: number of elements in the array

__repr__

public PyString __repr__()
String representation of PyArray

Returns: string representation of PyArray

__tojava__

public Object __tojava__(Class c)

Parameters: c target Class for the conversion

Returns: Java object converted to required class type if possible.

Jython homepage