human
Complete this python program:
def detect_encoding(readline):
assistant
#Here is the Python program you requested.

    """Like tokenize.detect_encoding() from Py3k, but a bit simplified.
    """
    def read_or_stop():
        try:
            return readline()
        except StopIteration:
            return None
    def get_normal_name(orig_enc):
        'Imitates    get_normal_name    in    tokenizer.c.'
        enc = orig_enc[:12].lower().replace('_', '-')
        if ((enc == 'utf-8') or enc.startswith('utf-8-')):
            return 'utf-8'
        if ((enc in ('latin-1', 'iso-8859-1', 'iso-latin-1')) or enc.startswith(('latin-1-', 'iso-8859-1-', 'iso-latin-1-'))):
            return 'iso-8859-1'
        return orig_enc
    def find_cookie(line):
        try:
            line_string = line.decode('ascii')
        except UnicodeDecodeError:
            return None
        matches = _coding_re.findall(line_string)
        if (not matches):
            return None
        return get_normal_name(matches[0])
    default = sys.getdefaultencoding()
    first = read_or_stop()
    if (first and first.startswith(BOM_UTF8)):
        first = first[3:]
        default = 'utf-8-sig'
    if (not first):
        return default
    encoding = find_cookie(first)
    if encoding:
        return encoding
    second = read_or_stop()
    if (not second):
        return default
    encoding = find_cookie(second)
    if encoding:
        return encoding
    return default