Last active
August 29, 2015 14:20
-
-
Save sudu/463ecd982dd57da7684a to your computer and use it in GitHub Desktop.
python super_len() 摘自https://github.com/kennethreitz/requests/blob/master/requests/utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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