PyXR

c:\python24\lib \ repr.py



0001 """Redo the `...` (representation) but with limits on most sizes."""
0002 
0003 __all__ = ["Repr","repr"]
0004 
0005 import __builtin__
0006 from itertools import islice
0007 
0008 class Repr:
0009 
0010     def __init__(self):
0011         self.maxlevel = 6
0012         self.maxtuple = 6
0013         self.maxlist = 6
0014         self.maxarray = 5
0015         self.maxdict = 4
0016         self.maxset = 6
0017         self.maxfrozenset = 6
0018         self.maxdeque = 6
0019         self.maxstring = 30
0020         self.maxlong = 40
0021         self.maxother = 20
0022 
0023     def repr(self, x):
0024         return self.repr1(x, self.maxlevel)
0025 
0026     def repr1(self, x, level):
0027         typename = type(x).__name__
0028         if ' ' in typename:
0029             parts = typename.split()
0030             typename = '_'.join(parts)
0031         if hasattr(self, 'repr_' + typename):
0032             return getattr(self, 'repr_' + typename)(x, level)
0033         else:
0034             s = __builtin__.repr(x)
0035             if len(s) > self.maxother:
0036                 i = max(0, (self.maxother-3)//2)
0037                 j = max(0, self.maxother-3-i)
0038                 s = s[:i] + '...' + s[len(s)-j:]
0039             return s
0040 
0041     def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
0042         n = len(x)
0043         if level <= 0 and n:
0044             s = '...'
0045         else:
0046             newlevel = level - 1
0047             repr1 = self.repr1
0048             pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
0049             if n > maxiter:  pieces.append('...')
0050             s = ', '.join(pieces)
0051             if n == 1 and trail:  right = trail + right
0052         return '%s%s%s' % (left, s, right)
0053 
0054     def repr_tuple(self, x, level):
0055         return self._repr_iterable(x, level, '(', ')', self.maxlist, ',')
0056 
0057     def repr_list(self, x, level):
0058         return self._repr_iterable(x, level, '[', ']', self.maxlist)
0059 
0060     def repr_array(self, x, level):
0061         header = "array('%s', [" % x.typecode
0062         return self._repr_iterable(x, level, header, '])', self.maxarray)
0063 
0064     def repr_set(self, x, level):
0065         x = sorted(x)
0066         return self._repr_iterable(x, level, 'set([', '])', self.maxset)
0067 
0068     def repr_frozenset(self, x, level):
0069         x = sorted(x)
0070         return self._repr_iterable(x, level, 'frozenset([', '])',
0071                                    self.maxfrozenset)
0072 
0073     def repr_deque(self, x, level):
0074         return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)
0075 
0076     def repr_dict(self, x, level):
0077         n = len(x)
0078         if n == 0: return '{}'
0079         if level <= 0: return '{...}'
0080         newlevel = level - 1
0081         repr1 = self.repr1
0082         pieces = []
0083         for key in islice(sorted(x), self.maxdict):
0084             keyrepr = repr1(key, newlevel)
0085             valrepr = repr1(x[key], newlevel)
0086             pieces.append('%s: %s' % (keyrepr, valrepr))
0087         if n > self.maxdict: pieces.append('...')
0088         s = ', '.join(pieces)
0089         return '{%s}' % (s,)
0090 
0091     def repr_str(self, x, level):
0092         s = __builtin__.repr(x[:self.maxstring])
0093         if len(s) > self.maxstring:
0094             i = max(0, (self.maxstring-3)//2)
0095             j = max(0, self.maxstring-3-i)
0096             s = __builtin__.repr(x[:i] + x[len(x)-j:])
0097             s = s[:i] + '...' + s[len(s)-j:]
0098         return s
0099 
0100     def repr_long(self, x, level):
0101         s = __builtin__.repr(x) # XXX Hope this isn't too slow...
0102         if len(s) > self.maxlong:
0103             i = max(0, (self.maxlong-3)//2)
0104             j = max(0, self.maxlong-3-i)
0105             s = s[:i] + '...' + s[len(s)-j:]
0106         return s
0107 
0108     def repr_instance(self, x, level):
0109         try:
0110             s = __builtin__.repr(x)
0111             # Bugs in x.__repr__() can cause arbitrary
0112             # exceptions -- then make up something
0113         except:
0114             return '<%s instance at %x>' % (x.__class__.__name__, id(x))
0115         if len(s) > self.maxstring:
0116             i = max(0, (self.maxstring-3)//2)
0117             j = max(0, self.maxstring-3-i)
0118             s = s[:i] + '...' + s[len(s)-j:]
0119         return s
0120 
0121 aRepr = Repr()
0122 repr = aRepr.repr
0123 

Generated by PyXR 0.9.4
SourceForge.net Logo