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"""
7from .column_operator import ColumnOperator
8from .others_types import NA
11class ColumnGroupOperator(ColumnOperator):
12 """
13 Defines an operation between two columns.
14 """
16 def __init__(self):
17 """
18 Initiates the operator.
20 @param name name of the column
21 """
22 pass
24 def __str__(self):
25 """
26 usual
27 """
28 raise NotImplementedError()
30 def __call__(self, columns):
31 """
32 returns the results of this operation between a list of columns
33 """
34 raise NotImplementedError()
37class OperatorGroupLen(ColumnGroupOperator):
39 """
40 defines the group function ``len``
41 """
43 def __str__(self):
44 """
45 usual
46 """
47 return "len"
49 def __call__(self, columns):
50 """
51 returns the results of this operation between a list of columns
52 """
53 if not hasattr(columns, '__iter__'):
54 raise TypeError(
55 "we expect an iterator here not " + str(type(columns)))
56 return len(columns)
59class OperatorGroupAvg(ColumnGroupOperator):
61 """
62 defines the group function ``avg``, the default value when the set is empty is None
63 """
65 def __str__(self):
66 """
67 usual
68 """
69 return "avg"
71 def __call__(self, columns):
72 """
73 returns the results of this operation between a list of columns,
74 it returns @see cl NA for a null set
75 """
76 if not hasattr(columns, '__iter__'):
77 raise TypeError(
78 "we expect an iterator here not " + str(type(columns)))
80 # we walk through the set only once
81 nb = 0
82 for val in columns:
83 if nb == 0:
84 s = val
85 else:
86 s += val
87 nb += 1
89 if nb == 0:
90 return NA
91 else:
92 return s / nb