In Herculeum I originally had a single class called Configuration that holds configuration for the system and knows how to set it up. It was soon clear that the class would be gigantic in proportions, even when loading of some of the stuff was in other classes. I made little code-pushing today and break off configuration of levels into separate functions in appropriately named modules (configure_catacombs.py and configure_upper_crypt.py):
def init_level(rng, item_generator, creature_generator, level_size):
This function creates configuration for a level (or multiple similarly themed levels).
Calling this identically named functions from different modules started looking like repeating myself. Also, in order to create a new configuration I would have to write the configuration and add call to it in the main configuration class. I wanted to avoid that and added this little piece of code in the original configuration class:
config_names = filter(lambda x: x != '_', dir(pyherc.config.levels)) config_modules = map(lambda x: getattr(pyherc.config.levels, x), config_names) configurators = map(lambda x: getattr(x, 'init_level'), config_modules)
for configurator in configurators: self.extend_configuration(config, configurator(self.rng, self.item_generator, self.creature_generator, self.level_size))
Now I can add new level configurations simply by adding a module with init_level function and placing it in pyherc.config.levels – folder. Building your system to be easily extendable without need to be modified is usually a good thing. Of course one can not anticipate all the changes of future, so some common sense and good judgement is required (as always with software development).