How to increase performance of wx.grid object that has 5,000,0000 cells and uses various background colors on each cell?

Hi Steve,

I have started working on this and will let you know about the result. Using a database is more professional, and I prefer it. But I don't understand and don't know how the application will know when it has to fetch data from database. If the grid is too wide, it won't fit to screen, and user will have to scroll to right to see the rest of the grid. Then the virtual table gets the data from dictionary (or database) on demand. How will I cache the necessary amount of data??

The grid will call the "GetValue" method you have overridden in your VirtualTable class when ever it needs to display data for the row/col. The "GetValue" method can then call whatever method you defined to get data from your cache, if it is not found in the cache you can then call your method to get the data from the database.

An entry in your cache should probably store the values for all the columns of that particular row.

Werner

P.S.
If you go the database route you should probably look into SQLAlchemy as a middleware to access your data.

P.S.2
As an alternative to using a grid you might also want to look into using a listctrl, see the DVC list samples in the wxPython demo or ObjectListView (see e.g. Mike's blog http://www.blog.pythonlibrary.org/index.php?s=objectlistview&submit=Search )

···

On 20/01/2014 13:36, steve wrote:

Hi Werner,

I’m a novice, so please don’t get tired of my newbie questions:

1- What do you mean by “your cache”? Is “cache” the dictionary? I don’t know how to put data in a cache. Could you please provide an example?
2- I use mysqldb module to access the MySQL database. Is SQLAlchemy better than mysqldb module? Does SQLAlchemy work with a MySQL database?

Best regards

···

On Monday, January 20, 2014 3:44:46 PM UTC+2, werner wrote:

Hi Steve,

On 20/01/2014 13:36, steve wrote:

I have started working on this and will let you know about the result.
Using a database is more professional, and I prefer it. But I don’t
understand and don’t know how the application will know when it has to
fetch data from database. If the grid is too wide, it won’t fit to
screen, and user will have to scroll to right to see the rest of the
grid. Then the virtual table gets the data from dictionary (or
database) on demand. How will I cache the necessary amount of data??

The grid will call the “GetValue” method you have overridden in your
VirtualTable class when ever it needs to display data for the row/col.

The “GetValue” method can then call whatever method you defined to get
data from your cache, if it is not found in the cache you can then call
your method to get the data from the database.

An entry in your cache should probably store the values for all the
columns of that particular row.

Werner

P.S.

If you go the database route you should probably look into SQLAlchemy as
a middleware to access your data.

P.S.2

As an alternative to using a grid you might also want to look into using
a listctrl, see the DVC list samples in the wxPython demo or
ObjectListView (see e.g. Mike’s blog
http://www.blog.pythonlibrary.org/index.php?s=objectlistview&submit=Search )

No problem at all, I was there too and sometimes/often still am
A dictionary would do fine, there is more sophisticated stuff out
there but I don’t know/use them.
Something like this (untested) with a dict:
self.dataCache = {0: {0: ‘row 0 col 0 data’, 1: ‘row 0 col 1
data’,}, 1: {0: ‘row 1 col 0 data’, 1: ‘row 1 col 1 data’,},}
SQLAlchemy is nicer (at least in my view) and it can use mysqldb as
one of its backends. The advantage of SA is that you do not depend
on one SQL database as a backend, with no or little change to your
code you can use SQLite, MySQL, Firebird, Oracle and many other
databases SA is supporting. The other thing I like I rarely have to
issue SQL commands, you can use e.g. “fn = Person.first_name” and SA
will do the necessary stuff to get the “first_name” column out of
the ‘person’ table.
SA is a big library, so it takes some time to learn it but I think
it is worse it. If you decide to go down that route I would suggest
to use the ‘Declarative’ extension:
Mike Driscoll and I have some time ago put a little demo application
together which shows the use of SA and wxPython.

···

Hi Steve,

  On 20/01/2014 17:06, steve wrote:

Hi Werner,

    I'm a novice, so please don't get tired of my newbie questions:

:wink:

    1- What do you mean by "your cache"? Is "cache" the dictionary?

I don’t know how to put data in a cache. Could you please
provide an example?

2- I use mysqldb module to access the MySQL
database. Is SQLAlchemy better than mysqldb module? Does
SQLAlchemy work with a MySQL database?

http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/declarative.html

http://www.blog.pythonlibrary.org/index.php?s=medialocker&submit=Search
https://bitbucket.org/driscollis/medialocker

I have tested different approaches for large, sparse grids (10**6 cells
filled out of 10**9 cells).

Tuples as keys turned out to be considerably faster than any nested
structure that I tested. The only faster alternative that I found was
the pysparse module (see http://pysparse.sourceforge.net/).

Martin

···

On Sat, 18 Jan 2014 16:58:39 -0800 Christopher Barker <pythonchb@gmail.com> wrote:

On Sat, Jan 18, 2014 at 1:52 PM, steve <oslocourse@gmail.com> wrote:
>
> self.data = {(0,0):"cell_content_1", (0,1):"cell_content_2", ...}
>
> Or do you recommend a list, tuple or a set instead of a dictonary?
> Which one is fastest?
>

well, I think a list of lists is the most natural, and will be very
efficient.

A dict like that would make sense if the data in the grid is sparse
-- i.e. lots of empty cells, then you wouldn't need to store anything
for the empty ones. But if there are not very many empty cells, then
a list of lists maps well to the rectangular nature of the grid.

I wouldn't use tuples, as they are immutable, you'd need to re-create
them if anything changed. It might be OK if it's read only data,
though.