Mercurial > cpython-lru_cache-weakref
annotate Lib/pprint.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 1802a9edb4ee |
| children | d26d8575ac62 |
| rev | line source |
|---|---|
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
1 # Author: Fred L. Drake, Jr. |
|
20348
425c6c558fac
Remove obsolete email address.
Fred Drake <fdrake@acm.org>
parents:
19697
diff
changeset
|
2 # fdrake@acm.org |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 # |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 # This is a simple little module I wrote to make life easier. I didn't |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 # see anything quite like it in the library, though I may have overlooked |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 # something. I wrote this when I was trying to read some heavily nested |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
11620
diff
changeset
|
7 # tuples with fairly non-descriptive content. This is modeled very much |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 # after Lisp/Scheme - style pretty-printing of lists. If you find it |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 # useful, thank small children who sleep at night. |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 """Support to pretty-print lists, tuples, & dictionaries recursively. |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 Very simple, but useful, especially in debugging data structures. |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
15 Classes |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
16 ------- |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
17 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
18 PrettyPrinter() |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
19 Handle pretty-printing operations onto a stream using a configured |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
20 set of formatting parameters. |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
21 |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 Functions |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 --------- |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 pformat() |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 Format a Python object into a pretty-printed representation. |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 pprint() |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
29 Pretty-print a Python object to a stream [default is sys.sydout]. |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
31 saferepr() |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
32 Generate a 'standard' repr()-like value, but protect against recursive |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
33 data structures. |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 """ |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 |
|
19697
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
37 from types import DictType, ListType, TupleType, StringType |
|
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
38 import sys |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
40 try: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
41 from cStringIO import StringIO |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
42 except ImportError: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
43 from StringIO import StringIO |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
16562
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
13620
diff
changeset
|
45 __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", |
|
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
13620
diff
changeset
|
46 "PrettyPrinter"] |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
48 # cache these for faster access: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
49 _commajoin = ", ".join |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
50 _sys_modules = sys.modules |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
51 _id = id |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
52 _len = len |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
53 _type = type |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
54 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
55 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
56 def pprint(object, stream=None): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
57 """Pretty-print a Python object to a stream [default is sys.sydout].""" |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
58 printer = PrettyPrinter(stream=stream) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
59 printer.pprint(object) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
60 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
61 def pformat(object): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
62 """Format a Python object into a pretty-printed representation.""" |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
63 return PrettyPrinter().pformat(object) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
64 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
65 def saferepr(object): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
66 """Version of repr() which can handle recursive data structures.""" |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
67 return _safe_repr(object, {}, None, 0)[0] |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
69 def isreadable(object): |
|
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
70 """Determine if saferepr(object) is readable by eval().""" |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
71 return _safe_repr(object, {}, None, 0)[1] |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
72 |
|
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
73 def isrecursive(object): |
|
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
74 """Determine if object requires a recursive representation.""" |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
75 return _safe_repr(object, {}, None, 0)[2] |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
77 class PrettyPrinter: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
78 def __init__(self, indent=1, width=80, depth=None, stream=None): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
79 """Handle pretty printing operations onto a stream using a set of |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
80 configured parameters. |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
82 indent |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
83 Number of spaces to indent for each level of nesting. |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
84 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
85 width |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
86 Attempted maximum number of columns in the output. |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
87 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
88 depth |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
89 The maximum depth to print out nested structures. |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
91 stream |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
92 The desired output stream. If omitted (or false), the standard |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
93 output stream available at construction will be used. |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
94 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
95 """ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
96 indent = int(indent) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
97 width = int(width) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
98 assert indent >= 0 |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
99 assert depth is None or depth > 0, "depth must be > 0" |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
100 assert width |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
101 self.__depth = depth |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
102 self.__indent_per_level = indent |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
103 self.__width = width |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
104 if stream: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
105 self.__stream = stream |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
106 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
107 self.__stream = sys.stdout |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
109 def pprint(self, object): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
110 self.__stream.write(self.pformat(object) + "\n") |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
111 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
112 def pformat(self, object): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
113 sio = StringIO() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
114 self.__format(object, sio, 0, 0, {}, 0) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
115 return sio.getvalue() |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 |
|
5837
2e917f9372e0
Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents:
5337
diff
changeset
|
117 def isrecursive(self, object): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
118 self.__recursive = 0 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
119 self.__repr(object, {}, 0) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
120 return self.__recursive |
|
5837
2e917f9372e0
Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents:
5337
diff
changeset
|
121 |
|
2e917f9372e0
Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents:
5337
diff
changeset
|
122 def isreadable(self, object): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
123 self.__recursive = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
124 self.__readable = 1 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
125 self.__repr(object, {}, 0) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
126 return self.__readable and not self.__recursive |
|
5837
2e917f9372e0
Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents:
5337
diff
changeset
|
127 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
128 def __format(self, object, stream, indent, allowance, context, level): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
129 level = level + 1 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
130 objid = _id(object) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
131 if objid in context: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
132 stream.write(_recursion(object)) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
133 self.__recursive = 1 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
134 self.__readable = 0 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
135 return |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
136 rep = self.__repr(object, context, level - 1) |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
137 typ = _type(object) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
138 sepLines = _len(rep) > (self.__width - 1 - indent - allowance) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
139 write = stream.write |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
141 if sepLines: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
142 if typ is DictType: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
143 write('{') |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
144 if self.__indent_per_level > 1: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
145 write((self.__indent_per_level - 1) * ' ') |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
146 length = _len(object) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
147 if length: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
148 context[objid] = 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
149 indent = indent + self.__indent_per_level |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
150 items = object.items() |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
151 items.sort() |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
152 key, ent = items[0] |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
153 rep = self.__repr(key, context, level) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
154 write(rep) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
155 write(': ') |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
156 self.__format(ent, stream, indent + _len(rep) + 2, |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
157 allowance + 1, context, level) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
158 if length > 1: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
159 for key, ent in items[1:]: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
160 rep = self.__repr(key, context, level) |
|
21061
1802a9edb4ee
__format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents:
20967
diff
changeset
|
161 write(',\n%s%s: ' % (' '*indent, rep)) |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
162 self.__format(ent, stream, indent + _len(rep) + 2, |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
163 allowance + 1, context, level) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
164 indent = indent - self.__indent_per_level |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
165 del context[objid] |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
166 write('}') |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
167 return |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
169 if typ is ListType or typ is TupleType: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
170 if typ is ListType: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
171 write('[') |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
172 endchar = ']' |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
173 else: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
174 write('(') |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
175 endchar = ')' |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
176 if self.__indent_per_level > 1: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
177 write((self.__indent_per_level - 1) * ' ') |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
178 length = _len(object) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
179 if length: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
180 context[objid] = 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
181 indent = indent + self.__indent_per_level |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
182 self.__format(object[0], stream, indent, allowance + 1, |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
183 context, level) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
184 if length > 1: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
185 for ent in object[1:]: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
186 write(',\n' + ' '*indent) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
187 self.__format(ent, stream, indent, |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
188 allowance + 1, context, level) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
189 indent = indent - self.__indent_per_level |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
190 del context[objid] |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
191 if typ is TupleType and length == 1: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
192 write(',') |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
193 write(endchar) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
194 return |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
196 write(rep) |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
198 def __repr(self, object, context, level): |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
199 repr, readable, recursive = _safe_repr(object, context, |
|
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
200 self.__depth, level) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
201 if not readable: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
202 self.__readable = 0 |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
203 if recursive: |
|
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
204 self.__recursive = 1 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
205 return repr |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 |
|
17820
4bcf88e4abff
SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
16562
diff
changeset
|
207 # Return triple (repr_string, isreadable, isrecursive). |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
209 def _safe_repr(object, context, maxlevels, level): |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
210 typ = _type(object) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
211 if typ is StringType: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
212 if 'locale' not in _sys_modules: |
|
19697
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
213 return `object`, 1, 0 |
|
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
214 if "'" in object and '"' not in object: |
|
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
215 closure = '"' |
|
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
216 quotes = {'"': '\\"'} |
|
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
217 else: |
|
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
218 closure = "'" |
|
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
219 quotes = {"'": "\\'"} |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
220 qget = quotes.get |
|
19697
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
221 sio = StringIO() |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
222 write = sio.write |
|
19697
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
223 for char in object: |
|
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
224 if char.isalpha(): |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
225 write(char) |
|
19697
bc6bd35f5998
Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents:
17828
diff
changeset
|
226 else: |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
227 write(qget(char, `char`[1:-1])) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
228 return ("%s%s%s" % (closure, sio.getvalue(), closure)), 1, 0 |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17820
diff
changeset
|
229 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
230 if typ is DictType: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
231 if not object: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
232 return "{}", 1, 0 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
233 objid = _id(object) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
234 if maxlevels and level > maxlevels: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
235 return "{...}", 0, objid in context |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
236 if objid in context: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
237 return _recursion(object), 0, 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
238 context[objid] = 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
239 readable = 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
240 recursive = 0 |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17820
diff
changeset
|
241 components = [] |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
242 append = components.append |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
243 level += 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
244 saferepr = _safe_repr |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17820
diff
changeset
|
245 for k, v in object.iteritems(): |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
246 krepr, kreadable, krecur = saferepr(k, context, maxlevels, level) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
247 vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
248 append("%s: %s" % (krepr, vrepr)) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6312
diff
changeset
|
249 readable = readable and kreadable and vreadable |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
250 if krecur or vrecur: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
251 recursive = 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
252 del context[objid] |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
253 return "{%s}" % _commajoin(components), readable, recursive |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17820
diff
changeset
|
254 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
255 if typ is ListType or typ is TupleType: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
256 if typ is ListType: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
257 if not object: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
258 return "[]", 1, 0 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
259 format = "[%s]" |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
260 elif _len(object) == 1: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
261 format = "(%s,)" |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
262 else: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
263 if not object: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
264 return "()", 1, 0 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
265 format = "(%s)" |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
266 objid = _id(object) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
267 if maxlevels and level > maxlevels: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
268 return format % "...", 0, objid in context |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
269 if objid in context: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
270 return _recursion(object), 0, 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
271 context[objid] = 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
272 readable = 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
273 recursive = 0 |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17820
diff
changeset
|
274 components = [] |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
275 append = components.append |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
276 level += 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
277 for o in object: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
278 orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
279 append(orepr) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
280 if not oreadable: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
281 readable = 0 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
282 if orecur: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
283 recursive = 1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
284 del context[objid] |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
285 return format % _commajoin(components), readable, recursive |
|
20967
bb8716095a81
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20819
diff
changeset
|
286 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
287 rep = `object` |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
288 return rep, (rep and not rep.startswith('<')), 0 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
289 |
|
17828
26745053f840
pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents:
17820
diff
changeset
|
290 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
291 def _recursion(object): |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
292 return ("<Recursion on %s with id=%s>" |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
293 % (_type(object).__name__, _id(object))) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
294 |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
296 def _perfcheck(object=None): |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
297 import time |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
298 if object is None: |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
299 object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
300 p = PrettyPrinter() |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
301 t1 = time.time() |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
302 _safe_repr(object, {}, None, 0) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
303 t2 = time.time() |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
304 p.pformat(object) |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
305 t3 = time.time() |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
306 print "_safe_repr:", t2 - t1 |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
307 print "pformat:", t3 - t2 |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 |
|
20819
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
309 if __name__ == "__main__": |
|
261a70c0aa79
Brute-force performance hackery; buys back about 20% of the time for
Fred Drake <fdrake@acm.org>
parents:
20348
diff
changeset
|
310 _perfcheck() |
