repr/reprlib的短、低成本字符串表示的更好版本。
项目描述
cheap_repr
此库提供短、快、可配置的字符串表示,以及注册您自己的简单API。它是标准库模块 reprlib
(repr
在Python 2中) 的改进。
只需使用 cheap_repr
函数代替 repr
>>> from cheap_repr import cheap_repr
>>> cheap_repr(list(range(100)))
'[0, 1, 2, ..., 97, 98, 99]'
cheap_repr
默认知道如何处理许多不同类型的表示。您可以注册任何类型的函数,并且欢迎将它们作为库的一部分的拉取请求。如果它不知道如何处理特定类型,将使用默认的 repr()
,可能被截断
>>> class MyClass(object):
... def __init__(self, items):
... self.items = items
...
... def __repr__(self):
... return 'MyClass(%r)' % self.items
...
>>> c = MyClass(list(range(20)))
>>> c
MyClass([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
>>> cheap_repr(c)
'MyClass([0, 1, 2, 3, 4, 5, 6... 13, 14, 15, 16, 17, 18, 19])'
抑制长reprs
cheap_repr
的目的是防止慢速、昂贵的字符串表示计算。因此,如果它发现某个类可能会产生非常长的表示,该类将被 抑制,这意味着将来将完全不计算 __repr__
>>> cheap_repr(MyClass(list(range(1000))))
'MyClass([0, 1, 2, 3, 4, 5, 6...94, 995, 996, 997, 998, 999])'
.../cheap_repr/__init__.py:80: ReprSuppressedWarning: MyClass.__repr__ is too long and has been suppressed. Register a repr for the class to avoid this warning and see an informative repr again, or increase cheap_repr.suppression_threshold
>>> cheap_repr(MyClass(list(range(1000))))
'<MyClass instance at 0x1034de7f0 (repr suppressed)>'
cheap_repr.suppression_threshold
指的是函数本身的属性,而不是模块。默认值为300,这意味着超过300个字符的 repr
将触发抑制。
注册您自己的repr函数
例如
>>> from cheap_repr import register_repr
>>> @register_repr(MyClass)
... def repr_my_class(x, helper):
... return helper.repr_iterable(x.items, 'MyClass([', '])')
...
>>> cheap_repr(MyClass(list(range(1000))))
'MyClass([0, 1, 2, 3, 4, 5, ...])'
通常,编写一个接收两个参数 (x, helper)
的函数,并用 register_repr(cls)
装饰它。然后 cheap_repr(x)
在 isinstance(x, cls)
时将调度到该函数,除非还有为 x
是其实例的子类注册的函数。更准确地说,将使用 MRO 中第一个类的对应函数。这与无法处理未显式 '注册' 的子类或具有相同名称的类的标准库模块 reprlib
相比。
helper
参数是一个具有一些有用属性和方法的对象
-
repr_iterable(iterable, left, right, end=False, length=None)
生成iterable
的逗号分隔表示形式,自动处理嵌套和过长的可迭代对象,用left
和right
包围。项目数量限制为func.maxparts
(请参阅下面的配置部分)。设置
end=True
以包括开始和结束的项目,可能会省略中间的项目。只有当iterable
支持高效切片在末尾时才这样做,例如iterable[-3:]
。如果
len(iterable)
不适用,请提供length
参数。通常不需要这样做。 -
truncate(string)
返回一个最多func.maxparts
个字符的string
版本,如果需要,中间用...
替换。 -
level
表示结果中允许的嵌套级别。如果它是 0,则返回一个最小化表示,如[...]
,以指示原始对象太深而无法显示所有内容。否则,如果您在x
内部的多个项上使用cheap_repr
,请将helper.level - 1
作为第二个参数传递,例如', '.join(cheap_repr(item, helper.level - 1) for item in x)
。
repr 函数中的异常
如果在 cheap_repr
中发生异常,无论是来自已注册的 repr 函数还是通常的 __repr__
,异常将被捕获,并抑制类的 cheap repr。
>>> @register_repr(MyClass)
... def repr_my_class(x, helper):
... return 'MyClass([%r, ...])' % x.items[0]
...
>>> cheap_repr(MyClass([]))
'<MyClass instance at 0x10f44ec50 (exception in repr)>'
.../cheap_repr/__init__.py:123: ReprSuppressedWarning: Exception 'IndexError: list index out of range' in repr_my_class for object of type MyClass. The repr has been suppressed for this type.
...
>>> cheap_repr(MyClass([1, 2, 3]))
'<MyClass instance at 0x10f44ecc0 (repr suppressed)>'
如果您希望异常正常冒泡,您可以
- 将
cheap_repr.raise_exceptions
设置为True
以全局使所有异常冒泡。 - 要使类的
__repr__
的异常冒泡,请调用raise_exceptions_from_default_repr()
。 - 将
repr_my_class.raise_exceptions
设置为True
(替换您的注册 repr 函数)以使该函数的异常冒泡。找到相关函数的方法在下文中。
配置
特定函数的配置
要配置特定函数,您在该函数上设置属性。要找到与类对应的函数,请使用 find_repr_function
。
>>> from cheap_repr import find_repr_function
>>> find_repr_function(MyClass)
<function repr_my_class at 0x10f43d8c8>
对于大多数函数,有两个可配置的属性,但鼓励贡献者和库编写者为自己函数添加任意属性。第一个属性是 raise_exceptions
,在上一节中描述。
maxparts
另一个可配置的属性是 maxparts
。所有已注册的 repr 函数都有此属性。它确定输出可以显示而不截断的输入的最大 '部分' 数(例如列表元素或字符串字符,含义取决于函数)。默认值为 6。装饰器 @maxparts(n)
便利地设置属性,使编写自己的注册函数更简单。例如
>>> from cheap_repr import maxparts
>>> @register_repr(MyClass)
... @maxparts(2)
... def repr_my_class(x, helper):
... return helper.repr_iterable(x.items, 'MyClass([', '])')
...
>>> cheap_repr(MyClass([1, 2, 3, 4]))
'MyClass([1, 2, ...])'
>>> find_repr_function(MyClass).maxparts = 3
>>> cheap_repr(MyClass([1, 2, 3, 4]))
'MyClass([1, 2, 3, ...])'
pandas
来自 pandas
库的 DataFrame
和 Series
函数不使用 maxparts
。对于 DataFrame
函数,有 max_rows
和 max_cols
。对于 Series
函数,只有 max_rows
。
level 和 max_level
cheap_repr
函数接受一个可选参数 level
,它控制嵌套对象的显示。通常,这个值通过递归调用而递减,当其值为 0 时,不会显示对象的内部内容。有关更多详情,请参阅“注册自定义 repr 函数”。这意味着您可以通过更改 level
参数来改变 cheap_repr
输出中的嵌套数据量。默认值是 cheap_repr.max_level
,初始值为 3。这意味着更改 cheap_repr.max_level
将在未显式指定时有效改变 level
参数。
全局配置
以下是可以全局配置的内容
cheap_repr.suppression_threshold
,在“抑制长 repr”部分中讨论。- 在“repr 函数中的异常”部分讨论了异常的处理,可以通过设置
cheap_repr.raise_exceptions = True
或调用raise_exceptions_from_default_repr()
来改变。 - 上面提到的
cheap_repr.max_level
。
其他杂项函数
basic_repr(x)
返回一个类似于默认的 object.__repr__
的字符串。如果您不想编写自己的 repr 函数来注册,这将很有用。只需注册此函数即可,例如:
>>> from cheap_repr import basic_repr
>>> register_repr(MyClass)(basic_repr)
>>> cheap_repr(MyClass([1, 2, 3, 4]))
'<MyClass instance at 0x10f39dda0>'
normal_repr(x)
返回 repr(x)
- 将其注册到类中以表示其自身的 __repr__
方法已经足够好。这可以防止其输出过长时被抑制。
try_register_repr
在您想要为可能不存在的类注册 repr 函数时很有用,例如,如果该类位于可能未安装的第三方包中。有关更多详情,请参阅文档字符串。
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分布
构建分布
cheap_repr-0.5.2.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 001a5cf8adb0305c7ad3152c5f776040ac2a559d97f85770cebcb28c6ca5a30f |
|
MD5 | 8004ea6c36f2aab35b0ad55b81db5f3a |
|
BLAKE2b-256 | b130f0e9d5bfe80b8287ea8a9263eb3c71c5fdf44b6f7a781a7c96f83172ccad |
cheap_repr-0.5.2-py2.py3-none-any.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 537ec1991bfee885c13c6d473afd110a408e039cde26882e95bf92761556ab6e |
|
MD5 | f0266af97a5c83022e03d89fa4bf6561 |
|
BLAKE2b-256 | ec52fec0262af470a157a557e46be1d52ecdaf1695cefd80bb62bb6a07cc4ea9 |