Skip to content

pprint: add simple pprint(), pp(), and pformat() methods for simple container types#447

Merged
dpgeorge merged 7 commits into
micropython:masterfrom
mikebentley15:pprint-simple
Jul 11, 2026
Merged

pprint: add simple pprint(), pp(), and pformat() methods for simple container types#447
dpgeorge merged 7 commits into
micropython:masterfrom
mikebentley15:pprint-simple

Conversation

@mikebentley15

Copy link
Copy Markdown
Contributor

Description

This change is to provide a simple implementation of pprint to replace the dummy placeholder implementation. This implementation should not be too slow nor take too much space on disk nor memory (which is the point of Micropython).

This does not implement the full specification from CPython, but enough for simple uses. It simply expands lists, sets, and dictionaries to multiple lines if there are more than one element. Useful for simple printing of lists, dictionaries, and sets to the console, especially when using the REPL.

I added one additional function that is not in the CPython implementation: ppdir(). This is a convenience function for calling pprint(dir(obj)), except by default, attributes that begin with an underscore will not be displayed (which can be changed by specifying hidden=True). I personally find this useful when using the REPL, similar to how printing works by default in iPython. If this additional convenience function is not wanted, it can be removed.

Note: this is my first pull request for Micropython. In fact, I've only been playing with it for about a week now. Please be gentle :)

Comment thread python-stdlib/pprint/pprint.py Outdated
Comment thread python-stdlib/pprint/pprint.py Outdated
'''
Simple implementation of a pretty-printer.

For simplicity, this does not recurse down. It does not produce the same

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to make it recurse anyway, as that's much more versatile? Shouldn't be too hard

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps not too hard. The main problem is that the pprint function from the standard library has safety checks such as

  • make sure not to overwhelm the output stream (by having a maximum width and meticulously checking the output and collapsing it)
  • ensure no infinite recursion (such as a list being a member of itself or a descendant of itself)

There are other tricky things like ensuring the indentation is handled properly. I do not intend to implement those checks at this time as they will add significant complexity.

If you think it would be good to have the option, then I could implement it to have a default of no recursion, but the user can specify a desired depth, at their own peril (if they're not careful) ;)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't care about the checks etc, but I mainly use pprint for looking at JSON data on the REPL (instead of e.g. opening in a text editor and pretty printing there) and I encounter nested data way more often than not. But that is just my experience.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've refactored to include depth recursion. However, since safety checks are not performed, I set the default to a depth of one instead of infinite depth.

@mikebentley15 mikebentley15 force-pushed the pprint-simple branch 2 times, most recently from 4c6b672 to 4faf413 Compare September 16, 2021 17:30
@mikebentley15

Copy link
Copy Markdown
Contributor Author

This pull request has been updated to not just handle a depth of 1, but handles any depth. The types that are expanded are limited to

  • list
  • tuple
  • set
  • dict

Without checks, it is not generally safe to set the depth to infinite, so the default is 1 in this implementation.

I have also added the convenience class PrettyPrinter which provides the ability to cache parameters and forward them on to pprint() and pformat().

@mikebentley15 mikebentley15 changed the title pprint: add simple pprint(), pp(), and pformat() methods for depth of 1 pprint: add simple pprint(), pp(), and pformat() methods for simple container types Sep 16, 2021

@dpgeorge dpgeorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution and sorry this didn't get any further attention after the initial review.

It looks good, relatively simply and provides CPython functionality.

mikebentley15 and others added 6 commits July 11, 2026 17:49
This change is to provide a simple implementation of pprint to replace the
dummy placeholder implementation.  This implementation should not be too
slow nor take too much space on disk nor memory (which is the point of
MicroPython).

This does not implement the full specification from CPython, but enough for
simple uses.  It simply expands lists, sets, and dictionaries to multiple
lines if there are more than one element.  Useful for simple printing of
lists, dictionaries, and sets to the console, especially when using the
REPL.
It won't work when imported as `_const`.

Also use non-underscore names for `io` and `sys` modules to reduce code
size (having the underscore requires a separate qstr).

Signed-off-by: Damien George <damien@micropython.org>
Lots of features were added in parent commits.

Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge merged commit 6cb320b into micropython:master Jul 11, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants