[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I want to keep track of the number of different exception happens in > my python program: > > ErrorHash = {} > > try: > > # come code ... > > except Exception, e: > print e > errcode = e > > if (ErrorHash.has_key(errcode)): > ErrorFailNo = ErrorHash[errcode] > > ErrorHash[errcode] = ErrorFailNo + 1 > > else: > ErrorHash[errcode] = 1 > > > But when i print out the ErrorHash like this: > > print ErrorHash > > i get an empty string. Can you please tell me how can I put an > Exception as the key of a hash table ? Or how can i dump out all the > content of the hashtable.
I can't reproduce your problem (using simpler code of course, yours is wildly redundant and needlessly complicated, though it should be semantically equivalent): >>> errdic = {} >>> def f(whatever): ... try: ... exec(whatever, {}) ... except Exception, e: ... errdic[e] = 1 + errdic.get(e, 0) ... >>> f('(syntax err') >>> errdic {SyntaxError('unexpected EOF while parsing', ('<string>', 1, 11, '(syntax err')): 1} >>> f('23/0') >>> errdic {SyntaxError('unexpected EOF while parsing', ('<string>', 1, 11, '(syntax err')): 1, ZeroDivisionError('integer division or modulo by zero',): 1} >>> f('23/0') >>> errdic {SyntaxError('unexpected EOF while parsing', ('<string>', 1, 11, '(syntax err')): 1, ZeroDivisionError('integer division or modulo by zero',): 1, ZeroDivisionError('integer division or modulo by zero',): 1} >>> As you can see, the only problem is that two occurrences of the same error ar kept distinct -- they're distinct instances of the same class without special hashing or comparison methods, and so are distinct when considered as keys into a dict. There are of course ways to "collapse" them (e.g, use (type(e), str(e)) as the dict key, or, many others), but at any rate this does not appear to have anything to do with the problem you report. Could you perhaps post a complete, self-sufficient short script or interactive interpreter session, such as the few lines I've just copied and pasted above, to help us understand your problem? (More generally, <http://www.catb.org/~esr/faqs/smart-questions.html> has pretty good advice about "how to ask questions the smart way"). Alex -- http://mail.python.org/mailman/listinfo/python-list