2424MAX_WIDTH
2525 Maximum width of the display. This is only used if the
2626 representation *can* be kept less than MAX_WIDTH characters wide.
27- May be set by the user before calling pprint().
27+ May be set by the user before calling pprint() if needed .
2828
29- TAB_WIDTH
30- The width represented by a single tab. This value is typically 8,
31- but 4 is the default under MacOS. Can be changed by the user if
32- desired, but is probably not a good idea.
3329"""
3430
3531INDENT_PER_LEVEL = 1
3632
3733MAX_WIDTH = 80
3834
39- import os
40- TAB_WIDTH = (os .name == 'mac' and 4 ) or 8
41- del os
42-
4335from types import DictType , ListType , TupleType
4436
4537
46- def _indentation (cols ):
47- """Create tabbed indentation string.
38+ def pformat (seq ):
39+ """Format a Python object into a pretty-printed representation.
40+
41+ The representation is returned with no trailing newline.
4842
49- cols
50- Width of the indentation, in columns.
5143 """
52- return ((cols / TAB_WIDTH ) * '\t ' ) + ((cols % TAB_WIDTH ) * ' ' )
44+ import StringIO
45+ sio = StringIO .StringIO ()
46+ pprint (seq , stream = sio )
47+ str = sio .getvalue ()
48+ if str and str [- 1 ] == '\n ' :
49+ str = str [:- 1 ]
50+ return str
5351
5452
55- def pprint (seq , stream = None , indent = 0 , allowance = 0 ):
53+ def pprint (seq , stream = None , indent = 0 , allowance = 0 ):
5654 """Pretty-print a list, tuple, or dictionary.
5755
5856 seq
@@ -74,6 +72,7 @@ def pprint(seq, stream = None, indent = 0, allowance = 0):
7472 without error, given readable representations of all elements are
7573 available via `repr()'. Output is restricted to `MAX_WIDTH'
7674 columns where possible.
75+
7776 """
7877 if stream is None :
7978 import sys
@@ -94,7 +93,7 @@ def pprint(seq, stream = None, indent = 0, allowance = 0):
9493
9594 if len (seq ) > 1 :
9695 for ent in seq [1 :]:
97- stream .write (',\n ' + _indentation ( indent ) )
96+ stream .write (',\n ' + ' ' * indent )
9897 pprint (ent , stream , indent , allowance + 1 )
9998
10099 indent = indent - INDENT_PER_LEVEL
@@ -117,7 +116,7 @@ def pprint(seq, stream = None, indent = 0, allowance = 0):
117116 if len (items ) > 1 :
118117 for key , ent in items [1 :]:
119118 rep = `key` + ': '
120- stream .write (',\n ' + _indentation ( indent ) + rep )
119+ stream .write (',\n ' + ' ' * indent + rep )
121120 pprint (ent , stream , indent + len (rep ), allowance + 1 )
122121
123122 indent = indent - INDENT_PER_LEVEL
0 commit comments