Eval Problem

Victor Subervi victorsubervi at gmail.com
Tue Apr 7 08:38:19 EDT 2009


Yes, Python is dynamic, but my server farm, in this era of big business
screwing the client and cutting all services and customer support, uses such
crappy hardware that all normal code breaks, so I am forced to find
work-arounds. Here is more complete code:

ourFile = string.split(__file__, "/")
p = ourFile[len(ourFile) - 1]
p = p[: - 9]
site = ourFile[4]
bitsFile = p + ".bits"
bitties = 0
bits = []

Above, I define a bits file with bits of code that the broken python
interpreter will not evaluate with normal text, so must be separated into
its own file and called line by line and blended with separated files that
quote only text. Pain in the hind end ;)

try:
  file = open(bitsFile, "r")
  for line in file:
    if len(line) > 2:
      bits.append(line)
      bitties += 1
except:
  pass
x = 1
while x <= bitties:
  file = open(p + str(x) + ".txt")
  for line in file:
    print line
  print eval(bits[x - 1])
  x += 1

I have excluded the code where I call the separate text files for printing
normal text. They work. It's my code that I cannot get to work. For example,
if I take out the "eval" part of the above, it will nicely print the
commands, such as this:

tableTop(123,456)

which is supposed to call said fn. If I place that line in the file calling
the text files and the bits file it will execute just fine, but that
inevitably makes my job harder. Ideas?
TIA,
Victor

On Mon, Apr 6, 2009 at 6:37 PM, J. Clifford Dyer <jcd at sdf.lonestar.org>wrote:

> On Mon, 2009-04-06 at 15:11 -0400, Victor Subervi wrote:
> > Hi:
> > I have this code:
> >
> > x = 1
> > while x <= bitties:
> >   file = open(p + str(x) + ".txt")
> >   for line in file:
> >     print line
> >   print eval(bits[x - 1])
> >   x += 1
> >
> > which throws this error:
> >
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: Traceback (most recent call
> > last):
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: File
> > "/usr/lib64/python2.4/site-packages/mod_python/apache.py", line 299,
> > in HandlerDispatch\n result = object(req)
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: File
> > "/usr/lib64/python2.4/site-packages/mod_python/cgihandler.py", line
> > 96, in handler\n imp.load_module(module_name, fd, path, desc)
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: File
> > "/var/www/vhosts/articles.13gems.com/httpdocs/index_frame.py", line
> > 89, in ?\n print eval(bits[1])
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: File "<string>", line 1
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: tableBottom(348,180)
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: ^
> > [Mon Apr 06 12:07:29 2009] [error] [client 190.166.0.221]
> > PythonHandler mod_python.cgihandler: SyntaxError: invalid syntax
> >
>
> Hmm.  That's not the problem I get.  For me, your code raises:
> "NameError: name 'bitties' is not defined"
>
> It's easier for us to help you if you present a self-contained piece of
> code that exhibits the problem you've encountered.  Creating that code
> will often reveal the problem with the original code, and you will have
> solved your own problem.
>
> The other problem with your code is that you are using eval.  Eval leads
> to difficult-to-debug errors, and is usually unnecessary, given the
> dynamic nature of python.  If you need to access a class method using a
> string, you can use getattr.  If you need to dynamically select a
> function based on some condition known in another variable, you can use
> a dictionary to hash into the function.
>
> It's hard to say what might be appropriate for your situation, because
> your code fragment is so... fragmentary.
>
> Cheers,
> Cliff
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090407/db4bb341/attachment-0001.html>


More information about the Python-list mailing list