def open_as_needed(filename):
    """Return a file-object given either a filename or an object.

    Handles opening with the right class based on the file extension.

    """
    if hasattr(filename, 'read'):
        return filename

    if filename.endswith('.bz2'):
        return bz2.BZ2File(filename, 'rb')
    elif filename.endswith('.gz'):
        return gzip.GzipFile(filename, 'rb')
    else:
        return open(filename, 'rb')
def open_as_needed ( filename ) : if hasattr ( filename , 'read' ) : return filename if filename . endswith ( '.bz2' ) : return bz2 . BZ2File ( filename , 'rb' ) elif filename . endswith ( '.gz' ) : return gzip . GzipFile ( filename , 'rb' ) else : return open ( filename , 'rb' )
Return a file-object given either a filename or an object.

    Handles opening with the right class based on the file extension.