exec function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • scottmallory@gmail.com

    #1

    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_ke y(i):
    t = tables.get(i)
    d = datadict.get(i)
    r = rowdata.get(i)
    exec('self.fram e.%s.SetTable(D ataTable(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

  • Jeff Epler

    #2
    Re: exec function

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

    Jeff

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

    iD8DBQFCDoB2Jd0 1MZaTXX0RAveLAK Cjw2PwZ4j0KlQwZ ibeuXnowHYpgwCf aBgj
    mTA3n7az8dhCu/4K/crYzrY=
    =BjU4
    -----END PGP SIGNATURE-----

    Comment

    • tertius

      #3
      Re: exec function

      scottmallory@gm ail.com wrote:
      [color=blue]
      > 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_ke y(i):
      > t = tables.get(i)
      > d = datadict.get(i)
      > r = rowdata.get(i)
      > exec('self.fram e.%s.SetTable(D ataTable(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
      >[/color]
      def getUniqueCustom er(self,idcusto mer): #,cols = "*"):
      sql = """select * from customer where idcustomer=%d"" "%(idcustom er)
      self.db.sql_exe c(sql)
      if self.db.crsr.ro wcount == 0:
      return None
      cols = list([i[0] for i in self.db.crsr.de scription])
      vals = self.db.sql_fet chone()
      return dict(zip(cols,v als))

      Comment

      • scott m

        #4
        Re: exec function

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

        Scott

        Comment

        Working...