Sebastien de Menten wrote:
Jeremy Bowers <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
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
Wouldn't it actually be better to use a namespace that reported attempts
to use its members? Then when you evaluated an expression you would be
in control of how the attempted accesses were recorded, and could
provide exactly the needed information.
regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
--
http://mail.python.org/mailman/listinfo/python-list