Difference between revisions of "Python - pprint module"

From PeformIQ Upgrade
Jump to navigation Jump to search
(New page: =Overview= See [Python doco|http://docs.python.org/lib/module-pprint.html] for an overview of the module. =Examples= Here is an [example|http://vsbabu.org/mt/archives/2003/02/13/joy_of_...)
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Overview=
=Overview=


See [Python doco|http://docs.python.org/lib/module-pprint.html] for an overview of the module.
See [http://docs.python.org/lib/module-pprint.html Python doco] for an overview of the module.


=Examples=
=Examples=


Here is an [example|http://vsbabu.org/mt/archives/2003/02/13/joy_of_python_dir_help_and_pprint.html] of a how it can be used to assist in exploring python code.
Here is an [http://vsbabu.org/mt/archives/2003/02/13/joy_of_python_dir_help_and_pprint.html example] of a how it can be used to assist in exploring python code.
=Info
 
<pre>
import pprint
p = pprint.PrettyPrinter(4)
p.pprint(pprint.__dict__)
</pre>
 
=Info=


The dictionary contains info about all the methods, variables and values contained within the module
The dictionary contains info about all the methods, variables and values contained within the module
Line 217: Line 224:
</pre>
</pre>


[[Categories:Python]]
[[Category:Python]]

Latest revision as of 13:54, 18 November 2008

Overview

See Python doco for an overview of the module.

Examples

Here is an example of a how it can be used to assist in exploring python code.

import pprint
p = pprint.PrettyPrinter(4)
p.pprint(pprint.__dict__)

Info

The dictionary contains info about all the methods, variables and values contained within the module

pprint Dictionary

>>> import pprint
>>> p = pprint
>>> p.pprint(p.__dict__)

{'PrettyPrinter': <class pprint.PrettyPrinter at 0x7fcde7dc>,
 '_StringIO': <built-in function StringIO>,
 '__all__': ['pprint',
             'pformat',
             'isreadable',
             'isrecursive',
             'saferepr',
             'PrettyPrinter'],
 '__builtins__': {'ArithmeticError': <type 'exceptions.ArithmeticError'>,
                  'AssertionError': <type 'exceptions.AssertionError'>,
                  'AttributeError': <type 'exceptions.AttributeError'>,
                  'BaseException': <type 'exceptions.BaseException'>,
                  'DeprecationWarning': <type 'exceptions.DeprecationWarning'>,
                  'EOFError': <type 'exceptions.EOFError'>,
                  'Ellipsis': Ellipsis,
                  'EnvironmentError': <type 'exceptions.EnvironmentError'>,
                  'Exception': <type 'exceptions.Exception'>,
                  'False': False,
                  'FloatingPointError': <type 'exceptions.FloatingPointError'>,
                  'FutureWarning': <type 'exceptions.FutureWarning'>,
                  'GeneratorExit': <type 'exceptions.GeneratorExit'>,
                  'IOError': <type 'exceptions.IOError'>,
                  'ImportError': <type 'exceptions.ImportError'>,
                  'ImportWarning': <type 'exceptions.ImportWarning'>,
                  'IndentationError': <type 'exceptions.IndentationError'>,
                  'IndexError': <type 'exceptions.IndexError'>,
                  'KeyError': <type 'exceptions.KeyError'>,
                  'KeyboardInterrupt': <type 'exceptions.KeyboardInterrupt'>,
                  'LookupError': <type 'exceptions.LookupError'>,
                  'MemoryError': <type 'exceptions.MemoryError'>,
                  'NameError': <type 'exceptions.NameError'>,
                  'None': None,
                  'NotImplemented': NotImplemented,
                  'NotImplementedError': <type 'exceptions.NotImplementedError'>,
                  'OSError': <type 'exceptions.OSError'>,
                  'OverflowError': <type 'exceptions.OverflowError'>,
                  'PendingDeprecationWarning': <type 'exceptions.PendingDeprecationWarning'>,
                  'ReferenceError': <type 'exceptions.ReferenceError'>,
                  'RuntimeError': <type 'exceptions.RuntimeError'>,
                  'RuntimeWarning': <type 'exceptions.RuntimeWarning'>,
                  'StandardError': <type 'exceptions.StandardError'>,
                  'StopIteration': <type 'exceptions.StopIteration'>,
                  'SyntaxError': <type 'exceptions.SyntaxError'>,
                  'SyntaxWarning': <type 'exceptions.SyntaxWarning'>,
                  'SystemError': <type 'exceptions.SystemError'>,
                  'SystemExit': <type 'exceptions.SystemExit'>,
                  'TabError': <type 'exceptions.TabError'>,
                  'True': True,
                  'TypeError': <type 'exceptions.TypeError'>,
                  'UnboundLocalError': <type 'exceptions.UnboundLocalError'>,
                  'UnicodeDecodeError': <type 'exceptions.UnicodeDecodeError'>,
                  'UnicodeEncodeError': <type 'exceptions.UnicodeEncodeError'>,
                  'UnicodeError': <type 'exceptions.UnicodeError'>,
                  'UnicodeTranslateError': <type 'exceptions.UnicodeTranslateError'>,
                  'UnicodeWarning': <type 'exceptions.UnicodeWarning'>,
                  'UserWarning': <type 'exceptions.UserWarning'>,
                  'ValueError': <type 'exceptions.ValueError'>,
                  'Warning': <type 'exceptions.Warning'>,
                  'ZeroDivisionError': <type 'exceptions.ZeroDivisionError'>,
                  '__debug__': True,
                  '__doc__': "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.",
                  '__import__': <built-in function __import__>,
                  '__name__': '__builtin__',
                  'abs': <built-in function abs>,
                  'all': <built-in function all>,
                  'any': <built-in function any>,
                  'apply': <built-in function apply>,
                  'basestring': <type 'basestring'>,
                  'bool': <type 'bool'>,
                  'buffer': <type 'buffer'>,
                  'callable': <built-in function callable>,
                  'chr': <built-in function chr>,
                  'classmethod': <type 'classmethod'>,
                  'cmp': <built-in function cmp>,
                  'coerce': <built-in function coerce>,
                  'compile': <built-in function compile>,
                  'complex': <type 'complex'>,
                  'copyright': """Copyright (c) 2001-2007 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.""",
                  'credits':     Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information.,
                  'delattr': <built-in function delattr>,
                  'dict': <type 'dict'>,
                  'dir': <built-in function dir>,
                  'divmod': <built-in function divmod>,
                  'enumerate': <type 'enumerate'>,
                  'eval': <built-in function eval>,
                  'execfile': <built-in function execfile>,
                  'exit': Use exit() or Ctrl-D (i.e. EOF) to exit,
                  'file': <type 'file'>,
                  'filter': <built-in function filter>,
                  'float': <type 'float'>,
                  'frozenset': <type 'frozenset'>,
                  'getattr': <built-in function getattr>,
                  'globals': <built-in function globals>,
                  'hasattr': <built-in function hasattr>,
                  'hash': <built-in function hash>,
                  'help': Type help() for interactive help, or help(object) for help about object.,
                  'hex': <built-in function hex>,
                  'id': <built-in function id>,
                  'input': <built-in function input>,
                  'int': <type 'int'>,
                  'intern': <built-in function intern>,
                  'isinstance': <built-in function isinstance>,
                  'issubclass': <built-in function issubclass>,
                  'iter': <built-in function iter>,
                  'len': <built-in function len>,
                  'license': Type license() to see the full license text,
                  'list': <type 'list'>,
                  'locals': <built-in function locals>,
                  'long': <type 'long'>,
                  'map': <built-in function map>,
                  'max': <built-in function max>,
                  'min': <built-in function min>,
                  'object': <type 'object'>,
                  'oct': <built-in function oct>,
                  'open': <built-in function open>,
                  'ord': <built-in function ord>,
                  'pow': <built-in function pow>,
                  'property': <type 'property'>,
                  'quit': Use quit() or Ctrl-D (i.e. EOF) to exit,
                  'range': <built-in function range>,
                  'raw_input': <built-in function raw_input>,
                  'reduce': <built-in function reduce>,
                  'reload': <built-in function reload>,
                  'repr': <built-in function repr>,
                  'reversed': <type 'reversed'>,
                  'round': <built-in function round>,
                  'set': <type 'set'>,
                  'setattr': <built-in function setattr>,
                  'slice': <type 'slice'>,
                  'sorted': <built-in function sorted>,
                  'staticmethod': <type 'staticmethod'>,
                  'str': <type 'str'>,
                  'sum': <built-in function sum>,
                  'super': <type 'super'>,
                  'tuple': <type 'tuple'>,
                  'type': <type 'type'>,
                  'unichr': <built-in function unichr>,
                  'unicode': <type 'unicode'>,
                  'vars': <built-in function vars>,
                  'xrange': <type 'xrange'>,
                  'zip': <built-in function zip>},
 '__doc__': "Support to pretty-print lists, tuples, & dictionaries recursively.\n\nVery simple, but useful, especially in debugging data structures.\n\nClasses\n-------\n\nPrettyPrinter()\n    Handle pretty-printing operations onto a stream using a configured\n    set of formatting parameters.\n\nFunctions\n---------\n\npformat()\n    Format a Python object into a pretty-printed representation.\n\npprint()\n    Pretty-print a Python object to a stream [default is sys.stdout].\n\nsaferepr()\n    Generate a 'standard' repr()-like value, but protect against recursive\n    data structures.\n\n",
 '__file__': '/usr/lib/python2.5/pprint.pyc',
 '__name__': 'pprint',
 '_commajoin': <built-in method join of str object at 0x7fce1360>,
 '_id': <built-in function id>,
 '_len': <built-in function len>,
 '_perfcheck': <function _perfcheck at 0x7fce710c>,
 '_recursion': <function _recursion at 0x7fce70d4>,
 '_safe_repr': <function _safe_repr at 0x7fcdde9c>,
 '_sys': <module 'sys' (built-in)>,
 '_type': <type 'type'>,
 'isreadable': <function isreadable at 0x7fcdde2c>,
 'isrecursive': <function isrecursive at 0x7fcdde64>,
 'pformat': <function pformat at 0x7fcdddbc>,
 'pprint': <function pprint at 0x7fcddd84>,
 'saferepr': <function saferepr at 0x7fcdddf4>}
>>>

__str__

>>> print p.__doc__
Support to pretty-print lists, tuples, & dictionaries recursively.

Very simple, but useful, especially in debugging data structures.

Classes
-------

PrettyPrinter()
    Handle pretty-printing operations onto a stream using a configured
    set of formatting parameters.

Functions
---------

pformat()
    Format a Python object into a pretty-printed representation.

pprint()
    Pretty-print a Python object to a stream [default is sys.stdout].

saferepr()
    Generate a 'standard' repr()-like value, but protect against recursive
    data structures.