Commit c8de4585 authored by Walter Dörwald's avatar Walter Dörwald

Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()

and pass them along to the PrettyPrinter constructor.
parent 7a7ede54
...@@ -77,17 +77,23 @@ within the constrained width, a best effort will be made. ...@@ -77,17 +77,23 @@ within the constrained width, a best effort will be made.
The \class{PrettyPrinter} class supports several derivative functions: The \class{PrettyPrinter} class supports several derivative functions:
\begin{funcdesc}{pformat}{object} \begin{funcdesc}{pformat}{object\optional{, indent\optional{,
Return the formatted representation of \var{object} as a string. The width\optional{, depth}}}}
default parameters for formatting are used. Return the formatted representation of \var{object} as a string. \var{indent},
\var{width} and \var{depth} will be passed to the \class{PrettyPrinter}
constructor as formatting parameters.
\versionchanged[The parameters \var{indent}, \var{width} and \var{depth}
were added]{2.4}
\end{funcdesc} \end{funcdesc}
\begin{funcdesc}{pprint}{object\optional{, stream}} \begin{funcdesc}{pprint}{object\optional{, stream\optional{,
indent\optional{, width\optional{, depth}}}}}
Prints the formatted representation of \var{object} on \var{stream}, Prints the formatted representation of \var{object} on \var{stream},
followed by a newline. If \var{stream} is omitted, \code{sys.stdout} followed by a newline. If \var{stream} is omitted, \code{sys.stdout}
is used. This may be used in the interactive interpreter instead of a is used. This may be used in the interactive interpreter instead of a
\keyword{print} statement for inspecting values. The default \keyword{print} statement for inspecting values. \var{indent},
parameters for formatting are used. \var{width} and \var{depth} will be passed to the \class{PrettyPrinter}
constructor as formatting parameters.
\begin{verbatim} \begin{verbatim}
>>> stuff = sys.path[:] >>> stuff = sys.path[:]
...@@ -101,6 +107,8 @@ parameters for formatting are used. ...@@ -101,6 +107,8 @@ parameters for formatting are used.
'/usr/local/lib/python1.5/sharedmodules', '/usr/local/lib/python1.5/sharedmodules',
'/usr/local/lib/python1.5/tkinter'] '/usr/local/lib/python1.5/tkinter']
\end{verbatim} \end{verbatim}
\versionchanged[The parameters \var{indent}, \var{width} and \var{depth}
were added]{2.4}
\end{funcdesc} \end{funcdesc}
\begin{funcdesc}{isreadable}{object} \begin{funcdesc}{isreadable}{object}
......
...@@ -48,14 +48,15 @@ _len = len ...@@ -48,14 +48,15 @@ _len = len
_type = type _type = type
def pprint(object, stream=None): def pprint(object, stream=None, indent=1, width=80, depth=None):
"""Pretty-print a Python object to a stream [default is sys.sydout].""" """Pretty-print a Python object to a stream [default is sys.sydout]."""
printer = PrettyPrinter(stream=stream) printer = PrettyPrinter(
stream=stream, indent=indent, width=width, depth=depth)
printer.pprint(object) printer.pprint(object)
def pformat(object): def pformat(object, indent=1, width=80, depth=None):
"""Format a Python object into a pretty-printed representation.""" """Format a Python object into a pretty-printed representation."""
return PrettyPrinter().pformat(object) return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object)
def saferepr(object): def saferepr(object):
"""Version of repr() which can handle recursive data structures.""" """Version of repr() which can handle recursive data structures."""
......
...@@ -154,6 +154,12 @@ class QueryTestCase(unittest.TestCase): ...@@ -154,6 +154,12 @@ class QueryTestCase(unittest.TestCase):
for type in [tuple, tuple2]: for type in [tuple, tuple2]:
self.assertEqual(pprint.pformat(type(o)), exp) self.assertEqual(pprint.pformat(type(o)), exp)
# indent parameter
o = range(100)
exp = '[ %s]' % ',\n '.join(map(str, o))
for type in [list, list2]:
self.assertEqual(pprint.pformat(type(o), indent=4), exp)
def test_subclassing(self): def test_subclassing(self):
o = {'names with spaces': 'should be presented using repr()', o = {'names with spaces': 'should be presented using repr()',
'others.should.not.be': 'like.this'} 'others.should.not.be': 'like.this'}
......
...@@ -170,6 +170,9 @@ Extension modules ...@@ -170,6 +170,9 @@ Extension modules
Library Library
------- -------
- pprint.pprint() and pprint.pformat() now have additional parameters
indent, width and depth.
- Patch #750542: pprint now will pretty print subclasses of list, tuple - Patch #750542: pprint now will pretty print subclasses of list, tuple
and dict too, as long as they don't overwrite __repr__(). and dict too, as long as they don't overwrite __repr__().
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment