?Method to list out map object's keys on KeyError
Pettersen, Bjorn S
BjornPettersen at fairisaac.com
Fri Oct 10 22:22:52 EDT 2003
> From: Norman Shelley [mailto:Norman.Shelley at motorola.com]
>
> Is there a way to get at the object one is trying to access
> with a key to list
> out all the possible keys?
> See the except clause below.
>
> #!/usr/bin/env python2.2
> from inspect import *
> import sys
>
> program = """
> abc = {}
> print "abc:", abc
> print abc['def']
> """
>
>
> gdct = globals()
> ldct = {}
> try:
> exec program in gdct, ldct
> except KeyError:
> # Some way to get at the abc object to list out all its keys
> # e.g. abc.keys() Of course program could have any map name
> # that would fail and I would like that object's keys
> raise
Sorry, I don't have exactly what you're asking for, however below is
what has become the top of our production Python apps. It utilizes the
cgitb module to generate a traceback that includes the values of the
variables in nicely formatted html and sends it through email. It prints
out the traceback also, but in plain text...
The original request we had was identical to yours, so hopefully this
will at least give you some ideas?
-- bjorn
import os, sys, cgitb, time
from cStringIO import StringIO
class EMailExcept(cgitb.Hook):
#
# DO MAKE SURE: that the logdir exists
def __init__(self, emailaddress, logdir='errorlogs'):
self.errout = StringIO()
self.email = emailaddress
cgitb.Hook.__init__(self, 1, logdir, 5, self.errout)
def handle(self, info=None):
cgitb.Hook.handle(self, info)
errtxt = self.errout.getvalue()
import smtplib
from email.Generator import Generator
from email.MIMEText import MIMEText
msg = MIMEText(errtxt, 'html')
msg['Subject'] = 'Traceback from: ' + sys.argv[0]
msg['From'] = 'The Excel to tab-delimited converter'
msg['To'] = self.email
out = StringIO()
g = Generator(out)
g(msg)
s = smtplib.SMTP()
s.connect('exchange')
s.sendmail('Python', [self.email], out.getvalue())
s.close()
print errtxt # to notify operators
sys.exit(20)
sys.excepthook = EMailExcept('productionxxxx at xxxxxxx.xxx')
More information about the Python-list
mailing list