On Aug 27, 2008, at 5:03 AM, Cecil Westerhof wrote:
> > I have a program in which I display severall tables. This is partly > generic, so I would like to have one general function and put the > specific functionality in the object definition itself. > > For example I have: > ##### > kmStandTable = sa.Table( > 'kmStand', metadata, > sa.Column('datum', sa.Date, primary_key = True), > sa.Column('beginStand', sa.Integer, nullable = False), > sa.Column('eindStand', sa.Integer, nullable = False), > ) > class KmStand(object): > def __repr__(self): > return '<KmStand: %10.10s, %6d, %6d>' % (self.datum, > self.beginStand, self.eindStand) > > columns = ( > ('Datum', 80), > ("BeginStand", 80), > ('EindStand', 80), > ('Verschil', 60), > ) > size = (320, 600) > title = 'Km Stand' > sa_orm.mapper(KmStand, kmStandTable) > ##### > > And in my main code I have: > ##### > i = 0 > for row in session.query(KmStand).all(): > if (row.beginStand == -1) or (row.eindStand == -1): > afstand = -1 > else: > afstand = row.eindStand - row.beginStand > addRow(self, i,('%10.10s' % (row.datum), '%6d' % > (row.beginStand), > '%6d' % (row.eindStand), '%5d' % (afstand))) > i = i + 1 > ##### > > This is quite a simple example. There are a few other tables where > there has to be done a lot more and is what has to be done dependend > on the previous record. > > What I would like to do is to put in my main code: > ##### > records = Table().getRecords() > i = 0 > for record in records: > addRow(self, i,record) > i = i + 1 > ##### > > And in my model: > ##### > kmStandTable = sa.Table( > 'kmStand', metadata, > sa.Column('datum', sa.Date, primary_key = True), > sa.Column('beginStand', sa.Integer, nullable = False), > sa.Column('eindStand', sa.Integer, nullable = False), > ) > class KmStand(object): > def __repr__(self): > return '<KmStand: %10.10s, %6d, %6d>' % (self.datum, > self.beginStand, self.eindStand) > > def getRecords(self): > records = [] > for record in kmStandTable.select().execute(): > if (record.beginStand == -1) or (record.eindStand == -1): > afstand = -1 > else: > afstand = record.eindStand - record.beginStand > records.append(('%10.10s' % (record.datum), '%6d' % > (record.beginStand), > '%6d' % (record.eindStand), '%5d' % > (afstand))) > return records > > columns = ( > ('Datum', 80), > ("BeginStand", 80), > ('EindStand', 80), > ('Verschil', 60), > ) > size = (320, 600) > title = 'Km Stand' > sa_orm.mapper(KmStand, kmStandTable) > ##### > > I tried this and it works. But is this a good way to do this? For > example is the returning of a big array not to expensive? At the > moment there are not that many records, but when the table grows, the > returned array will grow also. whats missing here is what "addRow()" does, so I can't see what it is you're trying to accomplish. In one example you're using the ORM to query for rows from "kmStand", in another you're hitting the table directly, so switching between those two idioms is something to be aware of (0.5 has been designed so that you shouldn't usually have to use select() constructs when using the ORM). If the goal is just "transfer all the rows from X to table Y", the most efficient way would be to use an INSERT/SELECT statement, which SQLA doesn't yet support as an expression construct but can be built using a plain string. The second most efficient way within Python is to just load the table as a list of tuples using select() constructs, and then push all the rows to the new table in one step as a list of dictionaries using a table.insert().execute([list of rows]). --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---