Skip to content
Advertisement

Wrapper to Objects

This is a software design question. I have a folder like this:

api.py
calendar/
  __init__.py
  asia/
  europe/
    __init.py
    germany.py
    italy.py
  america/
  ...
    

Each of the countries has its own calendar, like so:

## germany.py
from some_other_library import get_calendar

munich_params = {...}
munich_calendar = get_calendar("DE", 
                               start_time='2020-01-01', 
                               end_time='2020-05-01', 
                               params=munich_params)
berlin_params = {...}
berlin_calendar = get_calendar("DE",
                               start_time='2020-01-01', 
                               end_time='2020-05-01', 
                               params=berlin_params)
## italy.py
from some_other_library import get_calendar

milan_params = {...}
milan_calendar = get_calendar("IT", 
                              start_time='2020-01-01', 
                              end_time='2020-05-01', 
                              params=milan_params)
rome_params = {...}
rome_calendar = get_calendar("IT",
                             start_time='2020-01-01', 
                             end_time='2020-05-01', 
                             params=rome_params)

palermo_params = {...}
palermo_calendar = get_calendar("IT",
                                start_time='2020-01-01', 
                                end_time='2020-05-01', 
                                params=palermo_params)

What I want is a wrapper to all of these countries’ calendars. In short, something like

## api.py

def get_calendar():
    code code code
    pass

#######################

> import api
> calendar = api.get_calendar("munich")

I don’t want to hard code this. I just want to load all the calendars from the calendar folder into some wrapper. Maybe an enum class instead of a function? Not sure.

If no answer, any thoughts or resources where I can look for this?

Advertisement

Answer

You are looking for the Factory Pattern

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement