<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def open_compressed(filename, mode="r", encoding=None):
    "Return a text-based file object from a filename, even if compressed"

    # TODO: integrate this function in the library itself, using
    # get_filename_and_fobj
    binary_mode = "b" in mode
    extension = str(filename).split(".")[-1].lower()
    if binary_mode and encoding:
        raise ValueError("encoding should not be specified in binary mode")

    if extension == "xz":
        if lzma is None:
            raise RuntimeError("lzma support is not installed")

        fobj = lzma.open(filename, mode=mode)
        if binary_mode:
            return fobj
        else:
            return io.TextIOWrapper(fobj, encoding=encoding)

    elif extension == "gz":
        fobj = gzip.GzipFile(filename, mode=mode)
        if binary_mode:
            return fobj
        else:
            return io.TextIOWrapper(fobj, encoding=encoding)

    elif extension == "bz2":
        if bz2 is None:
            raise RuntimeError("bzip2 support is not installed")

        if binary_mode:  # ignore encoding
            return bz2.open(filename, mode=mode)
        else:
            if "t" not in mode:
                # For some reason, passing only mode='r' to bzip2 is equivalent
                # to 'rb', not 'rt', so we force it here.
                mode += "t"
            return bz2.open(filename, mode=mode, encoding=encoding)

    else:
        if binary_mode:
            return open(filename, mode=mode)
        else:
            return open(filename, mode=mode, encoding=encoding)