Skip to content
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

How to defer pretty printing to builtin pprint? #11434

Open
saulshanabrook opened this issue Oct 22, 2018 · 1 comment
Open

How to defer pretty printing to builtin pprint? #11434

saulshanabrook opened this issue Oct 22, 2018 · 1 comment

Comments

@saulshanabrook
Copy link

I have a custom class and would like IPython to use pprint when converting it to pretty text, since I have already defined the pretty print implementation of my class.

I am able to do this by overriding _repr_pretty to defer to pprint, like this:

matchpy.Operation._repr_pretty_ = lambda self, pp, cycle: pp.text(pprint.pformat(self))

However, then the output is not aligned properly, since Python's pprint doesn't know we are already indented. Here is an example, where Function should be indented more:

('/Users/saul/p/uarray/uarray/uarray/numpy.py:320',
 'Calling %s(*%s, **%s)',
 (<function __main__.some_fn(a, b)>,
  [ArrayLike(Sequence(Value('10000'),
                   Function(Scalar(Content(Call(Content(NumpyNDArray('array([   0,    1,    2, ..., 9997, 9998, 9999])')),
                                                Unbound('', variable_name=i0)))),
                            Unbound('', variable_name=i0)))),
   ArrayLike(Sequence(Value('10000'),
                   Function(Scalar(Content(Call(Content(NumpyNDArray('array([   0,    1,    2, ..., 9997, 9998, 9999])')),
                                                Unbound('', variable_name=i1)))),
                            Unbound('', variable_name=i1))))],
  {}))

Does anyone either have a tip on how to indent this properly or a way to more seamlessly integrate IPython's pprinting with Python's own pprint module?

@Carreau
Copy link
Member

Carreau commented Oct 27, 2018

I don't have any answer on that, pretty_print is relatively rarely used, and not the best API imho.

My take would be in your _repr_pretty_ to intercept the pp argument and try to implement a kind-of "dedent" functionality.

Not sure how easy it would be.

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

No branches or pull requests

2 participants