Merge pull request #11660 from BenjaminSchubert/bschubert/simplify-pprint
Remove more unused pprint features
This commit is contained in:
commit
803b190a17
|
@ -64,9 +64,6 @@ class PrettyPrinter:
|
||||||
indent: int = 4,
|
indent: int = 4,
|
||||||
width: int = 80,
|
width: int = 80,
|
||||||
depth: Optional[int] = None,
|
depth: Optional[int] = None,
|
||||||
*,
|
|
||||||
sort_dicts: bool = True,
|
|
||||||
underscore_numbers: bool = False,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Handle pretty printing operations onto a stream using a set of
|
"""Handle pretty printing operations onto a stream using a set of
|
||||||
configured parameters.
|
configured parameters.
|
||||||
|
@ -80,12 +77,7 @@ class PrettyPrinter:
|
||||||
depth
|
depth
|
||||||
The maximum depth to print out nested structures.
|
The maximum depth to print out nested structures.
|
||||||
|
|
||||||
sort_dicts
|
|
||||||
If true, dict keys are sorted.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
indent = int(indent)
|
|
||||||
width = int(width)
|
|
||||||
if indent < 0:
|
if indent < 0:
|
||||||
raise ValueError("indent must be >= 0")
|
raise ValueError("indent must be >= 0")
|
||||||
if depth is not None and depth <= 0:
|
if depth is not None and depth <= 0:
|
||||||
|
@ -95,8 +87,6 @@ class PrettyPrinter:
|
||||||
self._depth = depth
|
self._depth = depth
|
||||||
self._indent_per_level = indent
|
self._indent_per_level = indent
|
||||||
self._width = width
|
self._width = width
|
||||||
self._sort_dicts = sort_dicts
|
|
||||||
self._underscore_numbers = underscore_numbers
|
|
||||||
|
|
||||||
def pformat(self, object: Any) -> str:
|
def pformat(self, object: Any) -> str:
|
||||||
sio = _StringIO()
|
sio = _StringIO()
|
||||||
|
@ -174,10 +164,7 @@ class PrettyPrinter:
|
||||||
) -> None:
|
) -> None:
|
||||||
write = stream.write
|
write = stream.write
|
||||||
write("{")
|
write("{")
|
||||||
if self._sort_dicts:
|
|
||||||
items = sorted(object.items(), key=_safe_tuple)
|
items = sorted(object.items(), key=_safe_tuple)
|
||||||
else:
|
|
||||||
items = object.items()
|
|
||||||
self._format_dict_items(items, stream, indent, allowance, context, level)
|
self._format_dict_items(items, stream, indent, allowance, context, level)
|
||||||
write("}")
|
write("}")
|
||||||
|
|
||||||
|
@ -486,12 +473,7 @@ class PrettyPrinter:
|
||||||
write("\n" + " " * indent)
|
write("\n" + " " * indent)
|
||||||
|
|
||||||
def _repr(self, object: Any, context: Set[int], level: int) -> str:
|
def _repr(self, object: Any, context: Set[int], level: int) -> str:
|
||||||
return self.format(object, context.copy(), self._depth, level)
|
return self._safe_repr(object, context.copy(), self._depth, level)
|
||||||
|
|
||||||
def format(
|
|
||||||
self, object: Any, context: Set[int], maxlevels: Optional[int], level: int
|
|
||||||
) -> str:
|
|
||||||
return self._safe_repr(object, context, maxlevels, level)
|
|
||||||
|
|
||||||
def _pprint_default_dict(
|
def _pprint_default_dict(
|
||||||
self,
|
self,
|
||||||
|
@ -616,12 +598,6 @@ class PrettyPrinter:
|
||||||
|
|
||||||
r = getattr(typ, "__repr__", None)
|
r = getattr(typ, "__repr__", None)
|
||||||
|
|
||||||
if issubclass(typ, int) and r is int.__repr__:
|
|
||||||
if self._underscore_numbers:
|
|
||||||
return f"{object:_d}"
|
|
||||||
else:
|
|
||||||
return repr(object)
|
|
||||||
|
|
||||||
if issubclass(typ, dict) and r is dict.__repr__:
|
if issubclass(typ, dict) and r is dict.__repr__:
|
||||||
if not object:
|
if not object:
|
||||||
return "{}"
|
return "{}"
|
||||||
|
@ -634,13 +610,9 @@ class PrettyPrinter:
|
||||||
components: List[str] = []
|
components: List[str] = []
|
||||||
append = components.append
|
append = components.append
|
||||||
level += 1
|
level += 1
|
||||||
if self._sort_dicts:
|
for k, v in sorted(object.items(), key=_safe_tuple):
|
||||||
items = sorted(object.items(), key=_safe_tuple)
|
krepr = self._safe_repr(k, context, maxlevels, level)
|
||||||
else:
|
vrepr = self._safe_repr(v, context, maxlevels, level)
|
||||||
items = object.items()
|
|
||||||
for k, v in items:
|
|
||||||
krepr = self.format(k, context, maxlevels, level)
|
|
||||||
vrepr = self.format(v, context, maxlevels, level)
|
|
||||||
append(f"{krepr}: {vrepr}")
|
append(f"{krepr}: {vrepr}")
|
||||||
context.remove(objid)
|
context.remove(objid)
|
||||||
return "{%s}" % ", ".join(components)
|
return "{%s}" % ", ".join(components)
|
||||||
|
@ -668,7 +640,7 @@ class PrettyPrinter:
|
||||||
append = components.append
|
append = components.append
|
||||||
level += 1
|
level += 1
|
||||||
for o in object:
|
for o in object:
|
||||||
orepr = self.format(o, context, maxlevels, level)
|
orepr = self._safe_repr(o, context, maxlevels, level)
|
||||||
append(orepr)
|
append(orepr)
|
||||||
context.remove(objid)
|
context.remove(objid)
|
||||||
return format % ", ".join(components)
|
return format % ", ".join(components)
|
||||||
|
@ -676,7 +648,9 @@ class PrettyPrinter:
|
||||||
return repr(object)
|
return repr(object)
|
||||||
|
|
||||||
|
|
||||||
_builtin_scalars = frozenset({str, bytes, bytearray, float, complex, bool, type(None)})
|
_builtin_scalars = frozenset(
|
||||||
|
{str, bytes, bytearray, float, complex, bool, type(None), int}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _recursion(object: Any) -> str:
|
def _recursion(object: Any) -> str:
|
||||||
|
|
Loading…
Reference in New Issue