exec function

Discussion in 'Python' started by scottmallory@gmail.com, Feb 12, 2005.

  1. Guest

    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
    , Feb 12, 2005
    #1
    1. Advertising

  2. Jeff Epler Guest

    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-----
    Jeff Epler, Feb 12, 2005
    #2
    1. Advertising

  3. tertius Guest

    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))
    tertius, Feb 12, 2005
    #3
  4. scott m Guest

    Thanks, both of these examples will help me clean my code and improve
    it.

    Scott
    scott m, Feb 12, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Vaughan
    Replies:
    11
    Views:
    1,085
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,057
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    448
    Duncan Booth
    Jul 22, 2004
  4. Guillermo Riojas
    Replies:
    0
    Views:
    155
    Guillermo Riojas
    Nov 26, 2010
  5. Random Task
    Replies:
    12
    Views:
    631
    Joe Smith
    Dec 4, 2005
Loading...

Share This Page