GenericTable and query from ORM level of SQLalchemy

Hello,

I am reading the WXPyhon In Action Rapin/Dun Book.
And I would like to adapt methods specialy the GetValue() of
GenericTable object (p133) for using an other kind
of data wich is not a tuple of tuble but a
query.all() or a query.fiter_by from SQLalchemy interface.

def GetValue(self, row, col):
  retrun self.data[row][col]
  
easy for a tuble of tuble.

But for query object, how can I index it?

I could make a tuple of tuble, as an intermediate, from the
query.all().
But is there any more pythonistic code

thanks for your help

laurent FRANCOIS wrote:

Hello,

I am reading the WXPyhon In Action Rapin/Dun Book. And I would like to adapt methods specialy the GetValue() of
GenericTable object (p133) for using an other kind of data wich is not a tuple of tuble but a
query.all() or a query.fiter_by from SQLalchemy interface.

def GetValue(self, row, col):
  retrun self.data[row][col]
  
easy for a tuble of tuble.

But for query object, how can I index it?

I could make a tuple of tuble, as an intermediate, from the
query.all(). But is there any more pythonistic code

thanks for your help
  
I would recommend the sqlalchemy mailing list (Page Moved) for this question. You'll probably just have to create some indexer yourself unless SQLA has one built in.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

But for query object, how can I index it?

I could make a tuple of tuble, as an intermediate, from the
query.all().
But is there any more pythonistic code

Instead of specifying column and row, couldn't you specify attribute
name and row?

The row number selects the index of the record object, then you use an
attribute name to select the 'column'.

Another option is to query on Table rather than query on object.
I.e.

session.query(Table).filter....all()
rather than
session.query(UserObject).filter..all()

this will return a tuple of tuples instead of tuple of objects.

If you find any other solutions I would be interested to know, thanks.

Cheers,

Jervis

laurent FRANCOIS wrote:

Hello,

I am reading the WXPyhon In Action Rapin/Dun Book. And I would like to adapt methods specialy the GetValue() of
GenericTable object (p133) for using an other kind of data wich is not a tuple of tuble but a
query.all() or a query.fiter_by from SQLalchemy interface.

def GetValue(self, row, col):
  retrun self.data[row][col]
  
easy for a tuble of tuble.

But for query object, how can I index it?

I could make a tuple of tuble, as an intermediate, from the
query.all().

The only requirement of GetValue is that it returns the value to be displayed for the cell at the given row/col. How it does that or where it fetches the values from is entirely up to you. The sample in the book is using a simple sequence of sequences because the sample is not trying to show you how to manage data, but rather how to integrate it into a Grid widget.

My suggestion would be to take a step back and look at the structure of the data you want to display and what format it is in when you get it from the DB. Think about how that maps to the rows/cols that will be on screen, and then write a GetValue that does that mapping. In the book that is done with self.data[row][col]. In your case it might be something that is very specific to your data model, for example:

  obj = self.dataModel.lastQuery().getItem(row)
  text = [obj.id, obj.name, obj.description][col]

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!