Home | Trees | Indices | Help |
---|
|
1 #!/usr/bin/env python 2 """ 3 Module implements the Formula class for cell formulae. 4 Created Oct 2008 5 Copyright (C) Damien Farrell 6 7 This program is free software; you can redistribute it and/or 8 modify it under the terms of the GNU General Public License 9 as published by the Free Software Foundation; either version 2 10 of the License, or (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 """ 21 22 #import sys, os 23 from Tkinter import * 24 from types import * 25 import re28 """A class to handle formulas functionality in the table""" 29 30 #replace symbols in recnames with strings for proper parsing 31 #replace = {'+':'plus', '-':'minus', '*':'mult', 32 36 37 @classmethod13939 """Evaluate the cell and return true if its a formula""" 40 isform = False 41 if type(rec) is DictType: 42 if rec.has_key('formula'): 43 isform = True 44 return isform45 46 @classmethod48 """Get the formula field string""" 49 if not type(rec) is DictType: 50 return None 51 string = rec['formula'] 52 #print string 53 return string54 55 @classmethod57 """Get the operands and operators into lists from a string expression""" 58 ops = [] 59 vals = [] 60 p = re.compile('[()*/+-]') 61 x = p.split(expr) 62 ops = p.findall(expr) 63 #print expr, ops 64 for i in x: 65 if i == '': 66 vals.append(i) 67 else: 68 vals.append(eval(i)) 69 70 #print ops, vals 71 return vals, ops72 73 @classmethod75 """Create an expression string from provided operands and operators""" 76 expr = '' 77 if getvalues == True: 78 for i in range(len(vals)): 79 if vals[i] != '': 80 vals[i] = float(vals[i]) 81 if len(ops)>len(vals): 82 while len(ops): 83 #use lists as queues 84 expr += ops.pop(0) 85 if len(vals)!=0: 86 v=vals.pop(0) 87 if v == '': 88 pass 89 else: 90 expr += str(v) 91 elif len(ops)<len(vals): 92 while len(vals): 93 #use lists as queues 94 v=vals.pop(0) 95 if v == '': 96 pass 97 else: 98 expr += str(v) 99 if len(ops)!=0: 100 expr += ops.pop(0) 101 return expr102 103 @classmethod105 """Evaluate the formula for a cell and return the result 106 takes a formula dict or just the string as input""" 107 if type(cellformula) is DictType: 108 cellformula = cellformula['formula'] 109 110 vals = [] 111 cells, ops = cls.readExpression(cellformula) 112 113 #get cell records into their values 114 for i in cells: 115 if type(i) is ListType: 116 recname, col= i 117 if data.has_key(recname): 118 if data[recname].has_key(col): 119 v = data[recname][col] 120 if cls.isFormula(v): 121 #recursive 122 v = cls.doFormula(cls.getFormula(v),data) 123 vals.append(v) 124 else: 125 return '' 126 else: 127 return '' 128 elif i== '' or type(i) is IntType or type(i) is FloatType: 129 vals.append(i) 130 else: 131 return '' 132 if vals == '': 133 return '' 134 #print vals, ops 135 expr = cls.doExpression(vals, ops) 136 #print 'expr', expr 137 result = eval(expr) 138 return str(round(result,3))
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Mon Mar 25 10:53:12 2013 | http://epydoc.sourceforge.net |