Skip to content

Instantly share code, notes, and snippets.

@sudu
Last active August 29, 2015 14:20
Show Gist options
  • Save sudu/463ecd982dd57da7684a to your computer and use it in GitHub Desktop.
Save sudu/463ecd982dd57da7684a to your computer and use it in GitHub Desktop.
import io
import os
def super_len(o):
if hasattr(o, '__len__'):
return len(o)
if hasattr(o, 'len'):
return o.len
if hasattr(o, 'fileno'):
try:
fileno = o.fileno()
except io.UnsupportedOperation:
pass
else:
return os.fstat(fileno).st_size
if hasattr(o, 'getvalue'):
# e.g. BytesIO, cStringIO.StringIO
return len(o.getvalue())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment