skbio.io.registry.Format.writer

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

修饰函数以充当此格式的类的编写器。

状态:0.4.0稳定。

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

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

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

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

抛出:

DuplicateRegistrationError -- 什么时候 override 为假,并且编写器已注册到 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:
...     default_write_format = 'myformat'
...     def __init__(self, content):
...         self.content = content
...
>>> @myformat.writer(MyObject)
... def myformat_reader(obj, fh):
...     fh.write("myformat2\n")
...     for c in obj.content:
...         fh.write(c)
...
>>> registry.monkey_patch() # If developing skbio, this isn't needed
>>> obj = MyObject(["some content here!\n"])
>>> obj.write([], format='myformat')
['myformat2\n', 'some content here!\n']