Format.reader(cls, monkey_patch=True, override=False)[source]

Decorate a function to act as the reader for a class in this format.

State: Stable as of 0.4.0.

The function should take an argument which will be an implementation of either io.TextIOBase or io.BufferedReader depending on if the format is text or binary, respectively. Any kwargs given by the user which are not handled by will be passed into the function. Any kwarg with a default of FileSentinel will transform user input for that parameter into a filehandle or None if not provided.


cls : type or None

The class which the function will be registered to handle. If None, it is assumed that the function will produce a generator.

monkey_patch : bool, optional

Whether to allow an IORegistry to attach a read method to cls with this format listed as an option.

override : bool, optional

If True, any existing readers for cls in this format will be overriden.



When override is False and a reader is already registered to cls for this format.


>>> from 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
>>>["myformat2\n", "some content here!\n"],
...               format='myformat').content
['some content here!\n']