1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """
23 To perform sort operation.
24
25 Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
26 GNU General Public License.
27 """
28
29 from etl.component import component
30
31 -class sort(component):
32 """
33 This is an ETL Component that performs sort operation.
34
35 Type : Data Component.
36 Computing Performance : Semi-Streamline.
37 Input Flows : 1.
38 * .* : The main data flow with input data.
39 Output Flows : 0-x.
40 * .* : Returns the main flow with sort result.
41 """
42
43 - def __init__(self, fieldname, name='component.transfer.sort'):
44 """
45 Required Parameters
46 fieldname : Specifies the field name according to which sorting process will be done.
47
48 Extra Parameters
49 name : Name of the component.
50 """
51 super(sort, self).__init__(name )
52 self._type = 'component.transfer.sort'
53 self.fieldname = fieldname
54
56 res = sort(self.fieldname, self.name)
57 return res
58
59
61 if self.is_start():
62 self.warning('No any Input attached')
63 if self.is_end():
64 self.warning('No any Output attached')
65 datas = []
66 for channel, trans in self.input_get().items():
67 for iterator in trans:
68 for d in iterator:
69 datas.append(d)
70 datas.sort(lambda x, y: cmp(x[self.fieldname], y[self.fieldname]))
71 for d in datas:
72 yield d, 'main'
73
75 from etl_test import etl_test
76 test = etl_test.etl_component_test(sort('name'))
77 test.check_input({'main': [{'id': 1, 'name': 'OpenERP'}, {'id': 2, 'name': 'Fabien'}]})
78 test.check_output([{'id': 2, 'name': 'Fabien'}, {'id': 1, 'name': 'OpenERP'}], 'main')
79 res = test.output()
80
81 if __name__ == '__main__':
82 test()
83