Created
November 27, 2012 14:52
-
-
Save minimal/4154625 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def trim(text, max=100): | |
"""Trim a string to `max` chars | |
""" | |
if len(text) > max: | |
return text[:max] | |
return text | |
class StatsDict(dict): | |
"""A dictionary that's repr trims the values of its keys so as not | |
to print too much | |
""" | |
tmpl = """{0}: {1},\n""" | |
def __repr__(self): | |
"""Print all keys with values trimmed | |
""" | |
text = "" | |
for key, value in self.iteritems(): | |
text += self.tmpl.format(key, trim(str(value))) | |
return text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment