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

Y

yinglcs

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.
 
J

James Stroud

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.:
''

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
 
7

7stud

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
 
A

Alex Martelli

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):
.... try:
.... exec(whatever, {})
.... except Exception, e:
.... errdic[e] = 1 + errdic.get(e, 0)
.... {SyntaxError('unexpected EOF while parsing', ('<string>', 1, 11,
'(syntax err')): 1, ZeroDivisionError('integer division or modulo by
zero',): 1}{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
 
D

Dennis Lee Bieber

if (ErrorHash.has_key(errcode)):
ErrorFailNo = ErrorHash[errcode]

ErrorHash[errcode] = ErrorFailNo + 1

else:
ErrorHash[errcode] = 1
Note that this entire if block cant be condensed to:

ErrorHash[errcode] = ErrorHash.get(errcode, 0) + 1

..get() returns the value associated with the first argument, if there is
one, otherwise it returns the second argument (here used as an
initializer to 0).
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've not tried to force errors, but as mentioned you may prefer to
convert the exception to a string representation...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top