def redofunctions(txt):
    if txt.strip().startswith("#"):
        return txt
    if not txt.endswith("{"):
        return txt
    lbr = txt.find("(")
    rbr = txt.find(") {")
    if lbr == -1 or rbr == -1 or lbr > rbr:
        return txt
    lbr2 = txt.find("(", lbr+1)
    if lbr2 > 0:
        return txt
    count = countspaces(txt)
    pre = txt[count:lbr]
    args = txt[lbr+1:rbr]

    if pre.strip() == 'switch':
        return txt

    is_exception = 0
    if pre.find("except") >= 0:
        is_exception = 1
    else:
        pre = map(string.strip, pre.split(' '))
        if len(pre) == 1: # assume it's a constructor
            pre = '__init__'
        elif len(pre) == 2:
            pre = pre[-1] # drop the first word (return type)
        else:
            print txt, pre
            error # deliberately cause error - investigate 3-word thingies!

    args = map(string.strip, args.split(','))
    newargs = []
    for arg in args:
        if arg == '':
            continue
        arg = map(string.strip, arg.split(' '))
        if len(arg) == 2:
            newargs.append(arg[1]) # drop first word (arg type)
        else:
            print txt
            print pre, args, arg
            error # deliberately cause error - find out why arg no type
    if count != 0 and not is_exception:
        # assume class not global function - add self
        newargs = ['self'] + newargs
    newargs = ', '.join(newargs)
    if is_exception:
        return "%s%s%s:" % (count*' ', pre, newargs)
    return "%sdef %s(%s):" % (count*' ', pre, newargs)
dataset/ETHPy150Open anandology/pyjamas/contrib/java2py.py/redofunctions/original