Skip to content

Instantly share code, notes, and snippets.

@igorsobreira
Created October 30, 2012 00:50
Show Gist options
  • Save igorsobreira/3977650 to your computer and use it in GitHub Desktop.
Save igorsobreira/3977650 to your computer and use it in GitHub Desktop.
def assert_file_like_object(self, obj):
interface = set(('open', 'close', 'encoding', 'fileno', 'flush', 'isatty',
'newlines', 'read', 'readinto', 'readline', 'readlines',
'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines',
'xreadlines', 'name'))
methods = set(dir(obj))
has_valid_api = interface.issubset(methods)
missing_methods = ', '.join(interface.difference(methods))
self.assertTrue(has_valid_api,
"{0} should be file-like object. Missing methods/attributes: {1}"
.format(obj, missing_methods))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment