def convert(s):
    '''convert a list of token tuples from infix order to RPN'''
    input=s[:]
    stack=[]                            # 'Texas' LIFO
    output=[]                            # 'California' output accumulator
    while len(input)>0: # Careful!  We're iterating over changing input list.
        if input[0][0]=='IDENT' or input[0][0]=='NUMBER':
            # move operands directly to output
            output.append(input.pop(0))
        elif input[0][0]=='LPAREN':
            # Now handle a left parenthesis
            stack.append(input.pop(0))
        elif input[0][0]=='RPAREN':
            # Now handle a right parenthesis
            if not stack:
                # We shouldn't have an RPAREN without an LPAREN
                raise UnmatchedParenthesis
            elif stack[-1][0]=='LPAREN':
                # when RPAREN catches up with LPAREN, vaporize both
                stack.pop()
                input.pop(0)
            else:
                # pop operands until we hit LPAREN
                output.append(stack.pop())
        else:
            # logically, we should have an operand at the input, but
            # don't know about stack
            if stack:
                # there is at least one item in the stack
                if stack[-1][0]=='LPAREN':
                    # LPAREN on stack, push next operand to stack
                    stack.append(input.pop(0))
                elif PRIORITY[input[0][0]]>PRIORITY[stack[-1][0]]:
                    # higher priority input item goes to stack
                    stack.append(input.pop(0))
                else:
                    output.append(stack.pop())
            else:
                # no stack, push input to stack
                stack.append(input.pop(0))
    while len(stack)>0:
        # when input is empty we still may have stack items to move over
        if stack[-1][0]=='LPAREN':
            # Shouldn't be any LPARENs left!
            raise UnmatchedParenthesis
        else:
            output.append(stack.pop())
    return output
convert a list of token tuples from infix order to RPN
recipes/Python/577333_Numerical_type_units/recipe-577333.py