<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write(self, document, obj, *args, **kwargs):
 """ Returns a Deferred that fire the factory result that should be the document. """

        try:
            document = IWritableDocument(document)
            mime_type = document.mime_type
            writer = self.lookup_writer(mime_type, obj)
            if not writer:
                msg = ("No adapter found to write object %s to %s document"
                       % (obj.__class__.__name__, mime_type))
                raise NoWriterFoundError(msg)
            return writer.write(document, obj, *args, **kwargs)
        except:
            return defer.fail(Failure())