annotate Lib/pprint.py @ 20010:d03c19db3094 legacy-trunk

Document new file() constructor, with the body of open()'s text, plus a "new in 2.2" blurb at the end. Replace open()'s text by pointing back to file().
author Tim Peters <tim.peters@gmail.com>
date Thu, 20 Sep 2001 19:55:29 +0000
parents bc6bd35f5998
children 425c6c558fac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
2 # fdrake@cnri.reston.va.us, 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
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
48 def pprint(object, stream=None):
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
49 """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
50 printer = PrettyPrinter(stream=stream)
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
51 printer.pprint(object)
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
52
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
53 def pformat(object):
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
54 """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
55 return PrettyPrinter().pformat(object)
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
56
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
57 def saferepr(object):
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
58 """Version of repr() which can handle recursive data structures."""
5837
2e917f9372e0 Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents: 5337
diff changeset
59 return _safe_repr(object, {})[0]
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
61 def isreadable(object):
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
62 """Determine if saferepr(object) is readable by eval()."""
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
63 return _safe_repr(object, {})[1]
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
64
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
65 def isrecursive(object):
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
66 """Determine if object requires a recursive representation."""
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
67 return _safe_repr(object, {})[2]
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
69 class PrettyPrinter:
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
70 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
71 """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
72 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
73
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
74 indent
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
75 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
76
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
77 width
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
78 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
79
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
80 depth
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
81 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
82
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
83 stream
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
84 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
85 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
86
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
87 """
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
88 indent = int(indent)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
89 width = int(width)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
90 assert indent >= 0
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
91 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
92 assert width
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
93 self.__depth = depth
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
94 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
95 self.__width = width
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
96 if stream:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
97 self.__stream = stream
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
98 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
99 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
100
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
101 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
102 self.__stream.write(self.pformat(object) + "\n")
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
103
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
104 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
105 sio = StringIO()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
106 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
107 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
108
5837
2e917f9372e0 Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents: 5337
diff changeset
109 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
110 self.__recursive = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
111 self.pformat(object)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
112 return self.__recursive
5837
2e917f9372e0 Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents: 5337
diff changeset
113
2e917f9372e0 Slight mods to make the module conform to the documentation.
Fred Drake <fdrake@acm.org>
parents: 5337
diff changeset
114 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
115 self.__recursive = 0
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
116 self.__readable = 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
117 self.pformat(object)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
118 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
119
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
120 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
121 level = level + 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
122 if context.has_key(id(object)):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
123 object = _Recursion(object)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
124 self.__recursive = 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
125 rep = self.__repr(object, context, level - 1)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
126 objid = id(object)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
127 context[objid] = 1
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
128 typ = type(object)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
129 sepLines = len(rep) > (self.__width - 1 - indent - allowance)
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
131 if sepLines and typ in (ListType, TupleType):
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
132 # Pretty-print the sequence.
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
133 stream.write((typ is ListType) and '[' or '(')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
134 if self.__indent_per_level > 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
135 stream.write((self.__indent_per_level - 1) * ' ')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
136 length = len(object)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
137 if length:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
138 indent = indent + self.__indent_per_level
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
139 self.__format(object[0], stream, indent, allowance + 1,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
140 context, level)
11620
c87b861c6ec6 Don't call len() if the value is already cached! Caught by Gerrit
Fred Drake <fdrake@acm.org>
parents: 11253
diff changeset
141 if length > 1:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
142 for ent in object[1:]:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
143 stream.write(',\n' + ' '*indent)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
144 self.__format(ent, stream, indent,
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
145 allowance + 1, context, level)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
146 indent = indent - self.__indent_per_level
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
147 if typ is TupleType and length == 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
148 stream.write(',')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
149 stream.write(((typ is ListType) and ']') or ')')
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
151 elif sepLines and typ is DictType:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
152 stream.write('{')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
153 if self.__indent_per_level > 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
154 stream.write((self.__indent_per_level - 1) * ' ')
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
155 length = len(object)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
156 if length:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
157 indent = indent + self.__indent_per_level
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
158 items = object.items()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
159 items.sort()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
160 key, ent = items[0]
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
161 rep = self.__repr(key, context, level) + ': '
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
162 stream.write(rep)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
163 self.__format(ent, stream, indent + len(rep),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
164 allowance + 1, context, level)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
165 if len(items) > 1:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
166 for key, ent in items[1:]:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
167 rep = self.__repr(key, context, level) + ': '
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
168 stream.write(',\n' + ' '*indent + rep)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
169 self.__format(ent, stream, indent + len(rep),
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
170 allowance + 1, context, level)
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
171 indent = indent - self.__indent_per_level
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
172 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
173
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
174 else:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
175 stream.write(rep)
11253
c6bdf032b705 Fix indent error in __format(): del context[objid] at the end should
Guido van Rossum <guido@python.org>
parents: 10212
diff changeset
176
c6bdf032b705 Fix indent error in __format(): del context[objid] at the end should
Guido van Rossum <guido@python.org>
parents: 10212
diff changeset
177 del context[objid]
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
178
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
179 def __repr(self, object, context, level):
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
180 repr, readable, recursive = _safe_repr(object, context,
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
181 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
182 if not readable:
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
183 self.__readable = 0
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
184 if recursive:
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
185 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
186 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
187
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
188 # 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
189
19697
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
190 _have_module = sys.modules.has_key
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
191
5336
746f687acfc1 _safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents: 5335
diff changeset
192 def _safe_repr(object, context, maxlevels=None, level=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
193 level += 1
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
194 typ = type(object)
19697
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
195 if not (typ in (DictType, ListType, TupleType, StringType) and object):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
196 rep = `object`
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
197 return rep, (rep and (rep[0] != '<')), 0
19697
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
198 elif typ is StringType:
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
199 if not _have_module('locale'):
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
200 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
201 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
202 closure = '"'
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
203 quotes = {'"': '\\"'}
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
204 else:
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
205 closure = "'"
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
206 quotes = {"'": "\\'"}
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
207 sio = StringIO()
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
208 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
209 if char.isalpha():
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
210 sio.write(char)
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
211 else:
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
212 sio.write(quotes.get(char, `char`[1:-1]))
bc6bd35f5998 Make pprint more locale-friendly; patch contributed by Denis S. Otkidach.
Fred Drake <fdrake@acm.org>
parents: 17828
diff changeset
213 return 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
214
5336
746f687acfc1 _safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents: 5335
diff changeset
215 if context.has_key(id(object)):
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
216 return `_Recursion(object)`, 0, 1
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
217 objid = id(object)
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
218 context[objid] = 1
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
219
10212
e3397ae88129 _safe_repr(): Simplify the condition tests in the first possible
Fred Drake <fdrake@acm.org>
parents: 7755
diff changeset
220 readable = 1
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
221 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
222 startchar, endchar = {ListType: "[]",
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
223 TupleType: "()",
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
224 DictType: "{}"}[typ]
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
225 if maxlevels and level > maxlevels:
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
226 with_commas = "..."
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
227 readable = 0
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
228
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
229 elif typ is DictType:
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
230 components = []
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
231 for k, v in object.iteritems():
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
232 krepr, kreadable, krecur = _safe_repr(k, context, maxlevels,
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
233 level)
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
234 vrepr, vreadable, vrecur = _safe_repr(v, context, maxlevels,
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
235 level)
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
236 components.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
237 readable = readable and kreadable and vreadable
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
238 recursive = recursive or krecur or vrecur
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
239 with_commas = ", ".join(components)
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
240
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
241 else: # list or tuple
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
242 assert typ in (ListType, TupleType)
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
243 components = []
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
244 for element in object:
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
245 subrepr, subreadable, subrecur = _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
246 element, context, maxlevels, level)
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
247 components.append(subrepr)
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
248 readable = readable and subreadable
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
249 recursive = recursive or subrecur
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
250 if len(components) == 1 and typ is TupleType:
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
251 components[0] += ","
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
252 with_commas = ", ".join(components)
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
253
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17820
diff changeset
254 s = "%s%s%s" % (startchar, with_commas, endchar)
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
255 del context[objid]
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents: 16562
diff changeset
256 return s, readable and not recursive, recursive
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
258 class _Recursion:
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
259 # represent a recursive relationship; really only used for the __repr__()
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
260 # method...
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
261 def __init__(self, object):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
262 self.__repr = "<Recursion on %s with id=%s>" \
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
263 % (type(object).__name__, id(object))
5330
5a22f9b38aac Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
5335
ee9275b0f5c8 Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents: 5330
diff changeset
265 def __repr__(self):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6312
diff changeset
266 return self.__repr