# Pyspread Tutorial

© Martin Manns 2009

## Running pyspread

Run pyspread with

    $ pyspread

Select the Menu File → New
Enter 200 rows, 10 columns and 5 tables in the pop-up menu.

After clicking OK, you get a new table with the typed-in dimensions.

## Standard cell commands

Select the top-left cell and type:

    1 + 5 * 2

The spreadsheet evaluates this Python statement and displays the result:

    11

In the cell that is one row below (cell (1, 0, 0)), type

    S

As we see from the result, S is a known object. In fact, it is the grid object that we are currently working in.

## Absolute addressing of cells

To access a cell, we can index the grid. Replace S with

    S[0, 0, 0]

and the same result as in the top-left cell that has the index (0, 0, 0) is displayed.
The first index is the row, the second parameter is the column and the third parameter is the table.
Now replace the expression in the top-left cell by

    1

Both cells change immediately because all visible cells are updated.

The main grid S can be sliced, too.
Write into cell (3, 0, 0):

    S[:2, 0, 0]

It now displays [1 1], which is a list of the results of the cells in [:2, 0, 0].

## Relative addressing of cells

Since cells are addressed via slicing, the cell content behaves similar to absolute addressing in other spreadsheets. In order to achieve relative addressing, three magic variables X (row), Y (column) and Z (table) are used.
These magic variables correspond to the position of the current cell in the grid.

Change to table 2 by selecting 2 in the iconbar combobox. Type into cell (1, 2, 2):

    [X, Y, Z]

The result is [1 2 2] as expected. Now copy the cell (Crtl-C) and paste it into the next lower cell (Ctrl-V). [2 2 2] is displayed.<p> Therefore, relative addressing is achieved. <p> Note that if cells are called from within other cells, the innermost cell is considered the current cell and its position is returned.

## Filling cells

The spread method of the grid S provides an easy way to fill cells with sequences.
(Note: The syntax has changed in 0.0.10)
Write into cell (0, 1, 2) and (1, 1, 2):

    range(100)

    S.spread(S[X - 1, Y, Z], (0, 0, 2))

When changing the focus to a new cell, the new values appear. You may delete the cell (1, 1, 2) again in order to prevent pyspread from overwriting the cells again and again.

## Named cells

Cells can be named by preceding the Python expression with “<name> =”. Type into cell (2, 4, 2):

    a = 3 * 5

and in cell (3, 4, 2):

    a ** 2

The results 15 and 225 appear. a is globally available in all cells.

## External modules

External modules can be imported into pyspread. Therefore, powerful types and manipulation methods are available.
Note: The package gmpy that is not part of the Python stock installation is required for the following example (use the current version at code.google.com).
Type into cell (5, 2, 2):

    gmpy = _ _ import _ _("gmpy")

<module 'gmpy' etc. is displayed. Now we redefine the rational number object in cell (6, 2, 2) in order to reduce typing and type in two rationals in the next two cells:

    q = gmpy.mpq

    q(1, 37)

    q(229, 13)

In the next cell (9, 2, 2) type:

    S[X - 2, Y, Z] + S[X - 1, Y, Z]

The result is 8486/481.

## Working with cells

Summing up cells:
Assuming that the cells (1,0,0) - (15, 0, 0) contain the values 1 to 15, entering into cell (16,0,0):

    sum(S[1:16,0,0])

yields 120 as expected.

However, if there are more columns (or tables) to sum up, each row is summed up individually. Therefore, copying the data to column 2 and changing the cell (16,0,0) to

    sum(S[1:16,0:2,0])

yields [120 120].

If everything shall be summed, the numpy.sum function has to be used:

    numpy.sum(S[1:16,0:2,0])

yields 240.

## Plotting

Note: The package rpy that is not part of the Python stock installation is required for the following example.
Switch to table 3. Import rpy in cell (0, 0, 3):

    rpy = _ _ import _ _("rpy")

Import the math module in cell (0, 1, 3):

    math = _ _ import _ _("math")

Create the x-values for a xy plot with the arange function of the package numpy. (numpy is accessible without any import because the grid S consists basically of wrapped numpy arrays.)
Type into cell (1, 0, 3):

    numpy.arange(0.0, 10.0, 0.1)

Create the y value list in cell (2, 0, 3):

    [math.sin(x) for x in S[1, 0, 3]]

Now plot everything by typing into cell (3, 0, 3):

    rpy.r.plot(S[1, 0, 3], S[2, 0, 3], xlab='', ylab='', type='l')