Change the "__ private" names to "_ protected"; this has been a pain for
subclassing so many times it should simply be changed.
This commit is contained in:
parent
1695bcb848
commit
e6691efa0f
|
@ -98,20 +98,20 @@ class PrettyPrinter:
|
||||||
assert indent >= 0
|
assert indent >= 0
|
||||||
assert depth is None or depth > 0, "depth must be > 0"
|
assert depth is None or depth > 0, "depth must be > 0"
|
||||||
assert width
|
assert width
|
||||||
self.__depth = depth
|
self._depth = depth
|
||||||
self.__indent_per_level = indent
|
self._indent_per_level = indent
|
||||||
self.__width = width
|
self._width = width
|
||||||
if stream is not None:
|
if stream is not None:
|
||||||
self.__stream = stream
|
self._stream = stream
|
||||||
else:
|
else:
|
||||||
self.__stream = sys.stdout
|
self._stream = sys.stdout
|
||||||
|
|
||||||
def pprint(self, object):
|
def pprint(self, object):
|
||||||
self.__stream.write(self.pformat(object) + "\n")
|
self._stream.write(self.pformat(object) + "\n")
|
||||||
|
|
||||||
def pformat(self, object):
|
def pformat(self, object):
|
||||||
sio = StringIO()
|
sio = StringIO()
|
||||||
self.__format(object, sio, 0, 0, {}, 0)
|
self._format(object, sio, 0, 0, {}, 0)
|
||||||
return sio.getvalue()
|
return sio.getvalue()
|
||||||
|
|
||||||
def isrecursive(self, object):
|
def isrecursive(self, object):
|
||||||
|
@ -121,43 +121,43 @@ class PrettyPrinter:
|
||||||
s, readable, recursive = self.format(object, {}, 0)
|
s, readable, recursive = self.format(object, {}, 0)
|
||||||
return readable and not recursive
|
return readable and not recursive
|
||||||
|
|
||||||
def __format(self, object, stream, indent, allowance, context, level):
|
def _format(self, object, stream, indent, allowance, context, level):
|
||||||
level = level + 1
|
level = level + 1
|
||||||
objid = _id(object)
|
objid = _id(object)
|
||||||
if objid in context:
|
if objid in context:
|
||||||
stream.write(_recursion(object))
|
stream.write(_recursion(object))
|
||||||
self.__recursive = True
|
self._recursive = True
|
||||||
self.__readable = False
|
self._readable = False
|
||||||
return
|
return
|
||||||
rep = self.__repr(object, context, level - 1)
|
rep = self._repr(object, context, level - 1)
|
||||||
typ = _type(object)
|
typ = _type(object)
|
||||||
sepLines = _len(rep) > (self.__width - 1 - indent - allowance)
|
sepLines = _len(rep) > (self._width - 1 - indent - allowance)
|
||||||
write = stream.write
|
write = stream.write
|
||||||
|
|
||||||
if sepLines:
|
if sepLines:
|
||||||
if typ is DictType:
|
if typ is DictType:
|
||||||
write('{')
|
write('{')
|
||||||
if self.__indent_per_level > 1:
|
if self._indent_per_level > 1:
|
||||||
write((self.__indent_per_level - 1) * ' ')
|
write((self._indent_per_level - 1) * ' ')
|
||||||
length = _len(object)
|
length = _len(object)
|
||||||
if length:
|
if length:
|
||||||
context[objid] = 1
|
context[objid] = 1
|
||||||
indent = indent + self.__indent_per_level
|
indent = indent + self._indent_per_level
|
||||||
items = object.items()
|
items = object.items()
|
||||||
items.sort()
|
items.sort()
|
||||||
key, ent = items[0]
|
key, ent = items[0]
|
||||||
rep = self.__repr(key, context, level)
|
rep = self._repr(key, context, level)
|
||||||
write(rep)
|
write(rep)
|
||||||
write(': ')
|
write(': ')
|
||||||
self.__format(ent, stream, indent + _len(rep) + 2,
|
self._format(ent, stream, indent + _len(rep) + 2,
|
||||||
allowance + 1, context, level)
|
allowance + 1, context, level)
|
||||||
if length > 1:
|
if length > 1:
|
||||||
for key, ent in items[1:]:
|
for key, ent in items[1:]:
|
||||||
rep = self.__repr(key, context, level)
|
rep = self._repr(key, context, level)
|
||||||
write(',\n%s%s: ' % (' '*indent, rep))
|
write(',\n%s%s: ' % (' '*indent, rep))
|
||||||
self.__format(ent, stream, indent + _len(rep) + 2,
|
self._format(ent, stream, indent + _len(rep) + 2,
|
||||||
allowance + 1, context, level)
|
allowance + 1, context, level)
|
||||||
indent = indent - self.__indent_per_level
|
indent = indent - self._indent_per_level
|
||||||
del context[objid]
|
del context[objid]
|
||||||
write('}')
|
write('}')
|
||||||
return
|
return
|
||||||
|
@ -169,20 +169,20 @@ class PrettyPrinter:
|
||||||
else:
|
else:
|
||||||
write('(')
|
write('(')
|
||||||
endchar = ')'
|
endchar = ')'
|
||||||
if self.__indent_per_level > 1:
|
if self._indent_per_level > 1:
|
||||||
write((self.__indent_per_level - 1) * ' ')
|
write((self._indent_per_level - 1) * ' ')
|
||||||
length = _len(object)
|
length = _len(object)
|
||||||
if length:
|
if length:
|
||||||
context[objid] = 1
|
context[objid] = 1
|
||||||
indent = indent + self.__indent_per_level
|
indent = indent + self._indent_per_level
|
||||||
self.__format(object[0], stream, indent, allowance + 1,
|
self._format(object[0], stream, indent, allowance + 1,
|
||||||
context, level)
|
context, level)
|
||||||
if length > 1:
|
if length > 1:
|
||||||
for ent in object[1:]:
|
for ent in object[1:]:
|
||||||
write(',\n' + ' '*indent)
|
write(',\n' + ' '*indent)
|
||||||
self.__format(ent, stream, indent,
|
self._format(ent, stream, indent,
|
||||||
allowance + 1, context, level)
|
allowance + 1, context, level)
|
||||||
indent = indent - self.__indent_per_level
|
indent = indent - self._indent_per_level
|
||||||
del context[objid]
|
del context[objid]
|
||||||
if typ is TupleType and length == 1:
|
if typ is TupleType and length == 1:
|
||||||
write(',')
|
write(',')
|
||||||
|
@ -191,13 +191,13 @@ class PrettyPrinter:
|
||||||
|
|
||||||
write(rep)
|
write(rep)
|
||||||
|
|
||||||
def __repr(self, object, context, level):
|
def _repr(self, object, context, level):
|
||||||
repr, readable, recursive = self.format(object, context.copy(),
|
repr, readable, recursive = self.format(object, context.copy(),
|
||||||
self.__depth, level)
|
self._depth, level)
|
||||||
if not readable:
|
if not readable:
|
||||||
self.__readable = False
|
self._readable = False
|
||||||
if recursive:
|
if recursive:
|
||||||
self.__recursive = True
|
self._recursive = True
|
||||||
return repr
|
return repr
|
||||||
|
|
||||||
def format(self, object, context, maxlevels, level):
|
def format(self, object, context, maxlevels, level):
|
||||||
|
|
Loading…
Reference in New Issue