What is the best data structure for a very simple spreadsheet?

MRAB python at mrabarnett.plus.com
Sun Jan 3 20:13:27 CET 2010


vsoler wrote:
> Hi,
> 
> Not sure this is the best group to post, but I cannot think of any
> other.
> 
> My application would contain a limited set of "cells" represented by
> the instances of a Cell class:
> 
> class Cell:
> ...
> 
> A1=Cell(7)
> A2=Cell(2*A1)
> A3=Cell(3*A1+A2)
> A4=Cell(A3*4)
> 
> Of course, A1 = 7, A2 = 14, A3 = 35 and A4 = 140
> 
> Now, I somehow want to be able to show a dependency tree
> 
> 1 level dependency trees
>   A1: None
>   A2: A1
>   A3: A1, A2
>   A4: A3
> 
> All levels dependency trees
> 
>   A1: None
>   A2: A1
>   A3: A1, A2
>   A4: A3, A2, A1
> 
> Leaf + values dependency trees:
> 
>   A1: 7
>   A2: A1=7, 2
>   A3: 3, A1=7, 2
>   A4: 3, A1=7, 2, 4
> 
> What I'd like to know is:
> 
> 1) what are, in your opinion, the basic elements of the Cell class?
> 2) Do I need a parser to evaluate the formulas like “3*A1+A2”? Can you
> recommend one library that already contains one?
> 3) Do I need a tree data structure to represent my data? would the
> tree be an attribute of the class instance?
> 
> I imagine a lot can be said on these questions. What I am looking for
> is some hints that help me get out of where I am now.
> 
> Any help is highly appreciated.
> 
As well as considering the other replies, you don't necessarily need to
parse the expressions yourself.

If A1 is a cell then 2*A1 tries to call the __rmul__ method of A1 (and
A1*2 tries to call the __mul__ method of A1), so you could have it
return a formula object that will multiply and return the numeric value
of self (A1) by 2 when its own numeric value is requested. This means
that a cell could be initialised with either a number or a formula.



More information about the Python-list mailing list