This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Tweak pprint.PrettyPrinter.format for subclassing
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: fdrake, georg.brandl, kathyvs, markhirota, rhettinger
Priority: normal Keywords: patch

Created on 2005-12-05 19:21 by markhirota, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pprint.diff markhirota, 2005-12-05 19:21 pprint.py unified forward diff
pprint.diff2 markhirota, 2005-12-06 18:26 A new diff file
Messages (6)
msg49169 - (view) Author: Mark Hirota (markhirota) Date: 2005-12-05 19:21
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".
msg49170 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-12-05 19:42
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?
msg49171 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2005-12-05 20:48
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.
msg49172 - (view) Author: Mark Hirota (markhirota) Date: 2005-12-06 18:26
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.



msg57586 - (view) Author: Kathy Van Stone (kathyvs) Date: 2007-11-16 17:00
I might be able to give a more compelling example (aside from the fact
wanting it to fit the documentation which implies that one can subclass
the pretty printer).  I had a structure containing mostly lists,
dictionary and primitives that I wanted to display, but it also
contained UUIDs. In order to be able to see what the UUID referred to, I
extended the pretty printer to lookup up the name associated with the
UUID and included that. I have it working now by keeping the width
narrow.  The patch listed here (moving the line length check inside
lists and dictionaries) doesn't entirely work as my altered
representation of a UUID is different enough from the original that the
calculation of line length is inaccurate.
msg61350 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-20 19:35
This has now been fixed with #1351692.
History
Date User Action Args
2022-04-11 14:56:14adminsetgithub: 42662
2008-01-20 19:35:08georg.brandlsetstatus: open -> closed
assignee: fdrake -> georg.brandl
resolution: fixed
messages: + msg61350
nosy: + georg.brandl
2007-11-16 17:00:36kathyvssetnosy: + kathyvs
messages: + msg57586
2005-12-05 19:21:18markhirotacreate