Package etl :: Package component :: Package transform :: Module sort
[hide private]
[frames] | no frames]

Source Code for Module etl.component.transform.sort

 1  # -*- encoding: utf-8 -*- 
 2  ############################################################################## 
 3  # 
 4  #    ETL system- Extract Transfer Load system 
 5  #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved 
 6  #    $Id$ 
 7  # 
 8  #    This program is free software: you can redistribute it and/or modify 
 9  #    it under the terms of the GNU General Public License as published by 
10  #    the Free Software Foundation, either version 3 of the License, or 
11  #    (at your option) any later version. 
12  # 
13  #    This program is distributed in the hope that it will be useful, 
14  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16  #    GNU General Public License for more details. 
17  # 
18  #    You should have received a copy of the GNU General Public License 
19  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
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
55 - def __copy__(self):
56 res = sort(self.fieldname, self.name) 57 return res
58 59 # Read all input channels, sort and write to 'main' channel
60 - def process(self):
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
74 -def test():
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