classification
Title: add !p to pprint.pformat() in str.format() an f-strings
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: later
Dependencies: Superseder:
Assigned To: Nosy List: Charles Machalow, eric.smith, rhettinger, xtreak
Priority: normal Keywords:

Created on 2020-07-16 04:17 by Charles Machalow, last changed 2020-07-17 02:22 by eric.smith. This issue is now closed.

Messages (8)
msg373738 - (view) Author: Charles Machalow (Charles Machalow) Date: 2020-07-16 04:17
Right now in str.format(), we have !s, !r, and !a to allow us to call str(), repr(), and ascii() respectively on the given expression.

I'm proposing that we add a !p conversion to have pprint.pformat() be called to convert the given expression to a 'pretty' string.

Calling
```
print(f"My dict: {d!p}")
```

is a lot more concise than:

```
import pprint
print(f"My dict: {pprint.pformat(d)}")
```

We may even be able to have a static attribute stored to change the various default kwargs of pprint.pformat().
msg373739 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-07-16 04:23
This needs discussion on python-ideas/ideas category on discourse similar to f-string debug notation.
msg373741 - (view) Author: Charles Machalow (Charles Machalow) Date: 2020-07-16 04:35
Fair enough. Didn't really know that list existed. Sent this there. Awaiting moderator approval. Thanks.
msg373786 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-07-17 00:56
If the python-ideas discussion is fruitful, go ahead and re-open this tracker item.

Personally, I don't see how this would work.  The pretty printing routines rely on knowing their current level of indentation.  Also, much of the "prettiness" comes from making the output span multiple lines, so this wouldn't fit well in the middle of an f-string.  Lastly, the pprint module leaves a lot to be desired and likely needs a rewrite from scratch, so it wouldn't be wise to tie it directly to a core language feature.
msg373787 - (view) Author: Charles Machalow (Charles Machalow) Date: 2020-07-17 01:17
One of the key things for ppformat here is to format long spanning dicts/lists to multiple lines, that look easy to read in a log. I feel as though that feature/usefulness outweigh potential indentation weirdness.

A lot of the usage would probably be towards the end of an fstring.
msg373788 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-07-17 01:50
I agree with Raymond that it's unlikely that this will work, as a practical matter. In addition to the other problems mentioned, there's the issue of the many parameters to control pprint.

And I agree with pprint, or a replacement, needing a redesign. I think it should use singledispatch.
msg373792 - (view) Author: Charles Machalow (Charles Machalow) Date: 2020-07-17 02:15
In terms of multiple parameters, I propose adding a method to control the defaults used by !p.

Though the defaults would work more than well enough for basic log and print usage.
msg373793 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-07-17 02:22
I suggest you discuss this on python-ideas, since we'll need to reach consensus there, first.
History
Date User Action Args
2020-07-17 02:22:56eric.smithsetmessages: + msg373793
components: + Interpreter Core, - Library (Lib), IO
2020-07-17 02:15:45Charles Machalowsetmessages: + msg373792
2020-07-17 01:50:48eric.smithsetmessages: + msg373788
2020-07-17 01:17:19Charles Machalowsetmessages: + msg373787
2020-07-17 00:56:11rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg373786

resolution: later
stage: resolved
2020-07-16 04:35:44Charles Machalowsetmessages: + msg373741
2020-07-16 04:23:44xtreaksetnosy: + eric.smith, xtreak
messages: + msg373739
2020-07-16 04:17:41Charles Machalowcreate