使文明的repr、str和unicode方法
autorepr的Python项目详细描述
现在支持Python3!
概述
python使类变得简单,但是__repr__方法却很难。你忘了吗 再次引用self?可能。你有没有想过 班级真的很简单,不需要代表吗?毫无疑问。是生产 因为你的__str__返回unicode而在上周被删除了三次?… 不?也许那只是我。
autorepr使构建表达性、安全性和正确性变得简单, ^方法中的{tt1}$、__str__、__unicode__和__bytes__方法 每条线一条。
使用autorepr,您可以获得所需的曲目,而无需担心 不稳定的位(如编码和解码),让您专注于您的项目:
>>>fromautoreprimportautorepr,autotext>>>classPerson(object):...name=u"Alex ☃"...height=123.456......__repr__=autorepr(["name","height:0.1f"])...__str__,__unicode__=autotext("{self.name} ({self.height:0.0f} cm)")...>>>p=Person()>>>repr(p)"<__main__.Person name=u'Alex \\u2603' height=123.5 at 0x...>">>>unicode(p)u'Alex \u2603 (123 cm)'>>>str(p)'Alex \xe2\x98\x83 (123 cm)'
安装
$ pip install autorepr
用法
autorepr公开两个主要函数:
autorepr,它通过传递 一个str.format样式的字符串,或者一个应该是 包含在name=value列表中:
autorepr(["name", "height:0.1f"]) --> "<pkg.Person name=u'Alex \u2603' height=123.5 at 0x...>" autorepr("{self.id} name={self.name!r}") --> "<pkg.Person 123 name=u'Alex \u2603' at 0x...>"
autotext,它使用autostr和autounicode创建 __str__和__unicode__方法以python 2+3友好方式:
__str__, __unicode__ = autotext("{self.name} ({self.height!d} cm)") --> str: 'Alex \xe2\x98\x83 (123cm)' unicode: u'Alex \u2603 (123cm)'
以及三个次要函数-autostr,autounicode,和 autobytes-生成__str__、__unicode__和__bytes__ 分别是函数。这些函数将尽力避免使用unicode 编码/解码错误,通常会做正确的事情,即使 投入未必明智。
注意:这里显示的示例是Python2,但是一切都运行良好 在python 3下。
>>>fromautoreprimportautorepr,autotext,autostr,autounicode>>>classPerson(object):...name=u"Alex ☃"...height=123.456......__repr__=autorepr(["name","height:0.1f"])...__str__,__unicode__=autotext("{self.name} ({self.height:0.0f} cm)")...>>>p=Person()>>>repr(p)"<__main__.Person name=u'Alex \\u2603' height=123.5 at 0x...>">>>unicode(p)u'Alex \u2603 (123 cm)'>>>str(p)'Alex \xe2\x98\x83 (123 cm)'
注意autostr和autorepr(在这里通过autotext调用) 能够智能地转换为/从Unicode(解码/编码为UTF-8) 必要时:
>>>p.name=u"unicode: ☃">>>unicode(p)u'unicode: \u2603 (123 cm)'>>>str(p)'unicode: \xe2\x98\x83 (123 cm)'>>>p.name='utf-8 bytes: \xe2\x98\x83'>>>unicode(p)u'utf-8 bytes: \u2603 (123 cm)'>>>str(p)'utf-8 bytes: \xe2\x98\x83 (123 cm)'
note:autostr和autorepr不会在无效的utf-8上崩溃(例如, 如果要求autounicode将二进制数据转换为Unicode,但结果是 是未定义的,可能不可取。
其他属性可以作为kwargs传入,将使用 实例作为参数:
>>>name_with_len=autostr("{self.name} length={len}",...len=lambdaself:len(self.name))...>>>p.name='Alex'>>>name_with_len(p)'Alex length=4'
这也适用于autorepr的列表模式:
>>>repr_with_len=autorepr(["name","len"],...len=lambdaself:len(self.name))...>>>repr_with_len(p)"<__main__.Person name='Alex' len=4 at 0x...>"
如果将常规格式字符串传递给autorepr,则它将使用该字符串 生成字符串的数目:
>>>repr_with_str=autorepr("{self.name!r}")>>>repr_with_str(p)"<__main__.Person 'Alex' at 0x...>"
当然,如果你不想把你的__repr__包装起来 <ClassName ...>,您可以使用autostr:
>>>repr_with_autostr=autostr("Person({self.name!r})")>>>repr_with_autostr(p)"Person('Alex')"
如果默认为 !r是不需要的(例如,截断浮点):
>>>with_fmt_spec=autorepr(["duration:0.1f","addr:x","type!s"],...duration=lambdax:123.456,...addr=lambdax:0xabc123,...type=lambdax:"foo")>>>with_fmt_spec(None)'<....NoneType duration=123.5 addr=abc123 type=foo at 0x...>'