Class for safely making a text representation of a Python object.
| 1236 | # -------------------------------------------- text documentation generator |
| 1237 | |
| 1238 | class TextRepr(Repr): |
| 1239 | """Class for safely making a text representation of a Python object.""" |
| 1240 | def __init__(self): |
| 1241 | Repr.__init__(self) |
| 1242 | self.maxlist = self.maxtuple = 20 |
| 1243 | self.maxdict = 10 |
| 1244 | self.maxstring = self.maxother = 100 |
| 1245 | |
| 1246 | def repr1(self, x, level): |
| 1247 | if hasattr(type(x), '__name__'): |
| 1248 | methodname = 'repr_' + '_'.join(type(x).__name__.split()) |
| 1249 | if hasattr(self, methodname): |
| 1250 | return getattr(self, methodname)(x, level) |
| 1251 | return cram(stripid(repr(x)), self.maxother) |
| 1252 | |
| 1253 | def repr_string(self, x, level): |
| 1254 | test = cram(x, self.maxstring) |
| 1255 | testrepr = repr(test) |
| 1256 | if '\\' in test and '\\' not in replace(testrepr, r'\\', ''): |
| 1257 | # Backslashes are only literal in the string and are never |
| 1258 | # needed to make any special characters, so show a raw string. |
| 1259 | return 'r' + testrepr[0] + test + testrepr[0] |
| 1260 | return testrepr |
| 1261 | |
| 1262 | repr_str = repr_string |
| 1263 | |
| 1264 | def repr_instance(self, x, level): |
| 1265 | try: |
| 1266 | return cram(stripid(repr(x)), self.maxstring) |
| 1267 | except: |
| 1268 | return '<%s instance>' % x.__class__.__name__ |
| 1269 | |
| 1270 | class TextDoc(Doc): |
| 1271 | """Formatter class for text documentation.""" |