<div class="gmail_quote">On Mon, Nov 30, 2009 at 1:12 PM, Dave Angel <span dir="ltr"><<a href="mailto:davea@ieee.org">davea@ieee.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Victor Subervi wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Sun, Nov 29, 2009 at 10:23 PM, Dave Angel <<a href="mailto:davea@ieee.org" target="_blank">davea@ieee.org</a>> wrote:<br>
<br>
  <br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
exec is a statement, and statements don't have "return values."   It's not<br>
a function, so there are no parentheses in its syntax, either.  exec is also<br>
a technique of last resort;  there's nearly always a better/safer/faster way<br>
to accomplish what you might want, but of course you don't say what that is.<br>
<br>
As for "returning" values, exec by default uses the same global space as<br>
your app, so you can just modify a global variable in your "called" code and<br>
use it afterwards.<br>
<br>
abc = 42<br>
value = 12<br>
exec "abc = %d" % value<br>
print abc<br>
<br>
    <br>
</blockquote>
<br>
Taking out the parenthesis did it! Thanks. Now, you state this is an option<br>
of last resort. Although this does indeed achieve my desired aim, here is a<br>
complete example of what I am trying to achieve. The following is from<br>
'createTables2.py':<br>
<br>
  for table in tables:<br>
    try:<br>
      exec 'from options import %s' % table<br>
    except:<br>
      pass<br>
    try:<br>
      exec '%s()' % table<br>
    except:<br>
      pass<br>
