skbio.io.registry.Format.reader

Format.reader(cls, monkey_patch=True, override=False)[源代码]

将函数修饰为充当此格式的类的读取器。

状态:0.4.0稳定。

该函数应该带有一个参数,该参数将是以下任一项的实现 io.TextIOBaseio.BufferedReader 分别取决于格式是文本还是二进制。用户给出的、但不是由 skbio.io.util.open() 将被传递到函数中。任何缺省值为 FileSentinel 将该参数的用户输入转换为文件句柄或 None 如未提供,请填写。

参数:
  • cls (type or None) -- 函数将被注册到处理的类。如果没有,则假定该函数将生成一个生成器。

  • monkey_patch (bool, optional) -- 是否允许IORegistry将 read 方法,以 cls 并将此格式列为选项。

  • override (bool, optional) -- 如果为True,则任何现有读取器 cls 将覆盖此格式中的。

抛出:

DuplicateRegistrationError -- 什么时候 override 为FALSE并且读卡器已注册到 cls 对于此格式。

示例

>>> from skbio.io.registry import Format, IORegistry
>>> registry = IORegistry()
>>> myformat = Format('myformat')
>>> registry.add_format(myformat)
>>> # If developing a new format for skbio, use the create_format()
>>> # factory instead of the above.
>>> class MyObject:
...     def __init__(self, content):
...         self.content = content
...
>>> @myformat.reader(MyObject)
... def myformat_reader(fh):
...     return MyObject(fh.readlines()[1:])
...
>>> registry.monkey_patch() # If developing skbio, this isn't needed
>>> MyObject.read(["myformat2\n", "some content here!\n"],
...               format='myformat').content
['some content here!\n']