[Patches] [ python-Patches-1373762 ] Tweak pprint.PrettyPrinter.format for subclassing

SourceForge.net noreply at sourceforge.net
Mon Dec 5 20:21:18 CET 2005


Patches item #1373762, was opened at 2005-12-05 11:21
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1373762&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Library (Lib)
Group: Python 2.4
Status: Open
Resolution: None
Priority: 5
Submitted By: Mark Hirota (markhirota)
Assigned to: Nobody/Anonymous (nobody)
Summary: Tweak pprint.PrettyPrinter.format for subclassing

Initial Comment:
The current format() method doesn't recursively apply 
an overridden format() method when the width of the 
object is too short.

This patch is designed to remove that limitation and 
allow a pprint.PrettyPrinter sublcass that could, for 
example, print all ints and longs in hex:

class MyPrettyPrinter(pprint.PrettyPrinter):
    def format(self, object, context, maxlevels, 
level):
        if isinstance(object, int):
            return hex(object), True, False
        else:
            return pprintmod.PrettyPrinter.format(
                self, object, context, maxlevels, 
level)


>>> mpp = MyPrettyPrinter()
>>> mpp.pprint(range(10))
[0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9]
>>> mpp.pprint(range(0x10000000,0x10000010))
[0x10000000,
 0x10000001,
 0x10000002,
 0x10000003,
 0x10000004,
 0x10000005,
 0x10000006,
 0x10000007,
 0x10000008,
 0x10000009,
 0x1000000a,
 0x1000000b,
 0x1000000c,
 0x1000000d,
 0x1000000e,
 0x1000000f]


The attached file contains "svn diff --diff-cmd diff -
x -b Lib/pprint.py".

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1373762&group_id=5470


More information about the Patches mailing list