Recreate mappings every time

This saves the user a little hassle and is very fast.
This commit is contained in:
Nate Bowman
2026-10-04 02:23:53 -04:00
parent 03ab8d72bc
commit 24f315c107
7 changed files with 28 additions and 65 deletions

View File

@@ -1,6 +1,5 @@
Project structure:
```
create-maps-from-namelist.py -- script for creating maps
data/ -- input files from AoE itself (taken from another project)
decks/ -- python scripts to create individual decks
* actual decks are not stored here
@@ -8,7 +7,6 @@ decks/ -- python scripts to create individual decks
flashcards/ -- all of the useful library code is in here
output/ -- files generated by the project
decks-output/ -- generated decks that can be read into Anki
maps/ -- mappings between names and numbers
namelists/ -- lists containing names to include for particular decks
settings.yaml -- controls where files are read from and written to
```
@@ -24,9 +22,8 @@ Big picture process:
* Create a list of names that you want included in the deck
* These are not just any names -- they need to match what is used in the
data file. Use the process below to help with this.
* Generate mappings for the data file from names to numbers and numbers to
names based on your namelist
* Create a deck using these mappings
* Create a deck using just these names. The library handles the mapping of
names to id numbers for you.
Process:
* Run flashcards/create_full_namelists.py
@@ -43,8 +40,6 @@ Process:
up-to-date or mistake-free
* Copy one of the pruned namelists and delete more entries until you have just
the names you need for a given deck
* Run create-maps-from-namelist.py using your new deck-specific namelist
* This creates two files in output/generated-maps/
* Using one of the scripts in decks/ as a template, write a Python
script that reads your generated maps and the data file and outputs an Anki
deck

View File

@@ -1,21 +0,0 @@
from flashcards.maps import create_maps
if 0:
# namelist you created before
namelist_fname = 'base-units.txt'
# dictionary key into config.data_file
key = 'units_buildings'
# how you want to name the output maps
out_base_fname = 'base-units'
elif 0:
namelist_fname = 'research.txt'
key = 'techs'
out_base_fname = 'research'
else:
namelist_fname = 'buildings.txt'
key = 'units_buildings'
out_base_fname = 'buildings'
create_maps(namelist_fname, key, out_base_fname)

View File

@@ -1,5 +1,5 @@
from flashcards.decks import create_deck
from flashcards.maps import read_data_to_dicts
from flashcards.maps import create_maps, read_data_to_dict
@@ -7,8 +7,9 @@ from flashcards.maps import read_data_to_dicts
deck_name = 'Unit Costs'
deck_fname = 'unit-costs'
# specify the namelist used to create the deck
namelist = 'base-units'
# specify the namelist to be used to create the deck
# give the relative path of the file from config.namelists_dir
namelist = 'base-units.txt'
# key into json file specifying which data you want
# uncomment either 'units_buildings' or 'techs'
@@ -17,12 +18,15 @@ data_json_key = 'units_buildings'
# --- create list of notes ---
# read in data and maps between names and numbers
data, nums, names = read_data_to_dicts(namelist, data_json_key)
# read in data
data = read_data_to_dict(data_json_key)
# create maps between names and numbers based on namelist
names, _ = create_maps(namelist, data_json_key)
# create cards based on data and maps
notes = []
for num, name in nums.items():
for num, name in names.items():
cost = data[num]['cost']
back = '\n'.join([f'{res}: {val}' for res, val in cost.items()
if val != 0])

View File

@@ -1,5 +1,5 @@
from flashcards.decks import create_deck
from flashcards.maps import read_data_to_dicts
from flashcards.maps import create_maps, read_data_to_dict
@@ -7,8 +7,9 @@ from flashcards.maps import read_data_to_dicts
deck_name = 'Research Costs'
deck_fname = 'research-costs'
# specify the namelist used to create the deck
namelist = 'research'
# specify the namelist to be used to create the deck
# give the relative path of the file from config.namelists_dir
namelist = 'research.txt'
# key into json file specifying which data you want
# uncomment either 'units_buildings' or 'techs'
@@ -17,12 +18,15 @@ data_json_key = 'techs'
# --- create list of notes ---
# read in data and maps between names and numbers
data, nums, names = read_data_to_dicts(namelist, data_json_key)
# read in data
data = read_data_to_dict(data_json_key)
# create maps between names and numbers based on namelist
names, _ = create_maps(namelist, data_json_key)
# create cards based on data and maps
notes = []
for num, name in nums.items():
for num, name in names.items():
cost = data[num]['cost']
back = '\n'.join([f'{res}: {val}' for res, val in cost.items()
if val != 0])

View File

@@ -13,5 +13,4 @@ root_path = Path(c['storage']['root'])
config = SimpleNamespace()
config.data_file = root_path / c['storage']['data'] / c['data_file']
config.namelists_dir = root_path / c['storage']['namelists']
config.maps_dir = root_path / c['storage']['maps']
config.decks_dir = root_path / c['storage']['decks']

View File

@@ -6,13 +6,12 @@ from pathlib import Path
def _fill_costs(big, key, whitelist):
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()
costs = sum([v for k, v in v['cost'].items()])
if lname in whitelist and lname not in nums.keys():
names[k] = lname
nums[lname] = k
@@ -20,13 +19,7 @@ def _fill_costs(big, key, whitelist):
return names, nums
def _write_map(fname, mapping):
out_path = config.maps_dir / fname
with open(out_path, 'w', encoding='utf-8') as f:
json.dump(mapping, f, ensure_ascii=False, indent=4)
def create_maps(namelist_fname, key, out_base_fname):
def create_maps(namelist_fname, key):
# Read in the full data dictionary
with open(config.data_file, 'r') as f:
big = json.load(f)
@@ -35,23 +28,13 @@ def create_maps(namelist_fname, key, out_base_fname):
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_costs(big, key, whitelist)
names, nums = _fill_maps(big, key, whitelist)
# Write names -> nums map
_write_map(f'{out_base_fname}-lname-to-num.json', nums)
# Write nums -> names map
_write_map(f'{out_base_fname}-num-to-lname.json', names)
return names, nums
def read_data_to_dicts(basename, key):
def read_data_to_dict(key):
with open(config.data_file, 'r') as f:
all_data = json.load(f)
data = all_data[key]
with open(config.maps_dir / f'{basename}-lname-to-num.json', 'r') as f:
nums = json.load(f)
with open(config.maps_dir / f'{basename}-num-to-lname.json', 'r') as f:
names = json.load(f)
return data, nums, names
return all_data[key]

View File

@@ -1 +0,0 @@
*