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

Source Code for Module etl.component.transform.join

 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  Data Join component. 
24  """ 
25   
26  from etl.component import component 
27  from etl.component.transform import map 
28  import datetime 
29  from etl import tools 
30   
31 -class join(map):
32 """ 33 Data Join component. 34 """ 35
36 - def __init__(self, map_criteria, join_keys={}, name='component.transfer.join', transformer=None, row_limit=0):
37 38 """ 39 Required Parameters 40 map_criteria : Mapping criteria. 41 42 Extra Parameters 43 name : Name of the component. 44 join_keys : 45 transformer : Transformer object to transform string data into particular object. 46 """ 47 super(map, self).__init__(name=name, transformer=transformer, row_limit=0) 48 self._type = 'component.transfer.join' 49 self.map_criteria = map_criteria 50 self.join_keys = join_keys 51 52 def preprocess(self, channels): 53 res = {} 54 for chn in join_keys: 55 cdict = {} 56 for iterator in channels[chn]: 57 for d in iterator: 58 cdict[d[join_keys[chn]]] = d 59 res[chn] = cdict 60 return res
61 self.preprocess = preprocess
62
63 - def __copy__(self):
64 res = join(self.map_criteria, self.join_keys, self.name, self.transformer, self.row_limit) 65 return res
66
67 - def process(self):
68 # to be chak process not defined 69 pass
70 71
72 -def test():
73 from etl_test import etl_test 74 import etl 75 input_part = [ 76 {'id': 1, 'name': 'Fabien', 'country_id': 3}, 77 {'id': 2, 'name': 'Luc', 'country_id': 3}, 78 {'id': 3, 'name': 'Henry', 'country_id': 1} 79 ] 80 input_cty = [{'id': 1, 'name': 'Belgium'}, {'id': 3, 'name': 'France'}] 81 map_keys = {'main': { 82 'id': "main['id']", 83 'name': "main['id'].upper()", 84 'country': "country[main['country_id']]['name']" 85 }} 86 join_keys = { 87 'country': 'id' 88 } 89 test = etl_test.etl_component_test(join(map_keys, join_keys)) 90 test.check_input({'partner':input_part, 'countries': input_cty}) 91 print test.output()
92 93 if __name__ == '__main__': 94 test() 95