pyspread FAQ

# FAQ

© Martin Manns 2009

Q: What kind of expressions can a cell contain?
A: A cell can contain any normal Python expression such as a list comprehension or a generator expression. However, it cannot contain statements such as

    for i in xrange(10): pass

If you want to program more complex algorithms, use the Macro Editor.
There, you can define one (!) function at a time that uses arbitrary Python code and is callable from any cell.

Example:
Type in the macro editor:

    def factorize(number):
        """Silly factorizing algorithm for demonstration purposes only"""
        counter = 1
        result = []
        while counter <= number:
            if number % counter == 0:
                result.append(counter)
            counter += 1
        return result

And in the cell:

     factorize(25)

Result is:

     [1 5 25]

Q: Is using the variable S the only way for reading values of a cell(s)?
A: The variable S is the very grid object. It can be used in various ways, such as using its attributes and methods. One example is the spread method.

The object S has the attributes sgrid (string grid), macros (Macro dict) and resultcache and unredo (do not use the last two). There are several methods to access and manipulate the data. Since the attribute sgrid is a numpy object grid, all numpy methods can be used directly. When accessing (and slicing) S, you actually call a method that evaluates S.fgrid. When you set an item in S, the sgrid item is set but it is not automatically updated. Have fun poking around in the S object.

Q: Is S.spread the only way to write to a cell using its absolute/relative address?
A: No. However, it is the preferred way.
Try this out:

    S[1,0,0] = '2'

Only after selecting the cell (1,0,0), the string 2 will appear and it will be updated.
Note the error message.

Q: How can I write to cells from within a macro?
A: S.spread is your friend
Macro:

    def test(a):
        S.spread(a, (1, 0, 0))

Grid:

    test(3)

Q: What are the boundaries for the number of rows/columns/sheets?
A: On a 64 bit system, these are limited by your memory (and maybe your stack restriction if any). However, for 32bit systems, the number of cells is limited to 2^28 because the memory consumption for 2^28 cells is at about 1.7 GB. Pyspread warns you about memory consumption if the grid gets larger. If you own a 64bit system and proceed, you should be able to create really large grids (especially with much RAM).
For really large and mostly empty grids, pyspread would have to use sparse grids. Since there are performance issues, sparse grids are currently not scheduled for implementation.

Q: I'd like to exact data from a saved .pys file it without using the GUI, e.g. I'd like to open one in a script and read things from it.
A: The .pys format is a bzip2-ed pickle dump of the numpy object array that contains the strings that are entered in the cells.
Example:

    >>> import bz2
    >>> infile=bz2.BZ2File('test.pys','r')
    >>> import cPickle as pickle
    >>> import numpy
    >>> data = pickle.load(infile)
    >>> data
    array([[[rpy = __import__("rpy"), 1, ],
    [, 3, ],
    [, -4, ],
    ...,
    [, , ],
    [, , ],
    [, , ]],

    [[__import__("math"), 2, ],
    [, 4, ],
    [, 5, ],
    ...,
    [, , ],
    [, , ],
    [, , ]],

    [[a = numpy.arange(-1,1,.001), 3, ],
    [, 35, ],
    [, 2, ],
    ...,
    [, , ],
    [, , ],
    [, , ]],

    ...,
    [[, , ],
    [, , ],
    [, , ],
    ...,
    [, , ],
    [, , ],
    [, , ]],

    [[, , ],
    [, , ],
    [, , ],
    ...,
    [, , ],
    [, , ],
    [, , ]],

    [[, , ],
    [, , ],
    [, , ],
    ...,
    [, , ],
    [, , ],
    [, , ]]], dtype=object)
    >>> infile.close()