By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

how can I put an Exception as the key of a hash table

P: n/a
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.

Thank you.

Apr 28 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
yi*****@gmail.com 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.

Thank you.
First, you may want

errcode = str(e)

instead of

errcode = e
Second, the empty string is probably because the Exception is
instantiated without a string. E.g.:
>>str(Exception())
''

Third, you must not be showing us everything, because an Exception can
be used as a key, but does not give an empty string upon printing. You
are definitely not showing us what it looks like when you print
ErrorHash, which would be helpful.

Fourth, that you are creating a table of error codes means that you are
attempting to transmogrify the way python handles exceptions into some
paradigm you have used in a language like FORTRAN and thus are not using
exceptions in their intended way. You might describe the reason you are
generating error codes and gather suggestions about a more pythonic
(i.e. reasonable) approach.

James
Apr 28 '07 #2

P: n/a

yi*****@gmail.com 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.

Thank you.
Apparently, the Exception class's __str__() method doesn't print
anything about the exception. That doesn't mean the exception is an
empty string though:
ErrorHash = {}

try:

raise ValueError

except Exception, e:
print "the exception is:", e, "<----"

if (ErrorHash.has_key(e)):
ErrorFailNo = ErrorHash[e]
ErrorHash[e] = ErrorFailNo + 1
else:
ErrorHash[e] = 1

print ErrorHash

Apr 28 '07 #3

P: n/a
yi*****@gmail.com <yi*****@gmail.comwrote:
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.htmlhas pretty good
advice about "how to ask questions the smart way").
Alex
Apr 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.