pprint: add simple pprint(), pp(), and pformat() methods for simple container types#447
Conversation
| ''' | ||
| Simple implementation of a pretty-printer. | ||
|
|
||
| For simplicity, this does not recurse down. It does not produce the same |
There was a problem hiding this comment.
Would it be possible to make it recurse anyway, as that's much more versatile? Shouldn't be too hard
There was a problem hiding this comment.
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) ;)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
4c6b672 to
4faf413
Compare
|
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
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 |
dpgeorge
left a comment
There was a problem hiding this comment.
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.
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>
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 callingpprint(dir(obj)), except by default, attributes that begin with an underscore will not be displayed (which can be changed by specifyinghidden=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 :)