Closed 3503ba5b-914c-442b-8ec9-669b46ef5c64 closed 16 years ago
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".
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?
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.
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.
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.
This has now been fixed with bpo-1351692.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = 'https://github.com/birkenfeld' closed_at =
created_at =
labels = ['library']
title = 'Tweak pprint.PrettyPrinter.format for subclassing'
updated_at =
user = 'https://bugs.python.org/markhirota'
```
bugs.python.org fields:
```python
activity =
actor = 'georg.brandl'
assignee = 'georg.brandl'
closed = True
closed_date =
closer = 'georg.brandl'
components = ['Library (Lib)']
creation =
creator = 'markhirota'
dependencies = []
files = ['6907', '6908']
hgrepos = []
issue_num = 1373762
keywords = ['patch']
message_count = 6.0
messages = ['49169', '49170', '49171', '49172', '57586', '61350']
nosy_count = 5.0
nosy_names = ['fdrake', 'georg.brandl', 'rhettinger', 'markhirota', 'kathyvs']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1373762'
versions = ['Python 2.5']
```