Welcome, guest | Sign In | My Account | Store | Cart

This recipe will insert multiplication symbols and convert all grouping symbols to equivalent parentheses. Uses the tokenize module for compact coding.

Python, 57 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def toProperFormula(s):
    """
    Given formula string, returns a modified formula with missing 
    multiplication symbols and grouping symbols [],{} replaced by parentheses.

    Only primitive error checking for mismatched grouping symbols is shown in 
    this recipe.

    author: ernesto.adorio@gmail.com, ernie@extremecomputing.org
    """
    import tokenize
    from cStringIO import StringIO
    
    f          = StringIO(s)
    
    # Make variables mutable to child function.
    formula    = [""]
    prevtoken  = [""]
    prevsymbol = [""]
    closers    = []
    
    def handle_token(type, token, (srow, scol), (erow, ecol), line):
         token  = str(token)
         symbol = tokenize.tok_name[type]
         
         if symbol == "OP":
             if token == ")":
                if  closers.pop() != "(":  raise FormulaError('Error: "' +line[:ecol] + '" unbalanced ).')
             elif token == "]":
                if closers.pop() != "[":   raise FormulaError('Error: "' +line[:ecol] + '" unbalanced ].')
                token = ")"
             elif token == "}":
                if closers.pop() != "{":   raise FormulaError('Error: "' +line[:ecol] + '" unbalanced }.')
                token = ")"
             elif token in ["(", "[", "{"]:
                closers.append(token)
                if prevtoken[0] == ")" or prevsymbol[0] == "NUMBER":
                    formula[0] += "*"
                token = "("
         elif symbol in ["NAME", "NUMBER"]:
             if prevtoken[0] == ")" or prevsymbol[0] in ["NAME", "NUMBER"]:
                 formula[0] += "*"

         formula[0]    += token
         prevtoken[0]  =  token
         prevsymbol[0] =  symbol
        
    tokenize.tokenize(f.readline, handle_token)
    return formula[0]



print toProperFormula("2 ( 23.45x - 4y) [34 - 5 x] + w^[atan2(2y, 4x)] 5")

"""
2*(23.45*x-4*y)*(34-5*x)+w^(atan2(2*y,4*x))*5
"""

This formula allows an ordinary user to input a formula string more naturally for processing by a program.