Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- class View(object):
- """
- Mock view class. Simulate web based dashboard with two way user binding.
- Retrieve input from user and pass to controller for data retrieval.
- """
- def __init__(self):
- self.view_controller = Controller()
- def main_view_loop(self):
- try:
- user_input = ""
- print("Enter a city name, zip code, temperature, or type quit to exit.")
- while (user_input != None and isinstance(user_input, str) and user_input != "quit"):
- user_input = input()
- city_temp = self.view_controller.get_weather_for_city(user_input)
- zip_weather = self.view_controller.get_weather_for_zip(user_input)
- temperature_data = self.view_controller.get_cities_by_temp(user_input)
- if not city_temp and not zip_weather and not temperature_data:
- print("No matches found.")
- elif city_temp:
- print("The temperature in {} is {} degrees.".format(user_input, city_temp))
- elif zip_weather:
- print("The temperature in {} is {} degrees.".format(zip_weather.get("city"), zip_weather.get("temp")))
- elif temperature_data:
- print("Cities with current temperature [{}]: {}".format(
- user_input,
- str(temperature_data)
- ))
- except KeyboardInterrupt:
- # quit
- sys.exit()
- class Model(object):
- """
- All I do is store and return data, but this is where you would put external API calls
- or database lookups.
- """
- cities = {
- "charleston": {
- "temp": 70,
- "zip": "29401"
- },
- "nashville": {
- "temp": 65,
- "zip": "37011"
- },
- "knoxville": {
- "temp": 65,
- "zip": "37917"
- }
- }
- def get_cities_data(self):
- return self.cities
- class Controller(object):
- """
- I recieve input from the view, retrieve that data from the model,
- and return it to the view in a friendly and easy to use format.
- """
- def __init__(self):
- self.m = Model()
- def get_weather_for_city(self, city_name):
- city_name = city_name.lower()
- if not city_name in self.m.cities.keys():
- return None
- else:
- return self.m.cities.get(city_name).get("temp")
- def get_weather_for_zip(self, zipcode):
- matching_cities = []
- for k, v in self.m.cities.items():
- if v.get("zip") == zipcode:
- return {
- "city": k,
- "temp": v.get("temp")
- }
- def get_cities_by_temp(self, temperature):
- matching_cities = []
- for k, v in self.m.cities.items():
- if str(v.get("temp")) == str(temperature):
- matching_cities.append(k)
- return matching_cities
- v = View()
- v.main_view_loop()
Advertisement
Add Comment
Please, Sign In to add comment