## RPN template math

 ``` 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98``` ```from django import template register = template.Library() class stack: def __init__(self): self.stack = [] def push(self, o): self.stack.append(o) #print 'push', self.stack def pop(self): if len(self.stack) == 0: raise KeyError, "Stack is empty" o = self.stack[-1] #print 'pop', self.stack del self.stack[-1] return o def is_empty(self): return len(self.stack) == 0 def __len__(self): return len(self.stack) # truncate a floating point number only if it has no decimal part (convert from string if necessary) def number(num): f = float(num) i = int(f) if i == f: #FIXME: floating point equality? return i return f stacks = {} @register.filter def stnew(value): #print 'stnew' stacks[value] = stack() return value @register.filter def stpush(value, arg): #print 'stpush:', stacks[value].push(number(arg)) return value @register.filter def stpop(value): #print 'stpop:', if value in stacks: stacks[value].pop() return value @register.filter def stget(value): #print 'stget:', if value in stacks: return stacks[value].pop() @register.filter def stadd(value): #print 'stadd:', two = stacks[value].pop() one = stacks[value].pop() stacks[value].push(one + two) return value @register.filter def stsub(value): #print 'stsub:', two = stacks[value].pop() one = stacks[value].pop() stacks[value].push(one - two) return value @register.filter def stmult(value): #print 'stmult:', two = stacks[value].pop() one = stacks[value].pop() stacks[value].push(one * two) return value @register.filter def stdiv(value): #print 'stdiv:', two = stacks[value].pop() one = stacks[value].pop() stacks[value].push(number(float(one) / float(two))) return value @register.filter def stmod(value): two = stacks[value].pop() one = stacks[value].pop() stacks[value].push(one % two) return value ```

### More like this

1. FloatField with safe expression parsing by joelegner 3 years, 2 months ago
2. Convert numbers in USA notation to brazilian notation by eOliva 4 years, 7 months ago
3. math tag by itchyfingrs 2 years ago
4. math filter by itchyfingrs 2 years, 1 month ago
5. Filter change list by a date range by aruseni 1 year, 3 months ago