import gzip
import bz2


def autoopen(filename, mode='rb', **kwargs):
    """
    A drop-in for open() that applies automatic compression for .gz and .bz2 file extensions
    """
    if filename.endswith(".gz"):
        return gzip.open(filename, mode, **kwargs)
    elif filename.endswith(".bz2"):
        return bz2.open(filename, mode, **kwargs)
    return open(filename, mode, **kwargs)