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

SourceForge.net noreply at sourceforge.net
Tue Dec 6 19:26:52 CET 2005


Patches item #1373762, was opened at 2005-12-05 11:21
Message generated for change (Comment added) made by markhirota
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.5
Status: Open
Resolution: None
Priority: 5
Submitted By: Mark Hirota (markhirota)
Assigned to: Fred L. Drake, Jr. (fdrake)
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".

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

>Comment By: Mark Hirota (markhirota)
Date: 2005-12-06 10:26

Message:
Logged In: YES 
user_id=1375527

I agree that while the example use case may not be 
interesting to you, it certainly has practical use when 
dealing with hexidecimal values all day long. Also, the 
change should benefit others looking to get more out of 
the pprint module.

I've attached a 2nd diff file that hopefully should be 
more useful.





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

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2005-12-05 12:48

Message:
Logged In: YES 
user_id=3066

Heh, reading the diff alone doesn't help me; there's no
helpful context.

While the example use case doesn't seem interesting, I
consider non-support of sub-class overriding the format()
method to be a bug.

I'll try and look at the patch more seriously soon, but I've
not managed to eek out many tuits lately, and this week is
as massively overbooked as any these days.

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

Comment By: Raymond Hettinger (rhettinger)
Date: 2005-12-05 11:42

Message:
Logged In: YES 
user_id=80475

I don't find the use case to be even slightly motivating. 
Fred, what do you think of the patch?

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

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