# Converts a list of lnames to two maps: # lname -> num and num -> lname from flashcards.config import config import json from pathlib import Path def _fill_maps(big, key, whitelist): names = {} # num -> localized name nums = {} # localized name -> num for k, v in big[key].items(): lname = v['localised_name'].strip() if lname in whitelist and lname not in nums.keys(): names[k] = lname nums[lname] = k return names, nums def create_maps(namelist_fname, key): # Read in the full data dictionary with open(config.data_file, 'r') as f: big = json.load(f) # Use whitelist to filter data and map only as needed namelist_path = config.namelists_dir / namelist_fname with open(namelist_path, 'r') as f: whitelist = [s.strip() for s in f.readlines()] names, nums = _fill_maps(big, key, whitelist) return names, nums def read_data_to_dict(key): with open(config.data_file, 'r') as f: all_data = json.load(f) return all_data[key]