实现自定义漂亮打印机的最佳方式 [英] best way to implement custom pretty-printers

查看:31
本文介绍了实现自定义漂亮打印机的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pprint 模块的文档提到方法 PrettyPrinter.format 旨在使自定义格式成为可能.

The documentation for the pprint module mentions that the method PrettyPrinter.format is intended to make it possible to customize formatting.

我认为可以在子类中覆盖此方法,但这似乎没有提供让基类方法应用换行和缩进的方法.

I gather that it's possible to override this method in a subclass, but this doesn't seem to provide a way to have the base class methods apply line wrapping and indentation.

  • 我在这里遗漏了什么吗?
  • 有没有更好的方法来做到这一点(例如另一个模块)?

我已经检查了 pretty 模块,这看起来很有趣,但似乎没有提供一种方法来自定义来自其他模块的类的格式而不修改这些模块.

I've checked out the pretty module, which looks interesting, but doesn't seem to provide a way to customize formatting of classes from other modules without modifying those modules.

我认为我正在寻找的东西可以让我提供类型(或可能是函数)的映射,以识别类型到处理节点的例程.处理节点的例程将获取一个节点并返回它的字符串表示形式,以及一个子节点列表.等等.

I think what I'm looking for is something that would allow me to provide a mapping of types (or maybe functions) that identify types to routines that process a node. The routines that process a node would take a node and return the string representation it, along with a list of child nodes. And so on.

我的最终目标是紧凑地打印 DocBook 格式的 xml.etree.ElementTree 的自定义格式部分.

My end goal is to compactly print custom-formatted sections of a DocBook-formatted xml.etree.ElementTree.

(我很惊讶没有找到对 DocBook 的更多 Python 支持.也许我错过了一些东西.)

(I was surprised to not find more Python support for DocBook. Maybe I missed something there.)

我在名为 xmlearn 的客户端中构建了一些基本功能,该客户端使用了 lxml.例如,要转储 Docbook 文件,您可以:

I built some basic functionality into a client called xmlearn that uses lxml. For example, to dump a Docbook file, you could:

xmlearn -i docbook_file.xml dump -f docbook -r book

这很糟糕,但它为我提供了我正在寻找的信息.

It's pretty half-ass, but it got me the info I was looking for.

xmlearn 还具有其他功能,例如能够构建图形图像并进行显示关系的转储XML 文档中的标记之间.这些与这个问题几乎完全无关.

xmlearn has other features too, like the ability to build a graph image and do dumps showing the relationships between tags in an XML document. These are pretty much totally unrelated to this question.

您还可以执行任意深度的转储,或指定 XPath 作为一组起点.XPath 的东西有点过时了特定于 docbook 的格式,所以它不是很发达.

You can also perform a dump to an arbitrary depth, or specify an XPath as a set of starting points. The XPath stuff sort of obsoleted the docbook-specific format, so that isn't really well-developed.

这仍然不是问题的真正答案.我仍然希望有一款易于定制的漂亮打印机.

This still isn't really an answer for the question. I'm still hoping that there's a readily customizable pretty printer out there somewhere.

推荐答案

我的解决方案是用一个简单的包装器替换 pprint.PrettyPrinter,该包装器在调用原始打印机之前格式化它找到的任何浮点数.

My solution was to replace pprint.PrettyPrinter with a simple wrapper that formats any floats it finds before calling the original printer.

from __future__ import division
import pprint
if not hasattr(pprint,'old_printer'):
    pprint.old_printer=pprint.PrettyPrinter

class MyPrettyPrinter(pprint.old_printer):
    def _format(self,obj,*args,**kwargs):
        if isinstance(obj,float):
            obj=round(obj,4)
        return pprint.old_printer._format(self,obj,*args,**kwargs)
pprint.PrettyPrinter=MyPrettyPrinter

def pp(obj):
    pprint.pprint(obj)

if __name__=='__main__':
    x=[1,2,4,6,457,3,8,3,4]
    x=[_/17 for _ in x]
    pp(x)

这篇关于实现自定义漂亮打印机的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