|
6 | 6 | # flake8: noqa
|
7 | 7 | # type: ignore
|
8 | 8 |
|
9 |
| -# |
10 |
| -# Author: Fred L. Drake, Jr. |
11 |
| - |
| 9 | +# Original Author: Fred L. Drake, Jr. |
| 10 | + |
12 | 11 | #
|
13 | 12 | # This is a simple little module I wrote to make life easier. I didn't
|
14 | 13 | # see anything quite like it in the library, though I may have overlooked
|
15 | 14 | # something. I wrote this when I was trying to read some heavily nested
|
16 | 15 | # tuples with fairly non-descriptive content. This is modeled very much
|
17 | 16 | # after Lisp/Scheme - style pretty-printing of lists. If you find it
|
18 | 17 | # useful, thank small children who sleep at night.
|
19 |
| - |
20 |
| -"""Support to pretty-print lists, tuples, & dictionaries recursively. |
21 |
| -
|
22 |
| -Very simple, but useful, especially in debugging data structures. |
23 |
| -
|
24 |
| -Classes |
25 |
| -------- |
26 |
| -
|
27 |
| -PrettyPrinter() |
28 |
| - Handle pretty-printing operations onto a stream using a configured |
29 |
| - set of formatting parameters.htop |
30 |
| -
|
31 |
| -Functions |
32 |
| ---------- |
33 |
| -
|
34 |
| -pformat() |
35 |
| - Format a Python object into a pretty-printed representation. |
36 |
| -
|
37 |
| -pprint() |
38 |
| - Pretty-print a Python object to a stream [default is sys.stdout]. |
39 |
| -
|
40 |
| -saferepr() |
41 |
| - Generate a 'standard' repr()-like value, but protect against recursive |
42 |
| - data structures. |
43 |
| -
|
44 |
| -""" |
45 |
| - |
46 | 18 | import collections as _collections
|
47 | 19 | import dataclasses as _dataclasses
|
48 | 20 | import re
|
49 | 21 | import sys as _sys
|
50 | 22 | import types as _types
|
51 | 23 | from io import StringIO as _StringIO
|
52 | 24 |
|
53 |
| -__all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", |
54 |
| - "PrettyPrinter", "pp"] |
55 |
| - |
56 |
| - |
57 |
| -def pprint(object, stream=None, indent=1, width=80, depth=None, *, |
58 |
| - compact=False, sort_dicts=True, underscore_numbers=False): |
59 |
| - """Pretty-print a Python object to a stream [default is sys.stdout].""" |
60 |
| - printer = PrettyPrinter( |
61 |
| - stream=stream, indent=indent, width=width, depth=depth, |
62 |
| - compact=compact, sort_dicts=sort_dicts, |
63 |
| - underscore_numbers=underscore_numbers) |
64 |
| - printer.pprint(object) |
65 |
| - |
66 |
| -def pformat(object, indent=1, width=80, depth=None, *, |
67 |
| - compact=False, sort_dicts=True, underscore_numbers=False): |
68 |
| - """Format a Python object into a pretty-printed representation.""" |
69 |
| - return PrettyPrinter(indent=indent, width=width, depth=depth, |
70 |
| - compact=compact, sort_dicts=sort_dicts, |
71 |
| - underscore_numbers=underscore_numbers).pformat(object) |
72 |
| - |
73 |
| -def pp(object, *args, sort_dicts=False, **kwargs): |
74 |
| - """Pretty-print a Python object""" |
75 |
| - pprint(object, *args, sort_dicts=sort_dicts, **kwargs) |
76 |
| - |
77 |
| -def saferepr(object): |
78 |
| - """Version of repr() which can handle recursive data structures.""" |
79 |
| - return PrettyPrinter()._safe_repr(object, {}, None, 0)[0] |
80 |
| - |
81 |
| -def isreadable(object): |
82 |
| - """Determine if saferepr(object) is readable by eval().""" |
83 |
| - return PrettyPrinter()._safe_repr(object, {}, None, 0)[1] |
84 |
| - |
85 |
| -def isrecursive(object): |
86 |
| - """Determine if object requires a recursive representation.""" |
87 |
| - return PrettyPrinter()._safe_repr(object, {}, None, 0)[2] |
88 | 25 |
|
89 | 26 | class _safe_key:
|
90 | 27 | """Helper function for key functions when sorting unorderable objects.
|
@@ -157,23 +94,11 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
|
157 | 94 | self._sort_dicts = sort_dicts
|
158 | 95 | self._underscore_numbers = underscore_numbers
|
159 | 96 |
|
160 |
| - def pprint(self, object): |
161 |
| - if self._stream is not None: |
162 |
| - self._format(object, self._stream, 0, 0, {}, 0) |
163 |
| - self._stream.write("\n") |
164 |
| - |
165 | 97 | def pformat(self, object):
|
166 | 98 | sio = _StringIO()
|
167 | 99 | self._format(object, sio, 0, 0, {}, 0)
|
168 | 100 | return sio.getvalue()
|
169 | 101 |
|
170 |
| - def isrecursive(self, object): |
171 |
| - return self.format(object, {}, 0, 0)[2] |
172 |
| - |
173 |
| - def isreadable(self, object): |
174 |
| - s, readable, recursive = self.format(object, {}, 0, 0) |
175 |
| - return readable and not recursive |
176 |
| - |
177 | 102 | def _format(self, object, stream, indent, allowance, context, level):
|
178 | 103 | objid = id(object)
|
179 | 104 | if objid in context:
|
|
0 commit comments