Skip to content

Instantly share code, notes, and snippets.

@inesp
Last active September 2, 2019 13:17
Show Gist options
  • Save inesp/b8cad08338ae9690ffe266c31c16d30d to your computer and use it in GitHub Desktop.
Save inesp/b8cad08338ae9690ffe266c31c16d30d to your computer and use it in GitHub Desktop.
The ZOO of configs
# ----------- File zoos_config_builder.py -----------------
from functools import partial
class ZOOsConfigBuilder:
...
def get_config(self):
return {
"paris": {
"is_open_to_public": True,
"get_config": self:_build_paris_config(["girrafe", "lion", "ape"]),
},
"vienna": {
"is_open_to_public": True,
"get_config": self._build_vienna_config(["elephant", "tucan"]),
},
....
}
def _build_paris_config(animal_types):
def _get_config(owner, zoo_size):
animals = create_animals(animal_types):
return {
"owner": owner,
"zoo_size": zoo_size,
"animals": animals,
"most_popular_animal": "giraffe",
}
return _get_config
def _build_vienna_config(animal_types):
def _get_config(owner, zoo_size):
animals = create_animals(animal_types + ["pelican"]):
return {
"owner": owner,
"zoo_size": zoo_size,
"animals": animals,
"free_entrance_day": "friday",
}
return _get_config
# ---------- city.py ----------------------
class City:
def __init__(self, name):
self.name = "vienna"
...
def create_zoo(self, owner):
zoo_configs = ZOOsConfigBuilder().get_config()
config = zoo_configs.get(self.name)
if not config or not config.get("is_open_to_public"):
return None
return Zoo(config["get_config"](owner, 130))
# ---------- zoo.py ----------------------
class Zoo:
def __init__(zoo_config):
self.owner_name = zoo_config["owner"]
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment