Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3@file
4@brief Creates custom classes to interpret Python expression as column operations.
5"""
8class ColumnOperator:
10 """
11 defines an operation between two columns
12 """
14 def __init__(self):
15 """
16 initiates the operator
18 @param name name of the column
19 """
20 pass
22 def __str__(self):
23 """
24 usual
25 """
26 raise NotImplementedError()
28 def __call__(self, columns):
29 """
30 returns the results of this operation between a list of columns
31 """
32 raise NotImplementedError()
35class OperatorId(ColumnOperator):
37 """
38 defines a constant
39 """
41 def __str__(self):
42 """
43 usual
44 """
45 return "="
47 def __call__(self, columns):
48 """
49 returns the results of this operation between a list of columns
50 """
51 if len(columns) != 1:
52 raise ValueError(
53 "we expect a single value in a array here: {0}".format(
54 str(columns)))
55 if not isinstance(columns, tuple):
56 raise TypeError("we expect a tuple here")
57 for c in columns:
58 c.IsColumnType()
59 return columns[0]()
62class OperatorMul(ColumnOperator):
64 """
65 defines the multiplication
66 """
68 def __str__(self):
69 """
70 usual
71 """
72 return "*"
74 def __call__(self, columns):
75 """
76 returns the results of this operation between a list of columns
77 """
78 if len(columns) == 0:
79 raise ValueError(
80 "we expect at least a value in a array here: {0}".format(
81 str(columns)))
82 if not isinstance(columns, tuple):
83 raise TypeError("we expect a tuple here")
84 for c in columns:
85 c.IsColumnType()
87 r = columns[0]()
88 for c in columns[1:]:
89 r *= c()
90 return r
93class OperatorAdd(ColumnOperator):
95 """
96 defines the addition
97 """
99 def __str__(self):
100 """
101 usual
102 """
103 return "+"
105 def __call__(self, columns):
106 """
107 returns the results of this operation between a list of columns
108 """
109 if len(columns) == 0:
110 raise ValueError(
111 "we expect at least a value in a array here: {0}".format(
112 str(columns)))
113 if not isinstance(columns, tuple):
114 raise TypeError("we expect a tuple here")
115 for c in columns:
116 c.IsColumnType()
118 r = columns[0]()
119 for c in columns[1:]:
120 r += c()
121 return r
124class OperatorDiv(ColumnOperator):
126 """
127 defines the division
128 """
130 def __str__(self):
131 """
132 usual
133 """
134 return "/"
136 def __call__(self, columns):
137 """
138 returns the results of this operation between a list of columns
139 """
140 if len(columns) != 2:
141 raise ValueError(
142 "we expect two values in a array here: {0}".format(
143 str(columns)))
144 if not isinstance(columns, tuple):
145 raise TypeError("we expect a tuple here")
146 for c in columns:
147 c.IsColumnType()
148 return columns[0]() / columns[1]()
151class OperatorSub(ColumnOperator):
153 """
154 defines the subtraction
155 """
157 def __str__(self):
158 """
159 usual
160 """
161 return "-"
163 def __call__(self, columns):
164 """
165 returns the results of this operation between a list of columns
166 """
167 if len(columns) != 2:
168 raise ValueError(
169 "we expect two values in a array here: {0}".format(
170 str(columns)))
171 if not isinstance(columns, tuple):
172 raise TypeError("we expect a tuple here")
173 for c in columns:
174 c.IsColumnType()
175 return columns[0]() - columns[1]()
178class OperatorPow(ColumnOperator):
180 """
181 defines the power
182 """
184 def __str__(self):
185 """
186 usual
187 """
188 return "**"
190 def __call__(self, columns):
191 """
192 returns the results of this operation between a list of columns
193 """
194 if len(columns) != 2:
195 raise ValueError(
196 "we expect two values in a array here: {0}".format(
197 str(columns)))
198 if not isinstance(columns, tuple):
199 raise TypeError("we expect a tuple here")
200 for c in columns:
201 c.IsColumnType()
203 return columns[0]() ** columns[1]()
206class OperatorMod(ColumnOperator):
208 """
209 defines the operator mod
210 """
212 def __str__(self):
213 """
214 usual
215 """
216 return "%"
218 def __call__(self, columns):
219 """
220 returns the results of this operation between a list of columns
221 """
222 if len(columns) != 2:
223 raise ValueError(
224 "we expect two values in a array here: {0}".format(
225 str(columns)))
226 if not isinstance(columns, tuple):
227 raise TypeError("we expect a tuple here")
228 for c in columns:
229 c.IsColumnType()
231 return columns[0]() % columns[1]()
234class OperatorDivN(ColumnOperator):
236 """
237 defines the division //
238 """
240 def __str__(self):
241 """
242 usual
243 """
244 return "//"
246 def __call__(self, columns):
247 """
248 returns the results of this operation between a list of columns
249 """
250 if len(columns) != 2:
251 raise ValueError(
252 "we expect two values in a array here: {0}".format(
253 str(columns)))
254 if not isinstance(columns, tuple):
255 raise TypeError("we expect a tuple here")
256 for c in columns:
257 c.IsColumnType()
259 return columns[0]() // columns[1]()
262class OperatorEq(ColumnOperator):
264 """
265 defines ==
266 """
268 def __str__(self):
269 """
270 usual
271 """
272 return "=="
274 def __call__(self, columns):
275 """
276 returns the results of this operation between a list of columns
277 """
278 if len(columns) != 2:
279 raise ValueError(
280 "we expect two values in a array here: {0}".format(
281 str(columns)))
282 if not isinstance(columns, tuple):
283 raise TypeError("we expect a tuple here")
284 for c in columns:
285 c.IsColumnType()
286 return columns[0]() == columns[1]()
289class OperatorNe(ColumnOperator):
291 """
292 defines !=
293 """
295 def __str__(self):
296 """
297 usual
298 """
299 return "!="
301 def __call__(self, columns):
302 """
303 returns the results of this operation between a list of columns
304 """
305 if len(columns) != 2:
306 raise ValueError(
307 "we expect two values in a array here: {0}".format(
308 str(columns)))
309 if not isinstance(columns, tuple):
310 raise TypeError("we expect a tuple here")
311 for c in columns:
312 c.IsColumnType()
313 return columns[0]() != columns[1]()
316class OperatorLt(ColumnOperator):
318 """
319 defines <
320 """
322 def __str__(self):
323 """
324 usual
325 """
326 return "<"
328 def __call__(self, columns):
329 """
330 returns the results of this operation between a list of columns
331 """
332 if len(columns) != 2:
333 raise ValueError(
334 "we expect two values in a array here: {0}".format(
335 str(columns)))
336 if not isinstance(columns, tuple):
337 raise TypeError("we expect a tuple here")
338 for c in columns:
339 c.IsColumnType()
340 return columns[0]() < columns[1]()
343class OperatorGt(ColumnOperator):
345 """
346 defines >
347 """
349 def __str__(self):
350 """
351 usual
352 """
353 return ">"
355 def __call__(self, columns):
356 """
357 returns the results of this operation between a list of columns
358 """
359 if len(columns) != 2:
360 raise ValueError(
361 "we expect two values in a array here: {0}".format(
362 str(columns)))
363 if not isinstance(columns, tuple):
364 raise TypeError("we expect a tuple here")
365 for c in columns:
366 c.IsColumnType()
367 return columns[0]() > columns[1]()
370class OperatorLe(ColumnOperator):
372 """
373 defines <=
374 """
376 def __str__(self):
377 """
378 usual
379 """
380 return "<="
382 def __call__(self, columns):
383 """
384 returns the results of this operation between a list of columns
385 """
386 if len(columns) != 2:
387 raise ValueError(
388 "we expect two values in a array here: {0}".format(
389 str(columns)))
390 if not isinstance(columns, tuple):
391 raise TypeError("we expect a tuple here")
392 for c in columns:
393 c.IsColumnType()
394 return columns[0]() <= columns[1]()
397class OperatorGe(ColumnOperator):
399 """
400 defines >=
401 """
403 def __str__(self):
404 """
405 usual
406 """
407 return ">="
409 def __call__(self, columns):
410 """
411 returns the results of this operation between a list of columns
412 """
413 if len(columns) != 2:
414 raise ValueError(
415 "we expect two values in a array here: {0}".format(
416 str(columns)))
417 if not isinstance(columns, tuple):
418 raise TypeError("we expect a tuple here")
419 for c in columns:
420 c.IsColumnType()
421 return columns[0]() >= columns[1]()
424class OperatorOr(ColumnOperator):
426 """
427 defines ``or``
428 """
430 def __str__(self):
431 """
432 usual
433 """
434 return "or"
436 def __call__(self, columns):
437 """
438 returns the results of this operation between a list of columns
439 """
440 if len(columns) != 2:
441 raise ValueError(
442 "we expect two values in a array here: {0}".format(
443 str(columns)))
444 if not isinstance(columns, tuple):
445 raise TypeError("we expect a tuple here")
446 for c in columns:
447 c.IsColumnType()
448 return columns[0]() or columns[1]()
451class OperatorAnd(ColumnOperator):
453 """
454 defines ``and``
455 """
457 def __str__(self):
458 """
459 usual
460 """
461 return "and"
463 def __call__(self, columns):
464 """
465 returns the results of this operation between a list of columns
466 """
467 if len(columns) != 2:
468 raise ValueError(
469 "we expect two values in a array here: {0}".format(
470 str(columns)))
471 if not isinstance(columns, tuple):
472 raise TypeError("we expect a tuple here")
473 for c in columns:
474 c.IsColumnType()
475 return columns[0]() and columns[1]()
478class OperatorNot(ColumnOperator):
480 """
481 defines ``not``
482 """
484 def __str__(self):
485 """
486 usual
487 """
488 return "not"
490 def __call__(self, columns):
491 """
492 returns the results of this operation between a list of columns
493 """
494 if len(columns) != 1:
495 raise ValueError(
496 "we expect one value in a array here: {0}".format(
497 str(columns)))
498 if not isinstance(columns, tuple):
499 raise TypeError("we expect a tuple here")
500 for c in columns:
501 c.IsColumnType()
502 return not columns[0]()
505class OperatorFunc(ColumnOperator):
507 """
508 defines a function call
509 """
511 def __init__(self, func):
512 """
513 constructor
514 """
515 self._func = func
517 def __str__(self):
518 """
519 usual
520 """
521 return "func"
523 def __call__(self, columns):
524 """
525 returns the results of this operation between a list of columns
526 """
527 if not isinstance(columns, tuple):
528 raise TypeError("we expect a tuple here")
529 for c in columns:
530 c.IsColumnType()
531 return self._func(* [c() for c in columns])