def get_menus(self): """Return a list of Menu objects in reverse-chronological order. This function checks for new menu data from the API. Obviously, this has no effect if `DEV_MENU_DATA` is True. (See config.py.) """ # Update self._menus. menu_data = fetch.menu_data() for menu_datum in menu_data: self._process_menu(menu_datum) # Return a shallow copy. return self._menus[:]
def __init__(self): """Create a MenuCache. Calls `menu.fetch.menu_data()` to get the unprocessed menu data and then constructs a list of Menu objects from the data, caching the 'menuId' members of the unprocessed data to avoid processing any menu twice. """ menu_data = fetch.menu_data() self._processed = set() # If the menu data happens to be empty, init self._menus and return if len(menu_data) == 0: self._menus = [] return # Reserve space the size of the menu_data, since it can be no larger # than that. self._menus = [None] * len(menu_data) # Process and insert the first menu so we don't get IndexErrors first_menu_datum = menu_data[0] self._processed.add(first_menu_datum['menuId']) self._menus[0] = menu.Menu(first_menu_datum) # Indicates the first unoccupied spot in the list first_open = 1 for m in menu_data[1:]: # Cache processed menu IDs self._processed.add(m['menuId']) new_menu = menu.Menu(m) # If the dates aren't equal, put the new menu at the end if self._menus[first_open - 1].date != new_menu.date: self._menus[first_open] = new_menu first_open += 1 continue # Else, compare the updated_at fields self._store_updated_menu(first_open - 1, new_menu) # Slice out the extra space in this list self._menus = self._menus[:first_open]