def test_merge(merger): tmp_dir = tempfile.mkdtemp() try: file_1 = path.join(tmp_dir, 'test_merger_1.kv') file_2 = path.join(tmp_dir, 'test_merger_2.kv') file_3 = path.join(tmp_dir, 'test_merger_3.kv') merge_file = path.join(tmp_dir, 'merge.kv') generate_keyvi(key_values_1, file_1) generate_keyvi(key_values_2, file_2) generate_keyvi(key_values_3, file_3) merger.Add(file_1) merger.Add(file_2) merger.Add(file_3) merger.Merge(merge_file) merged_dictionary = Dictionary(merge_file) key_values = {} key_values.update(key_values_1) key_values.update(key_values_2) key_values.update(key_values_3) key_values_ordered = collections.OrderedDict(sorted(key_values.items())) for (base_key, base_value), (keyvi_key, keyvi_value) in zip(key_values_ordered.items(), merged_dictionary.GetAllItems()): assert base_key == keyvi_key assert base_value == keyvi_value finally: shutil.rmtree(tmp_dir)
def dump(args): dictionary = Dictionary(args.input_file) with open(args.output_file, 'w') as file_out: for key, value in dictionary.GetAllItems(): if args.json_dumps: key = json.dumps(key) if isinstance(key, bytes): key = key.decode() file_out.write(key) if value: if args.json_dumps: value = json.dumps(value) file_out.write('\t{}'.format(value)) file_out.write('\n')