Skip to content

Instantly share code, notes, and snippets.

@justinabrahms
Created August 19, 2010 15:27
Show Gist options
  • Save justinabrahms/538146 to your computer and use it in GitHub Desktop.
Save justinabrahms/538146 to your computer and use it in GitHub Desktop.
from collections import defaultdict
import logging
class ErrorDict(defaultdict):
"""
Stores a list of errors per host, when initialized with a list.
>>> ed = ErrorDict()
>>> ed['google.com'].append('first')
>>> ed['google.com'].append('another error')
>>> ed['fark.com'].append('errorz')
>>> for host, error_list in ed.iteritems():
... print "%s" % host
... print '\n'.join(["\t%s" % (error) for error in error_list])
...
google.com
first
another error
fark.com
errorz
>>>
"""
def __init__(self, *args, **kwargs):
"""
Override the defaultfactory arg (original arg) as ErrorDict
should always have lists.
"""
super(ErrorDict, self).__init__(list, **kwargs)
def __setitem__(self, host, error):
logging.info('%s had an error: %s' % (host, error))
super(ErrorDict, self).__setitem__(host, error)
def pretty_print(self):
for key, val_list in self.iteritems():
print '%s' % key
print '\n'.join(['\t%s' % (val) for val in val_list])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment