args attribute of Exception objects

Sebastien de Menten sdementen at hotmail.com
Mon Apr 11 06:31:23 EDT 2005


Jeremy Bowers <jerf at jerf.org> wrote in message news:<pan.2005.04.08.16.07.30.85408 at jerf.org>...
> On Fri, 08 Apr 2005 09:32:37 +0000, Sébastien de Menten wrote:
> 
> > Hi,
> > 
> > When I need to make sense of a python exception, I often need to parse the 
> > string exception in order to retrieve the data.
> 
> What exactly are you doing with this info? (Every time I started to do
> this, I found a better way. Perhaps one of them will apply for you.)
> 
> (As a general comment, I'd point out that you don't have to check the
> entire error message; checking for a descriptive substring, while still
> not "safe", is at least safe*r*.)

I have symbolic expressions in a dictionnary like:

dct = dict( a = "b**2 + c", b = "cos(2.3) + sin(v)", v = "4", c =
"some_very_expensive_function(v)")

I want to build a function that finds the links between all those
expressions (think about computing dependencies between cells in a
spreadsheet).

All I do is:
def link(name):
   dependencies = {}
   while True:
      try:
         eval(dct[name], globals(), dependencies)
      except NameError,e:
         dependencies[e.args[0][6:-16]] = 1
      else:
         return dependencies

globals() can be replaced by a custom dictionnary for security
purposes.
variation on the theme can:
  - check SyntaxError and give interlligent feedback to user (BTW,
SyntaxError args are much smarter)
  - find or/and eval recursively the whole tree and keep in cache
values,...

Seb



More information about the Python-list mailing list