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.
--
Cecil Westerhof
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---