[Python-ideas] Have REPL print less by default

Nathaniel Smith njs at pobox.com
Sun Apr 17 23:57:07 EDT 2016


On Mon, Apr 18, 2016 at 2:33 AM, Dan Sommers <dan at tombstonezero.net> wrote:
> On Sun, 17 Apr 2016 21:04:11 -0400, Franklin? Lee wrote:
>
>> These two behaviors and their corresponding functions could go into a
>> special module which is (by default) loaded by the interactive shell. The
>> behaviors can be turned off with some command-line verbosity flag, or
tuned
>> with command-line parameters (e.g. how many lines/pages to print).
>
> The existing pretty print module already does some of what you want.
> Instead of creating a new module, maybe extending that one would have
> greater benefits to more users.

There's also IPython's repr system as prior art:

http://ipython.readthedocs.org/en/stable/api/generated/IPython.lib.pretty.html#module-IPython.lib.pretty

My impression (as I guess one of the few people who have tried to
systematically add _repr_pretty_ callbacks to their libraries) is that it's
only about 30% baked and has a number of flaws and limitations, but it's
still a significant step beyond __repr__ or the stdlib pprint.

(Some issues I've run into: there's something wonky in the line breaking
that often produces misaligned lines; integration with __repr__ is awkward
if you don't want to reimplement everything twice [1]; there's no built-in
control for compressing output down to ellipses; handling the most common
case of reprs that look like Foo(bar, baz, kwarg=1) is way more awkward
than it needs to be [2]; and IIRC there were some awkward limitations in
the formatting tools provided though I don't remember the details now. But
despite all this it does make it easy to write composable reprs, so e.g.
here's a syntax tree:

In [1]: import patsy
In [2]: patsy.parse_formula.parse_formula("y ~ 1 + (a * b)")
Out[2]:
ParseNode('~',
          Token('~', <Origin y ->~<- 1 + (a * b) (2-3)>),
          [ParseNode('PYTHON_EXPR',
                     Token('PYTHON_EXPR',
                           <Origin ->y<- ~ 1 + (a * b) (0-1)>,
                           extra='y'),
                     []),
           ParseNode('+',
                     Token('+', <Origin y ~ 1 ->+<- (a * b) (6-7)>),
                     [ParseNode('ONE',
                                Token('ONE',
                                      <Origin y ~ ->1<- + (a * b) (4-5)>,
                                      extra='1'),
                                []),
                      ParseNode('*',
                                Token('*',
                                      <Origin y ~ 1 + (a ->*<- b) (11-12)>),
                                [ParseNode('PYTHON_EXPR',
                                           Token('PYTHON_EXPR',
                                                 <Origin y ~ 1 + (->a<- *
b) (9-10)>,
                                                 extra='a'),
                                           []),
                                 ParseNode('PYTHON_EXPR',
                                           Token('PYTHON_EXPR',
                                                 <Origin y ~ 1 + (a *
->b<-) (13-14)>,
                                                 extra='b'),
                                           [])])])])



-n

[1]
https://github.com/pydata/patsy/blob/23ab37519276188c4ce09db03a1c40c5b9938bfc/patsy/util.py#L356-L424
[2]
https://github.com/pydata/patsy/blob/23ab37519276188c4ce09db03a1c40c5b9938bfc/patsy/util.py#L426-L452

-- 
Nathaniel J. Smith -- https://vorpus.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160418/2f49f0c5/attachment-0001.html>


More information about the Python-ideas mailing list