Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: repr.py
"""Redo the builtin repr() (representation) but with limits on most sizes."""
[0] Fix | Delete
[1] Fix | Delete
__all__ = ["Repr","repr"]
[2] Fix | Delete
[3] Fix | Delete
import __builtin__
[4] Fix | Delete
from itertools import islice
[5] Fix | Delete
[6] Fix | Delete
class Repr:
[7] Fix | Delete
[8] Fix | Delete
def __init__(self):
[9] Fix | Delete
self.maxlevel = 6
[10] Fix | Delete
self.maxtuple = 6
[11] Fix | Delete
self.maxlist = 6
[12] Fix | Delete
self.maxarray = 5
[13] Fix | Delete
self.maxdict = 4
[14] Fix | Delete
self.maxset = 6
[15] Fix | Delete
self.maxfrozenset = 6
[16] Fix | Delete
self.maxdeque = 6
[17] Fix | Delete
self.maxstring = 30
[18] Fix | Delete
self.maxlong = 40
[19] Fix | Delete
self.maxother = 20
[20] Fix | Delete
[21] Fix | Delete
def repr(self, x):
[22] Fix | Delete
return self.repr1(x, self.maxlevel)
[23] Fix | Delete
[24] Fix | Delete
def repr1(self, x, level):
[25] Fix | Delete
typename = type(x).__name__
[26] Fix | Delete
if ' ' in typename:
[27] Fix | Delete
parts = typename.split()
[28] Fix | Delete
typename = '_'.join(parts)
[29] Fix | Delete
if hasattr(self, 'repr_' + typename):
[30] Fix | Delete
return getattr(self, 'repr_' + typename)(x, level)
[31] Fix | Delete
else:
[32] Fix | Delete
s = __builtin__.repr(x)
[33] Fix | Delete
if len(s) > self.maxother:
[34] Fix | Delete
i = max(0, (self.maxother-3)//2)
[35] Fix | Delete
j = max(0, self.maxother-3-i)
[36] Fix | Delete
s = s[:i] + '...' + s[len(s)-j:]
[37] Fix | Delete
return s
[38] Fix | Delete
[39] Fix | Delete
def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
[40] Fix | Delete
n = len(x)
[41] Fix | Delete
if level <= 0 and n:
[42] Fix | Delete
s = '...'
[43] Fix | Delete
else:
[44] Fix | Delete
newlevel = level - 1
[45] Fix | Delete
repr1 = self.repr1
[46] Fix | Delete
pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
[47] Fix | Delete
if n > maxiter: pieces.append('...')
[48] Fix | Delete
s = ', '.join(pieces)
[49] Fix | Delete
if n == 1 and trail: right = trail + right
[50] Fix | Delete
return '%s%s%s' % (left, s, right)
[51] Fix | Delete
[52] Fix | Delete
def repr_tuple(self, x, level):
[53] Fix | Delete
return self._repr_iterable(x, level, '(', ')', self.maxtuple, ',')
[54] Fix | Delete
[55] Fix | Delete
def repr_list(self, x, level):
[56] Fix | Delete
return self._repr_iterable(x, level, '[', ']', self.maxlist)
[57] Fix | Delete
[58] Fix | Delete
def repr_array(self, x, level):
[59] Fix | Delete
header = "array('%s', [" % x.typecode
[60] Fix | Delete
return self._repr_iterable(x, level, header, '])', self.maxarray)
[61] Fix | Delete
[62] Fix | Delete
def repr_set(self, x, level):
[63] Fix | Delete
x = _possibly_sorted(x)
[64] Fix | Delete
return self._repr_iterable(x, level, 'set([', '])', self.maxset)
[65] Fix | Delete
[66] Fix | Delete
def repr_frozenset(self, x, level):
[67] Fix | Delete
x = _possibly_sorted(x)
[68] Fix | Delete
return self._repr_iterable(x, level, 'frozenset([', '])',
[69] Fix | Delete
self.maxfrozenset)
[70] Fix | Delete
[71] Fix | Delete
def repr_deque(self, x, level):
[72] Fix | Delete
return self._repr_iterable(x, level, 'deque([', '])', self.maxdeque)
[73] Fix | Delete
[74] Fix | Delete
def repr_dict(self, x, level):
[75] Fix | Delete
n = len(x)
[76] Fix | Delete
if n == 0: return '{}'
[77] Fix | Delete
if level <= 0: return '{...}'
[78] Fix | Delete
newlevel = level - 1
[79] Fix | Delete
repr1 = self.repr1
[80] Fix | Delete
pieces = []
[81] Fix | Delete
for key in islice(_possibly_sorted(x), self.maxdict):
[82] Fix | Delete
keyrepr = repr1(key, newlevel)
[83] Fix | Delete
valrepr = repr1(x[key], newlevel)
[84] Fix | Delete
pieces.append('%s: %s' % (keyrepr, valrepr))
[85] Fix | Delete
if n > self.maxdict: pieces.append('...')
[86] Fix | Delete
s = ', '.join(pieces)
[87] Fix | Delete
return '{%s}' % (s,)
[88] Fix | Delete
[89] Fix | Delete
def repr_str(self, x, level):
[90] Fix | Delete
s = __builtin__.repr(x[:self.maxstring])
[91] Fix | Delete
if len(s) > self.maxstring:
[92] Fix | Delete
i = max(0, (self.maxstring-3)//2)
[93] Fix | Delete
j = max(0, self.maxstring-3-i)
[94] Fix | Delete
s = __builtin__.repr(x[:i] + x[len(x)-j:])
[95] Fix | Delete
s = s[:i] + '...' + s[len(s)-j:]
[96] Fix | Delete
return s
[97] Fix | Delete
[98] Fix | Delete
def repr_long(self, x, level):
[99] Fix | Delete
s = __builtin__.repr(x) # XXX Hope this isn't too slow...
[100] Fix | Delete
if len(s) > self.maxlong:
[101] Fix | Delete
i = max(0, (self.maxlong-3)//2)
[102] Fix | Delete
j = max(0, self.maxlong-3-i)
[103] Fix | Delete
s = s[:i] + '...' + s[len(s)-j:]
[104] Fix | Delete
return s
[105] Fix | Delete
[106] Fix | Delete
def repr_instance(self, x, level):
[107] Fix | Delete
try:
[108] Fix | Delete
s = __builtin__.repr(x)
[109] Fix | Delete
# Bugs in x.__repr__() can cause arbitrary
[110] Fix | Delete
# exceptions -- then make up something
[111] Fix | Delete
except Exception:
[112] Fix | Delete
return '<%s instance at %x>' % (x.__class__.__name__, id(x))
[113] Fix | Delete
if len(s) > self.maxstring:
[114] Fix | Delete
i = max(0, (self.maxstring-3)//2)
[115] Fix | Delete
j = max(0, self.maxstring-3-i)
[116] Fix | Delete
s = s[:i] + '...' + s[len(s)-j:]
[117] Fix | Delete
return s
[118] Fix | Delete
[119] Fix | Delete
[120] Fix | Delete
def _possibly_sorted(x):
[121] Fix | Delete
# Since not all sequences of items can be sorted and comparison
[122] Fix | Delete
# functions may raise arbitrary exceptions, return an unsorted
[123] Fix | Delete
# sequence in that case.
[124] Fix | Delete
try:
[125] Fix | Delete
return sorted(x)
[126] Fix | Delete
except Exception:
[127] Fix | Delete
return list(x)
[128] Fix | Delete
[129] Fix | Delete
aRepr = Repr()
[130] Fix | Delete
repr = aRepr.repr
[131] Fix | Delete
[132] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function