Class for safely making a text representation of a Python object.
| 1183 | # -------------------------------------------- text documentation generator |
| 1184 | |
| 1185 | class TextRepr(Repr): |
| 1186 | """Class for safely making a text representation of a Python object.""" |
| 1187 | def __init__(self): |
| 1188 | Repr.__init__(self) |
| 1189 | self.maxlist = self.maxtuple = 20 |
| 1190 | self.maxdict = 10 |
| 1191 | self.maxstring = self.maxother = 100 |
| 1192 | |
| 1193 | def repr1(self, x, level): |
| 1194 | if hasattr(type(x), '__name__'): |
| 1195 | methodname = 'repr_' + '_'.join(type(x).__name__.split()) |
| 1196 | if hasattr(self, methodname): |
| 1197 | return getattr(self, methodname)(x, level) |
| 1198 | return cram(stripid(repr(x)), self.maxother) |
| 1199 | |
| 1200 | def repr_string(self, x, level): |
| 1201 | test = cram(x, self.maxstring) |
| 1202 | testrepr = repr(test) |
| 1203 | if '\\' in test and '\\' not in replace(testrepr, r'\\', ''): |
| 1204 | # Backslashes are only literal in the string and are never |
| 1205 | # needed to make any special characters, so show a raw string. |
| 1206 | return 'r' + testrepr[0] + test + testrepr[0] |
| 1207 | return testrepr |
| 1208 | |
| 1209 | repr_str = repr_string |
| 1210 | |
| 1211 | def repr_instance(self, x, level): |
| 1212 | try: |
| 1213 | return cram(stripid(repr(x)), self.maxstring) |
| 1214 | except: |
| 1215 | return '<%s instance>' % x.__class__.__name__ |
| 1216 | |
| 1217 | class TextDoc(Doc): |
| 1218 | """Formatter class for text documentation.""" |