def map_table(self, merged_table, mapper): copied_column = [i.clone() for i in merged_table.columns] input_type = namedtuple("input", [c.program_name for c in merged_table.columns]) kidx, mapped_column = mapper[0](copied_column) names = set() for mc in mapped_column: dup = mc.program_name in names if dup: logger.fatal("duplicated column names") assert (mc.program_name not in names) names.add(mc.program_name) output_type = build_record([c.program_name for c in mapped_column]) mapping = self.get_index_mapping(mapped_column, merged_table.columns) mapped_table = Table() mapped_table.key_column = kidx mapped_table.columns = mapped_column tmp_matrix = [] for row in merged_table.matrix: input_row = input_type(*row) output_list = [None if i is None else row[i] for i in mapping] output_row = output_type(output_list) preserve = mapper[1](output_row, input_row) if preserve: #mapped_table.matrix.append(output_row.to_tuple()) tmp_matrix.append(output_row) mapper[2](tmp_matrix) for row in tmp_matrix: mapped_table.matrix.append(row.to_tuple()) return mapped_table
def get_sheet(self, file_name, sheet_name): sig = file_name, sheet_name if sig not in self.sheet_cache: logger.info("sheet %s not in cache") table = Table() table.build_from_sheet(file_name, sheet_name) self.sheet_cache[sig] = table return self.sheet_cache[sig]
self.parts.append(indent) self.parts.append(")") def dump_element(self, e, level): if type(e) in CONTAINER: self.dump_list(e, level) elif type(e) is unicode: self.dump_unicode(e, level) else: self.dump_basic(e, level) def write_file(self): body = "".join(self.parts) if self.module_name is None: print(body) return file_path = self.get_tmp_path(self.module_name) file_dir = os.path.dirname(file_path) if not os.path.exists(file_dir): os.makedirs(file_dir) with open(file_path, "w") as target: target.write(body) if __name__ == "__main__": from excel2data.table import Table t = Table() t.build_from_sheet(u"../test_input/调试.xlsx", u"一些例子") t.key_column = 0 exp = PyExporter(None, t) exp.run()