import six


def to_bytes(content, encoding='utf-8', errors='strict'):
    """Convert a sequence to a bytes type.

    Borrowed from `PySerial <https://github.com/pyserial/pyserial>`_ since
    it is now optional.
    """
    if isinstance(content, bytes):
        # Python 2: isinstance('', bytes) is True
        return bytes(content)
    elif isinstance(content, bytearray):
        return bytes(content)
    elif isinstance(content, memoryview):
        return content.tobytes()
    elif isinstance(content, six.string_types):
        return bytes(content.encode(encoding, errors))
    else:
        # handle list of integers and bytes (one or more items)
        # for Python 2 and 3
        return bytes(bytearray(content))