Skip to main content
`Ctrl`+`K`

# skbio.io.registry.Format.writer

Format.writer(cls, monkey_patch=True, override=False)

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

  The function should take an instance of cls as its first argument and the second argument is a filehandle which will be an implementation of either `io.TextIOBase` or `io.BufferedWriter` depending on if the format is text or binary, respectively. Any kwargs given by the user which are not handled by `skbio.io.util.open()` 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.

  Parameters:
    cls type or None

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

    monkey_patch bool, optional

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

    override bool, optional

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

  Raises:
    DuplicateRegistrationError

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

  Examples

      >>> 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']