exec function

S

scottmallory

I'm a newbie to pthyon and I am creating an app with wxPython.
I have 7 tables (more on the way... they are feed from a database) that
need to be formatted in a certain way (DataTable() does this) and I am
just looking for a cleaner way to pass all of the Grids (grid_1,
grid_2, etc) through the same function.
I am currently doing this (ugly, but it works):

for i in tables.keys():
if datadict.has_key(i):
t = tables.get(i)
d = datadict.get(i)
r = rowdata.get(i)
exec('self.frame.%s.SetTable(DataTable(d, r[0]), True)' % t)


It works (slowly) and I have read that exec is very slow and bad
form...Can any one give me any pointers to clean this mess up?

frame = a wx.Frame (another file which is imported).

Thanks,
Scott
 
J

Jeff Epler

In this case, you can use getattr() instead of the exec statement:
getattr(self.frame, t).SetTable(DataTable(d, r[0]), True)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCDoB2Jd01MZaTXX0RAveLAKCjw2PwZ4j0KlQwZibeuXnowHYpgwCfaBgj
mTA3n7az8dhCu/4K/crYzrY=
=BjU4
-----END PGP SIGNATURE-----
 
T

tertius

I'm a newbie to pthyon and I am creating an app with wxPython.
I have 7 tables (more on the way... they are feed from a database) that
need to be formatted in a certain way (DataTable() does this) and I am
just looking for a cleaner way to pass all of the Grids (grid_1,
grid_2, etc) through the same function.
I am currently doing this (ugly, but it works):

for i in tables.keys():
if datadict.has_key(i):
t = tables.get(i)
d = datadict.get(i)
r = rowdata.get(i)
exec('self.frame.%s.SetTable(DataTable(d, r[0]), True)' % t)


It works (slowly) and I have read that exec is very slow and bad
form...Can any one give me any pointers to clean this mess up?

frame = a wx.Frame (another file which is imported).

Thanks,
Scott
def getUniqueCustomer(self,idcustomer): #,cols = "*"):
sql = """select * from customer where idcustomer=%d"""%(idcustomer)
self.db.sql_exec(sql)
if self.db.crsr.rowcount == 0:
return None
cols = list([i[0] for i in self.db.crsr.description])
vals = self.db.sql_fetchone()
return dict(zip(cols,vals))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top