-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
general pprint rewrite #51683
Comments
It would be nice if pprint could format namedtuples wrapping lines as it Looking at the code, this does not look like an easy task. Completely |
I agree with you that pprint needs to be rewritten to make it more I do not see a straight-forward way of handling your feature request. First, namedtuple() is a factory function and is not itself a class, so Second, collections.namedtuple() is intentionally designed to let the At best, I can imagine that pprint() grows the ability to print a For example: >>> class Point(namedtuple('Point', 'x y z')):
... 'Point with a multi-line repr'
... def __repr__(self):
... return 'Point(\n x=%r,\n y=%r,\n z=%r\n
)' % self
>>> Point(3,4,5)
Point(
x=3,
y=4,
z=5
)
>>> pprint([Point(3,4,5), Point(6,7,8)])
[Point(
x=3,
y=4,
z=5
),
Point(
x=6,
y=7,
z=8
)
] Alternatively, the pprint module could introduce a new magic method to class MyList(list):
... def __multirepr__(self):
... 'Return a list of strings to pprint'
... return Multi(head = 'Mylist([',
... body = [str(x).upper() for x in self],
... tail = ']) >>> pprint(MyList(['now', 'is', 'the', 'time', 'for']), width=15)
MyList(['NOW',
'IS',
'THE',
'TIME',
'FOR',
]) In summary, there are several ways to approach this problem but they are |
You could make all namedtuples inherit from a common base class, e.g. |
We need a more generic solution that allows multi-line reprs for a >>> pprint(s, width=15)
[OrderedDict([('x', 30000000000), ('y', 4000000000), ('z', 5000000000)]),
OrderedDict([('x', 6000000000), ('y', 70000000), ('z', 8000000000)])] What we want is to have it print like regular dictionaries do: >>> pprint([dict(p) for p in s], width=15)
[{'x': 30000000000,
'y': 4000000000,
'z': 5000000000},
{'x': 6000000000,
'y': 70000000,
'z': 8000000000}] It would also be nice if pprint could accept arguments telling it how to >>> pprint(s, width=15, format={int: '15,'})
[{'x': ' 30,000,000,000',
'y': ' 4,000,000,000',
'z': ' 5,000,000,000'},
{'x': ' 6,000,000,000',
'y': ' 70,000,000',
'z': ' 8,000,000,000'}] |
Sorry for the noise... |
Will ponder this a bit more but will likely close this specific request (leaving open the possibility of a more general rewrite of pprint). |
Could something like a generic __pprint__ hook achieve this? |
I would like to work on that. Expect a patch soon. Georg, Fred, I've added you to nosy because you're the ones watching over me currently. Bare with me :) |
Deferring the general rewrite until 3.3. |
Attaching a rough concept of how to make the existing pprint module extendible without doing a total rewrite. The actual handler is currently bogus (no thought out), so focus on the @guard decorator and the technique for scanning for handlers. |
Link to Armin's work on a pprint improvement based on a Ruby pprint tool: https://github.com/mitsuhiko/prettyprint |
For the record, my class-based approach from 2010 still available here: |
With PEP-443 added for Python 3.4, I believe Łukasz intended to pursue a new pprint implementation based on functools.singledispatch. That has obviously missed feature freeze for Python 3.4, but it's still a reasonable idea to pursue for 3.5. In addition to OrderedDict (mentioned above) and defaultdict (which was mentioned in bpo-5131), an updated pprint would also allow us to add support for the new dict view types, collections.deque, etc. Ideally, we'd also have a standard lazy import mechanism in 3.5, so these definitions could go in the collections module, but only installed if pprint was also imported. Otherwise, having pprint depend on collections would likely be preferable to having the dependency run the other way. |
That sounds more like an on-import hook than a lazy import mechanism, no? |
Oops... no, it's not easy. |
Ok, so why did Roundup add the easy keyword and doesn't want to remove it? |
Related issue bpo-21542: pprint support for multiline collections.Counter |
I've made a Pretty Printer and I'd like to know if anybody thinks it could of some help here. (I do) It's easily extensible and customizable to support any type in any way. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: