Project

General

Profile

« Previous | Next » 

Revision 1179

bin/map: DB, CSV inputs: Use column indexes instead of column names to look up each field (optimization to avoid repeated dict lookups of the same key)

View differences:

bin/map
160 160
            in_pkeys = {}
161 161
            cur = sql.select(in_db, table=in_root, fields=None, conds=None,
162 162
                limit=end, start=0)
163
            col_names = list(sql.col_names(cur))
164
            col_idxs = util.list_flip(col_names)
163 165
            
166
            mappings_new = []
167
            for i, mapping in enumerate(mappings):
168
                in_, out = mapping
169
                if metadata_value(in_) == None:
170
                    try: mapping = (col_idxs[in_], out)
171
                    except KeyError: continue
172
                mappings_new.append(mapping)
173
            mappings = mappings_new
174
            
164 175
            def get_value(in_, row):
165
                try: return util.cast(str, row[in_])
166
                except KeyError: return None
167
            
168
            col_names = list(sql.col_names(cur))
169
            col_idxs = util.list_flip(col_names)
176
                try: return util.cast(str, row.list[in_])
177
                except IndexError: return None
170 178
            def wrap_row(row): return util.ListDict(row, col_names, col_idxs)
171 179
            row_ct = map_rows(get_value, util.WrapIter(wrap_row, sql.rows(cur)))
172 180
            
......
183 191
        else: # input is CSV
184 192
            map_ = dict(mappings)
185 193
            reader = csv.reader(sys.stdin)
194
            col_names = reader.next()
195
            col_idxs = util.list_flip(col_names)
186 196
            
197
            mappings_new = []
198
            for i, mapping in enumerate(mappings):
199
                in_, out = mapping
200
                if metadata_value(in_) == None:
201
                    try: mapping = (col_idxs[in_], out)
202
                    except KeyError: continue
203
                mappings_new.append(mapping)
204
            mappings = mappings_new
205
            
187 206
            def get_value(in_, row):
188
                try: return util.none_if(row[in_], '')
189
                except KeyError: return None
190
            
191
            col_names = reader.next()
192
            col_idxs = util.list_flip(col_names)
207
                try: return util.none_if(row.list[in_], '')
208
                except IndexError: return None
193 209
            def wrap_row(row): return util.ListDict(row, col_names, col_idxs)
194 210
            row_ct = map_rows(get_value, util.WrapIter(wrap_row, reader))
195 211
        

Also available in: Unified diff