473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

exec function

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

Jul 18 '05 #1
3 1696
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)

iD8DBQFCDoB2Jd01MZaTXX0RAveLAKCjw2PwZ4j0KlQwZibeuX nowHYpgwCfaBgj
mTA3n7az8dhCu/4K/crYzrY=
=BjU4
-----END PGP SIGNATURE-----

Jul 18 '05 #2
sc**********@gmail.com wrote:
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))
Jul 18 '05 #3
Thanks, both of these examples will help me clean my code and improve
it.

Scott

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
5
by: Nick Jacobson | last post by:
This works fine: x = 1 def execfunc(): print x execfunc() So why doesn't this? s = \
1
by: Andr? Roberge | last post by:
I have the following two files: #--testexec.py-- def exec_code(co): try: exec co except: print "error" #-- test.py--
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
5
by: TPJ | last post by:
I have the following code: ----------------------------------- def f(): def g(): a = 'a' # marked line 1 exec 'a = "b"' in globals(), locals() print "g: a =", a
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
4
by: carl.dhalluin | last post by:
Hello I am completely puzzled why the following exec code does not work: mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)" def execute(): exec mycode execute()
23
by: Maarten | last post by:
Howdy, Recently I switched from a Windows PC to Mac OS-X 10.5 (php v5.2.6) and I have a little problem with one function within my cd-management script. For extracting a bit of info from my...
0
by: Stef Mientki | last post by:
Terry Reedy wrote: The locals of the code block where the instance of this class is created. No I think it's indeed handled as function call. I now that, because the globals (and sometimes ??? the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.