skbio.io.registry.Format.sniffer

Format.sniffer(override=False)[源代码]

修饰一个函数以充当此格式的嗅探器。

状态:0.4.0稳定。

该函数应该接受一个参数,该参数将是以下任一项的实现 io.TextIOBaseio.BufferedReader 分别取决于格式是文本还是二进制。

嗅探器将始终接收指向文件开头的文件句柄。它必须返回一个bool元组和一个建议的关键字参数词典(如果有)以传递给阅读器。

备注

中不允许使用关键字参数 sniffers. Sniffers may not raise exceptions; if an exception is thrown by a sniffer, the user will be asked to report it on our issue tracker

参数:

override (bool, optional) -- 如果为True,则现有嗅探器将被重写。

抛出:

DuplicateRegistrationError -- 什么时候 override 为FALSE,并且已经为此格式注册了嗅探器。

示例

>>> from skbio.io.registry import Format
>>> # If developing a new format for skbio, use the create_format()
>>> # factory instead of this constructor.
>>> myformat = Format('myformat')
>>> @myformat.sniffer()
... def myformat_sniffer(fh):
...     check = fh.read(8) == "myformat"
...     if check:
...         version = int(fh.read(1))
...         return True, {'version': version}
...     return False, {}
...
>>> myformat_sniffer(["myformat2\n", "some content\n"])
(True, {'version': 2})
>>> myformat_sniffer(["something else\n"])
(False, {})