Skip to content

Commit e05721c

Browse files
committed
Add a somewhat-reasonable pprint
1 parent e09a25b commit e05721c

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

python-stdlib/pprint/pprint.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
1-
def pformat(obj):
2-
return repr(obj)
31

42

5-
def pprint(obj):
6-
print(repr(obj))
3+
4+
def _pprint(obj, stream=None, indent=0):
5+
if stream is None:
6+
import sys
7+
stream = sys.stdout
8+
9+
if isinstance(obj, dict):
10+
stream.write("{\n")
11+
for k, v in obj.items():
12+
stream.write(" "*indent)
13+
_pprint(k, stream, indent+1)
14+
stream.write(": ")
15+
_pprint(v, stream, indent+1)
16+
stream.write(",\n")
17+
stream.write(" "*indent+"}")
18+
else:
19+
print(repr(obj), file=stream, end="")
20+
21+
22+
def pformat(obj):
23+
import io
24+
buf = io.StringIO()
25+
_pprint(obj, buf)
26+
return buf.getvalue()
27+
28+
def pprint(obj, stream=None):
29+
_pprint(obj, stream)
30+
print(file=stream)

0 commit comments

Comments
 (0)