forked from tomaszswiderski/Spreadsheet-Table-for-ReportLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formula.py
274 lines (221 loc) · 9.37 KB
/
formula.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# -*- coding: utf-8 -*-
__author__ = u'Tomasz Świderski <[email protected]>'
__copyright__ = u'Copyright (c) 2010 Tomasz Świderski'
from decimal import Decimal as D, InvalidOperation as DConversionError
class Formula(object):
"""
Abstract class of all formulas.
"""
def __init__(self, longest_value=None):
# This value will be used to determine cell width during Table's
# self._calc_width.
self._longest_value = longest_value
def get_max_value(self, data, repeat_rows, repeat_rows_b, cell_coord):
"""
Returns largest possible value based on data and coord information.
"""
if self._longest_value is not None:
return self._longest_value
return self._get_max_value(data, repeat_rows, repeat_rows_b,
cell_coord)
def __call__(self, data, repeat_rows, repeat_rows_b, active_rows,
cell_coord):
"""
Return evaluated string value.
"""
raise NotImplementedError
def _get_max_value(self, data, repeat_rows, repeat_rows_b, cell_coord):
"""
Returns largest possible value of Formula.
"""
raise NotImplementedError
class CurrentPageColSum(Formula):
"""
Calculates sum of column from current page only.
"""
def __init__(self, decimal_places=2, longest_value=None,
ignore_convert_errors=True):
Formula.__init__(self, longest_value)
self._ignore_convert_errors = ignore_convert_errors
self._decimal_places = decimal_places
def __call__(self, data, repeat_rows, repeat_rows_b, active_rows,
cell_coord):
"""
Calculates sum of column. Only values inside current active rows will
be considered. All data will be converted to decimals. Unconvertable
values can be ignored or cause to raise Exception.
"""
# Takes slice of data which will be used in evaluation.
cell_row = cell_coord[1]
if active_rows[0] <= cell_row < active_rows[1]:
raise ValueError('Formula inside range to be evaluated!')
active_data = data[active_rows[0]:active_rows[1]]
col_num = cell_coord[0]
sum = D(0)
for row_num, row in enumerate(active_data):
col_value = row[col_num]
if isinstance(col_value, Formula):
col_value = col_value(data, repeat_rows, repeat_rows_b,
active_rows, (col_num, row_num + active_rows[0]))
try:
value = D(col_value)
except DConversionError:
if not self._ignore_convert_errors:
raise
continue
sum += value
format_str = '%%.%df' % self._decimal_places
return format_str % sum
def _get_max_value(self, data, repeat_rows, repeat_rows_b, cell_coord):
"""
Returns largest possible value of Formula.
"""
# Takes slice of data which will be used in evaluation.
cell_row = cell_coord[1]
end_row = len(data) - repeat_rows_b
if repeat_rows <= cell_row < end_row:
raise ValueError('Formula inside range to be evaluated!')
active_data = data[repeat_rows:end_row]
col_num = cell_coord[0]
sum = D(0)
for row_num, row in enumerate(active_data):
col_value = row[col_num]
if isinstance(col_value, Formula):
col_value = col_value(data, repeat_rows, repeat_rows_b,
(repeat_rows, end_row), (col_num, row_num + repeat_rows))
try:
value = D(col_value)
except DConversionError:
if not self._ignore_convert_errors:
raise
continue
sum += value
format_str = '%%.%df' % self._decimal_places
return format_str % sum
class PreviousPagesColSum(Formula):
"""
Calculates sum of column from previous pages.
"""
def __init__(self, starting_value=D(0), decimal_places=2,
ignore_convert_errors=True, longest_value=None):
Formula.__init__(self, longest_value)
self._ignore_convert_errors = ignore_convert_errors
self._starting_value = starting_value
self._decimal_places = decimal_places
def __call__(self, data, repeat_rows, repeat_rows_b, active_rows,
cell_coord):
"""
Calculates sum of column from beginning to start of current page.
All data will be converted to decimals. Unconvertable values
can be ignored or cause to raise Exception.
"""
# Takes slice of data which will be used in evaluation.
cell_row = cell_coord[1]
if repeat_rows <= cell_row < active_rows[0]:
raise ValueError('Formula inside range to be evaluated!')
active_data = data[repeat_rows:active_rows[0]]
col_num = cell_coord[0]
sum = self._starting_value
for row_num, row in enumerate(active_data):
col_value = row[col_num]
if isinstance(col_value, Formula):
col_value = col_value(data, repeat_rows, repeat_rows_b,
active_rows, (col_num, row_num + active_rows[0]))
try:
value = D(col_value)
except DConversionError:
if not self._ignore_convert_errors:
raise
continue
sum += value
format_str = '%%.%df' % self._decimal_places
return format_str % sum
def _get_max_value(self, data, repeat_rows, repeat_rows_b, cell_coord):
"""
Returns largest possible value of Formula.
"""
# Takes slice of data which will be used in evaluation.
cell_row = cell_coord[1]
end_row = len(data) - repeat_rows_b
if repeat_rows <= cell_row < end_row:
raise ValueError('Formula inside range to be evaluated!')
active_data = data[repeat_rows:end_row]
col_num = cell_coord[0]
sum = self._starting_value
for row_num, row in enumerate(active_data):
col_value = row[col_num]
if isinstance(col_value, Formula):
col_value = col_value(data, repeat_rows, repeat_rows_b,
(repeat_rows, end_row), (col_num, row_num + repeat_rows))
try:
value = D(col_value)
except DConversionError:
if not self._ignore_convert_errors:
raise
continue
sum += value
format_str = '%%.%df' % self._decimal_places
return format_str % sum
class TotalPagesColSum(Formula):
"""
Calculates sum of column from previous pages and current page.
"""
def __init__(self, starting_value=D(0), decimal_places=2,
ignore_convert_errors=True, longest_value=None):
Formula.__init__(self, longest_value)
self._prev_pages_col_sum = PreviousPagesColSum(
starting_value = starting_value, decimal_places = decimal_places,
ignore_convert_errors = ignore_convert_errors,
longest_value = longest_value)
self._curr_page_col_sum = CurrentPageColSum(
decimal_places = decimal_places,
ignore_convert_errors = ignore_convert_errors,
longest_value = longest_value)
self._decimal_places = decimal_places
def __call__(self, data, repeat_rows, repeat_rows_b, active_rows,
cell_coord):
"""
Calculates sum of column up to current formula coordinates.
All data will be converted to decimals. Unconvertable values
can be ignored or cause to raise Exception.
"""
prev_value = self._prev_pages_col_sum(data, repeat_rows, repeat_rows_b,
active_rows, cell_coord)
curr_value = self._curr_page_col_sum(data, repeat_rows, repeat_rows_b,
active_rows, cell_coord)
sum = D(prev_value) + D(curr_value)
format_str = '%%.%df' % self._decimal_places
return format_str % sum
def _get_max_value(self, data, repeat_rows, repeat_rows_b, cell_coord):
"""
Returns largest possible value of Formula.
"""
return self._prev_pages_col_sum.get_max_value(data, repeat_rows,
repeat_rows_b, cell_coord)
class RowNumber(Formula):
"""
Returns number of current row starting from beginning of current page.
"""
def __init__(self, starting_value=1, longest_value=None):
Formula.__init__(self, longest_value)
self._starting_value = starting_value
def __call__(self, data, repeat_rows, repeat_rows_b, active_rows,
cell_coord):
"""
Returns row number of formula.
"""
cell_row = cell_coord[1]
if not active_rows[0] <= cell_row < active_rows[1]:
raise ValueError('Formula must be inside visible range!')
return str(cell_row - active_rows[0] + self._starting_value)
def _get_max_value(self, data, repeat_rows, repeat_rows_b, cell_coord):
"""
Returns largest possible value of Formula.
"""
# Takes slice of data which will be used in evaluation.
cell_row = cell_coord[1]
end_row = len(data) - repeat_rows_b
if not repeat_rows <= cell_row < end_row:
raise ValueError('Formula must be inside visible range!')
active_data = data[repeat_rows:end_row]
return str(len(active_data))