Guest User

Untitled

a guest
Dec 29th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. class View(object):
  5.     """
  6.        Mock view class. Simulate web based dashboard with two way user binding.
  7.        Retrieve input from user and pass to controller for data retrieval.
  8.    """
  9.     def __init__(self):
  10.         self.view_controller = Controller()
  11.        
  12.     def main_view_loop(self):
  13.         try:
  14.             user_input = ""
  15.             print("Enter a city name, zip code, temperature, or type quit to exit.")
  16.             while (user_input != None and isinstance(user_input, str) and user_input != "quit"):
  17.                 user_input = input()
  18.                
  19.                 city_temp = self.view_controller.get_weather_for_city(user_input)
  20.                 zip_weather = self.view_controller.get_weather_for_zip(user_input)
  21.                 temperature_data = self.view_controller.get_cities_by_temp(user_input)
  22.                
  23.                 if not city_temp and not zip_weather and not temperature_data:
  24.                     print("No matches found.")
  25.                 elif city_temp:
  26.                     print("The temperature in {} is {} degrees.".format(user_input, city_temp))
  27.                 elif zip_weather:
  28.                     print("The temperature in {} is {} degrees.".format(zip_weather.get("city"), zip_weather.get("temp")))
  29.                 elif temperature_data:
  30.                     print("Cities with current temperature [{}]: {}".format(
  31.                         user_input,
  32.                         str(temperature_data)
  33.                     ))
  34.         except KeyboardInterrupt:
  35.             # quit
  36.             sys.exit()
  37.  
  38.  
  39. class Model(object):
  40.     """
  41.        All I do is store and return data, but this is where you would put external API calls
  42.        or database lookups.
  43.    """
  44.     cities = {
  45.         "charleston": {
  46.             "temp": 70,
  47.             "zip": "29401"
  48.         },
  49.         "nashville": {
  50.             "temp": 65,
  51.             "zip": "37011"
  52.         },
  53.         "knoxville": {
  54.             "temp": 65,
  55.             "zip": "37917"
  56.         }
  57.     }
  58.    
  59.  
  60.        
  61.     def get_cities_data(self):
  62.         return self.cities
  63.    
  64.  
  65. class Controller(object):
  66.     """
  67.        I recieve input from the view, retrieve that data from the model,
  68.        and return it to the view in a friendly and easy to use format.
  69.    """
  70.    
  71.     def __init__(self):
  72.         self.m = Model()
  73.    
  74.     def get_weather_for_city(self, city_name):
  75.         city_name = city_name.lower()
  76.        
  77.         if not city_name in self.m.cities.keys():
  78.             return None
  79.         else:
  80.             return self.m.cities.get(city_name).get("temp")
  81.    
  82.     def get_weather_for_zip(self, zipcode):
  83.         matching_cities = []
  84.         for k, v in self.m.cities.items():
  85.             if v.get("zip") == zipcode:
  86.                 return {
  87.                     "city": k,
  88.                     "temp": v.get("temp")
  89.                 }
  90.  
  91.     def get_cities_by_temp(self, temperature):
  92.         matching_cities = []
  93.         for k, v in self.m.cities.items():
  94.             if str(v.get("temp")) == str(temperature):
  95.                 matching_cities.append(k)
  96.         return matching_cities
  97.        
  98.  
  99.  
  100.  
  101.  
  102. v = View()
  103. v.main_view_loop()
Advertisement
Add Comment
Please, Sign In to add comment