<br>
<br>
The following is from 'options.py':<br>
<br>
def jewelry(which=''):<br>
  code = []<br>
  names = []<br>
  meanings = []<br>
  code.append(['5', '5&frac12;', '6', '6&frac12;', '7', '7&frac12;', '8',<br>
'8&frac12;', '9', '9&frac12;', '10', '10&frac12;', '11', '11&frac12;', '12',<br>
'12&frac12;', '13', '13&frac12;'])<br>
  meanings.append('The standard ring sizes.')<br>
  names.append('ringSizes')<br>
  code.append(['Petite (7&#34;)', 'Average (7&frac12;&#34;)', 'Large<br>
(8&#34;)', 'Extra-large (8&frac12;&#34;)'])<br>
  meanings.append('The standard bracelet sizes.')<br>
  names.append('braceletSizes')<br>
  code.append(['16&#34;', '18&#34;', '20&#34;', '22&#34;', '24&#34;'])<br>
  meanings.append('The standard necklace sizes.')<br>
  names.append('necklaceSizes')<br>
  code.append(['14K gold', '18K gold', 'silver', '14K white gold', '18K<br>
white gold', 'platinum', 'tungsten', 'titanium'])<br>
  meanings.append('The standard jewelry metals.')<br>
  names.append('metals')<br>
  code.append(['diamond', 'emerald', 'ruby', 'sapphire', 'pearl', 'opal',<br>
'topaz', 'onyx', 'lapiz lazuli', 'tanzanite', 'garnet', 'quartz', 'rose<br>
quartz', 'amethyst', 'alexandrite', 'peridot', 'tourmaline', 'citrine',<br>
'turquoise'])<br>
  meanings.append('The standard jewelry stones.')<br>
  names.append('stones')<br>
  if which == '':<br>
    i = 0<br>
    all = ''<br>
    while i < len(meanings):<br>
      table = '%s\n' % meanings[i]<br>
      table += "<table>\n <tr>\n  <td colspan='8' align='center'>%s</td>\n<br>
</tr>" % names[i]<br>
      j = 0<br>
      for elt in code:<br>
        if (j + 8) % 8 == 0:<br>
          table += ' <tr>\n'<br>
        table += '  <td>%s</td>\n' % code[i]<br>
        if (j + 8) % 8 == 0:<br>
          table += ' </tr>\n'<br>
        j += 1<br>
      if table[-6:] != '</tr>\n':<br>
        table += ' </tr>\n'<br>
      table += '</table>\n'<br>
      all += table + '<br /><br />'<br>
      i += 1<br>
    print all<br>
<br>
This all works fine; however, if there is a better way of doing it, please<br>
  <br>
</blockquote>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
let me know.<br>
Thanks,<br>
V<br>
<br>
  <br>
</blockquote>
The parentheses can't do any harm for that particular expression, so that wasn't your problem.  But I'm glad you fixed whatever else was your problem.  I mentioned it because sometimes they can cause problems, and you shouldn't get in bad habits.  (things like if, return, and exec are all statements that take an expression, but do not need parentheses.)<br>

<br>
I'll throw in a comment here about a bare except.  Also a bad idea.  You could easily mask some other problem involved in the import, and the program silently continues.  If you know of a specific problem, or category of problems that you want to ignore, then pick an exception, or tuple of exceptions, to do that.<br>

<br>
<br>
The immediate question is how to get rid of exec.  You're using it two places.  First case, you're just using it to extract specific objects from that module's namespace.<br>
<br>
Assuming you've already imported options earlier in the code, you can replace<br>
   from options import xyzzy<br>
by<br>
   optfunc = options.xyzzy<br>
or<br>
   option_func = getattr(options, "xyzzy", None)<br>
or even<br>
   option_func = getattr(options, "xyzzy", dummyfunc)<br>
(where dummyfunc() is a function which takes no arguments and does nothing)<br>
<br>
Now, assuming you plan to call each such function immediately, you can just say<br>
   option_func()<br>
in the same loop.<br>
<br>
<br>
def  dummyfunc():<br>
    pass<br>
<br>
....<br>
   for table in tables:<br>
        option_func = getattr(options, table, dummyfunc)<br>
        option_func()<br>
<br>
If you didn't have the dummyfunc, you'd need an "if option_func" in there.<br>
<br>
<br>
Naturally, if this "tables" comes from the user, you need to give him some feedback, so perhaps dummyfunc isn't an empty function after all, but is supplied in options.py<br>
<br>
Other comments about your code:  Someone else has mentioned names, so I won't dwell on that.<br>
<br>
     if table[-6:] != '</tr>\n':<br>
<br>
should use  endswith(), and you won't run the risk of counting wrong if your literal changes.<br>
<br>
       if (j + 8) % 8 == 0:<br>
<br>
could be simpler:<br>
<br>
       if j % 8 == 0:<br>
<br>
The pattern:<br>
           j=0<br>
<br>
     for elt in code:<br>
should be replaced by:<br>
     for j, elt in enumerate(code):<br>
<br>
(and of course don't forget to then remove the j+=1 )<br>
<br>
You're using the indexing variable i when it'd make much more sense to use zip.  Or perhaps meanings and code should be a single list, with each item being a tuple.  That's what zip builds, after the fact.  But if you build it explicitly, you're less likely to accidentally have an extra or missing element in one of them, and have to deal with that bug.<br>

<br>
The zip approach might look something like:<br>
   for meaning, code_element in zip(meanings, code):<br>
<br>
and then whenever you're using meanings[i]  you use meaning, and whenever you're using code[i], you use code_element.  (Obviously name changes would help, since code is a list, but the name isn't plural)<br>
<br>
<br>
<br>
More generally, when I see code with that much data embedded in it, it cries out for templates.  They're known by many different names, but the idea is to write your data structures somewhere else, and manipulate them with the code, instead of embedding it all together.  I suspect that each of these functions looks very similar, and they each have their own set of bugs and glitches.  That's a good indicator that you need to separate the data.<br>

<br>
For my web work, I've written my own, very simple templating logic that's only as good as I needed.  So I can't comment on the various ones that are already available.  But the idea is you put data into one or more text files, which you systematically manipulate to produce your end result.  A particular text file might be comma delimited, or defined in sections like an .ini file, or both, or some other mechanism, such as xml.  But make it easy to parse, so you can concentrate on getting the data into its final form, consistently, and with common code for all your tables, parameterized by the stuff in the data files.<br>

<br></blockquote><div><br>Yeah, maybe on the next iteration after I've finished the shopping cart and got it working. I'm the one who does everything right now, so it's not an issue for me.<br>Thanks,<br>V<br>
</div></div>