2
first,second,third = tokenize(s)
4
#print "%s is a number -> put in var1" % first
7
#print "%s is not a number -> eval" % first
8
left = evaluate(first[1:-1])
9
# if division by zero or division had reminders,
10
# consider the whole expression wrong
11
# This will speed up calculations by approx 2x
15
if is_operand(second):
16
#print "%s is an operation -> put in operation" % second
20
print "%s is not an operation, skipping (this will crash)" % second
24
#print "%s is a number -> put in var2" % third
27
#print "%s is not a number -> eval" % third
28
right = evaluate(third[1:-1])
31
result = do_op(left,operation,right)
32
#print "we return the value of %s %s %s which is %s" % (left,operation,right,result)
37
if s[0] not in ('(',')') :
38
first = read_number(s)
39
second = s[len(first)]
40
third = s[len(first)+1:]
55
#print "tokenization : %s, %s , %s" % (first,second,third)
56
return first,second,third
65
def do_op(left,operation,right):
67
# we may have None in one of those,
68
# for example when dividing by 0
70
if not (left and right) :
76
elif operation == "*" :
79
elif operation == "/" :
82
# we may only allow net divisions
86
elif operation == "-" :
93
return s in ["+","/","*","-"]
98
print evaluate(sys.argv[1])
100
if __name__ == "__main__":