Formalized pretty & encoding-aware object representation (was dunder methods for...)
Based on the conversations stemming from my previous post, it is clear that the topic was too implementation-specific. It is not clear whether dunder methods are an appropriate component of the solution (they might or might not be). Also, it presumably makes sense to start by looking at prior art rather than inventing from scratch. Quotes from previous thread regarding prior art to look at: Jonathan Fine wrote: <snip>
Here's some comments on the state of the art. In addition to https://docs.python.org/3/library/pprint.html there's also https://docs.python.org/3/library/reprlib.html and https://docs.python.org/3/library/json.html I expect that these three modules have some overlap in purpose and design (but probably not in code). And if you're brave, there's also https://docs.python.org/3/library/pickle.html and https://github.com/psf/black Time to declare a special interest. I'm a long-time user and great fan of TeX / LaTeX. And some nice way of pretty-printing Python objects using TeX notation could be useful. And also related is Geoffrey French's Larch environment for editing Python, which has a pretty-printing component. http://www.britefury.com/larch_site/ with best wishes Jonathan
Alex Hall wrote:
Might be helpful to look at https://github.com/tommikaikkonen/prettyprinter and https://github.com/wolever/pprintpp
Steve Jorgensen wrote:
Based on the conversations stemming from my previous post, it is clear that the topic was too implementation-specific. It is not clear whether dunder methods are an appropriate component of the solution (they might or might not be). Also, it presumably makes sense to start by looking at prior art rather than inventing from scratch.
There has been some argument regarding whether objects should say how to present themselves "prettily". I think a case can be made either way, but in either case, it makes sense that it should be easy to override the representation for an object type without subclassing or monkey-patching it. Also, it might make sense not to clutter up the dunder-method space for all kinds of objects with this kind of thing. Without using dunder methods, it could still be possible for any body of code to provide default special-representational rules for its objects by registering hooks. Also, as a hybrid-approach, it could be that the defaults for representation are determined first by looking at a default registry and then falling back to dunder methods if present.
On 3/16/2020 1:15 PM, Steve Jorgensen wrote:
Steve Jorgensen wrote:
Based on the conversations stemming from my previous post, it is clear that the topic was too implementation-specific. It is not clear whether dunder methods are an appropriate component of the solution (they might or might not be). Also, it presumably makes sense to start by looking at prior art rather than inventing from scratch. There has been some argument regarding whether objects should say how to present themselves "prettily". I think a case can be made either way, but in either case, it makes sense that it should be easy to override the representation for an object type without subclassing or monkey-patching it. Also, it might make sense not to clutter up the dunder-method space for all kinds of objects with this kind of thing. This is exactly what singledispatch is designed for: https://www.python.org/dev/peps/pep-0443/ Without using dunder methods, it could still be possible for any body of code to provide default special-representational rules for its objects by registering hooks. Also, as a hybrid-approach, it could be that the defaults for representation are determined first by looking at a default registry and then falling back to dunder methods if present.
Which a singledispatch default handler could do. ========================== from functools import singledispatch @singledispatch def pretty(arg): try: fn = arg.pretty except AttributeError: return f"pretty default {arg}" return fn() @pretty.register(int) def _(arg): return f"pretty int {arg}" class HasPretty: def pretty(self): return "custom pretty function" print(pretty(1)) print(pretty(object)) print(pretty('a string')) print(pretty(HasPretty())) ========================== produces: ========================== pretty int 1 pretty default <class 'object'> pretty default 'a string' custom pretty function ========================== Just add arguments if desired and change the names as necessary. Eric
Steve Jorgensen wrote:
Based on the conversations stemming from my previous post, it is clear that the topic was too implementation-specific. It is not clear whether dunder methods are an appropriate component of the solution (they might or might not be). Also, it presumably makes sense to start by looking at prior art rather than inventing from scratch. Quotes from previous thread regarding prior art to look at: Jonathan Fine wrote: <snip>
Here's some comments on the state of the art. In addition to https://docs.python.org/3/library/pprint.html there's also https://docs.python.org/3/library/reprlib.html and https://docs.python.org/3/library/json.html I expect that these three modules have some overlap in purpose and design (but probably not in code). And if you're brave, there's also https://docs.python.org/3/library/pickle.html and https://github.com/psf/black Time to declare a special interest. I'm a long-time user and great fan of TeX / LaTeX. And some nice way of pretty-printing Python objects using TeX notation could be useful. And also related is Geoffrey French's Larch environment for editing Python, which has a pretty-printing component. http://www.britefury.com/larch_site/ with best wishes Jonathan Alex Hall wrote: Might be helpful to look at https://github.com/tommikaikkonen/prettyprinter and https://github.com/wolever/pprintpp
Angus Hollands wrote:
Has anyone mentioned the IPython pretty printer yet? I'm late to the conversation unfortunately, so apologies if someone else already raised it. https://ipython.readthedocs.io/en/stable/api/generated/IPython.lib.pretty.ht...
participants (2)
-
Eric V. Smith -
Steve Jorgensen