From 117ccc69b0c6d8dd78249575fc636686d750a960 Mon Sep 17 00:00:00 2001 From: "Yi Yang @ Anteros" Date: Fri, 27 Oct 2023 02:41:47 +0800 Subject: [PATCH] (Aegis) finish the parametr refactor --- industry/aegis_gen.py | 8 ++++---- industry/docgen/economy.py | 10 ++++------ industry/lib/economy/__init__.py | 4 ++-- industry/lib/parameters.py | 31 +++++++++++++------------------ 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/industry/aegis_gen.py b/industry/aegis_gen.py index 0b995850..051211a3 100644 --- a/industry/aegis_gen.py +++ b/industry/aegis_gen.py @@ -3,7 +3,7 @@ import struct import argparse from industry.economies import vanilla_temperate, vanilla_subarctic, vanilla_subtropical, firs_temperate, firs_arctic -from industry.lib.parameters import iterate_variations +from industry.lib.parameters import parameter_choices from industry.lib.validator import validate @@ -17,7 +17,7 @@ def initialize_metadata(): # Make up the lists for meta_economy in all_economies: - for variation in iterate_variations(): + for variation in parameter_choices.iterate_variations(): economy = meta_economy.get_economy(variation) for industry in economy.industries: if industry not in all_industries: @@ -32,7 +32,7 @@ def initialize_metadata(): industry._props["acceptance_types"] = SplitDefinition((0,), {}) for i, meta_economy in enumerate(all_economies): - for variation in iterate_variations(): + for variation in parameter_choices.iterate_variations(): economy = meta_economy.get_economy(variation) for industry in all_industries: if industry in economy.industries: @@ -101,7 +101,7 @@ def main(): gen() elif args.cmd == "test": for meta_economy in all_economies: - for variation in iterate_variations(): + for variation in parameter_choices.iterate_variations(): economy = meta_economy.get_economy(variation) try: validate(economy) diff --git a/industry/docgen/economy.py b/industry/docgen/economy.py index b8a00c50..33deb6dd 100644 --- a/industry/docgen/economy.py +++ b/industry/docgen/economy.py @@ -3,19 +3,17 @@ from industry.lib.parameters import ( docs_parameter_choices, parameter_choices, - iterate_variations, - parameter_desc, PRESETS, ) -default_variation = "0" * len(parameter_choices) +default_variation = "0" * len(parameter_choices.choices) def gen_economy_doc(all_economies, string_manager): prefix = "docs/industry/economies" for i, meta_economy in enumerate(all_economies): - for variation in iterate_variations(parameter_choices=docs_parameter_choices): + for variation in docs_parameter_choices.iterate_variations(): economy = meta_economy.get_economy(variation) variation_desc = economy.parameter_desc if variation_desc == default_variation: @@ -75,7 +73,7 @@ def gen_economy_doc(all_economies, string_manager): choices_text = [] for preset, preset_params in PRESETS.items(): - preset_desc = parameter_desc(preset_params) + preset_desc = parameter_choices.desc(preset_params) if preset_desc == variation_desc: choices_text.append(f"{preset}") else: @@ -88,7 +86,7 @@ def gen_economy_doc(all_economies, string_manager): file=f, ) - for i, (param, choices) in enumerate(docs_parameter_choices): + for i, (param, choices) in enumerate(docs_parameter_choices.choices): if len(choices) == 1: continue choices_text = [] diff --git a/industry/lib/economy/__init__.py b/industry/lib/economy/__init__.py index 55946092..cc2feeee 100644 --- a/industry/lib/economy/__init__.py +++ b/industry/lib/economy/__init__.py @@ -105,6 +105,6 @@ def cargos(self): @property def parameter_desc(self): - from industry.lib.parameters import parameter_desc + from industry.lib.parameters import parameter_choices - return parameter_desc(self.parameters) + return parameter_choices.desc(self.parameters) diff --git a/industry/lib/parameters.py b/industry/lib/parameters.py index 8f35a8a8..257fa2ef 100644 --- a/industry/lib/parameters.py +++ b/industry/lib/parameters.py @@ -192,6 +192,19 @@ def fix_docs_params(self, cat, options): assert all(o in all_options for o in options) self.choices[idx] = (cat, options) + def iterate_variations(self, i=0, params={}): + if i == len(self.choices): + yield params + else: + for j in self.choices[i][1]: + new_params = params.copy() + new_params[self.choices[i][0]] = j + for variation in self.iterate_variations(i + 1, new_params): + yield variation + + def desc(self, params): + return "".join(str(options.index(params[i])) for i, options in self.choices) + parameter_choices = SearchSpace( [ @@ -224,9 +237,6 @@ def fix_docs_params(self, cat, options): docs_parameter_choices.fix_docs_params("TOWN_GOODS", ["ORGANIC"]) -parameter_choices = parameter_choices.choices -docs_parameter_choices = docs_parameter_choices.choices - PRESETS = { "VANILLA": { "POLICY": "AUTARKY", @@ -264,18 +274,3 @@ def fix_docs_params(self, cat, options): "TOWN_GOODS": "ORGANIC", }, } - - -def iterate_variations(i=0, params={}, parameter_choices=parameter_choices): - if i == len(parameter_choices): - yield params - else: - for j in parameter_choices[i][1]: - new_params = params.copy() - new_params[parameter_choices[i][0]] = j - for variation in iterate_variations(i + 1, new_params, parameter_choices=parameter_choices): - yield variation - - -def parameter_desc(params): - return "".join(str(options.index(params[i])) for i, options in parameter_choices)