import pprint
import requests
class ApiWeatherForecast:
def __init__(self):
self._city_cache = {}
def get(self, city):
if city in self._city_cache:
print('Looking for the city in my cache')
return self._city_cache[city]
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid=b833e810d4ab6c298dc3b897f77acc23'
data = requests.get(url).json()
forecast1 = str(data['main']['temp_min'])
forecast2 = data['weather'][0]['main']
forecast3 = data['weather'][0]['description']
forecast = forecast2 + " " + " " + forecast1 + " " + forecast3
print('Sending HTTP request')
self._city_cache[city] = forecast
return forecast
class CityForecast:
def __init__(self, city, weather_forecast=None):
self.city = city
self._weather_forecast = weather_forecast or ApiWeatherForecast()
def get_forecast(self):
return self._weather_forecast.get(self.city)
def _main():
for i in range(5):
city = CityForecast('Moscow')
forecast = city.get_forecast()
pprint.pprint(forecast)
if __name__ == 'main':
_main()
Sending HTTP request
'Clouds 287.15 few clouds'
Sending HTTP request
'Clouds 287.15 few clouds'
Sending HTTP request
'Clouds 287.15 few clouds'
Sending HTTP request
'Clouds 287.15 few clouds'
Sending HTTP request
'Clouds 287.15 few clouds'
Sending HTTP request
'Clouds 287.15 few clouds'
'Clouds 287.15 few clouds'
'Clouds 287.15 few clouds'
'Clouds 287.15 few clouds'
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid=b833e810d4ab6c298dc3b897f77acc23'
data = requests.get(url).json()
forecast1 = str(data['main']['temp_min'])
forecast2 = data['weather'][0]['main']
forecast3 = data['weather'][0]['description']
forecast = forecast2 + " " + " " + forecast1 + " " + forecast3
print('Sending HTTP request')
self._city_cache[city] = forecast
return forecast
import pprint
import requests
class ApiWeatherForecast:
# Кэш
def __init__(self):
self._city_cache = {}
# получить данные из API
def get(self, city):
if city in self._city_cache:
print('Looking for the city in my cache')
return self._city_cache[city]
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid=b833e810d4ab6c298dc3b897f77acc23'
data = requests.get(url).json()
forecast1 = str(data['main']['temp_min'])
forecast2 = data['weather'][0]['main']
forecast3 = data['weather'][0]['description']
forecast = forecast2 + " " + " " + forecast1 + " " + forecast3
print('Sending HTTP request')
self._city_cache[city] = forecast
return forecast
class CityForecast:
def __init__(self, city, weather_forecast=None):
self.city = city
self._weather_forecast = weather_forecast or ApiWeatherForecast()
# Обращаемся к методу класса за ифнормацией
def get_forecast(self):
return self._weather_forecast.get(self.city)
def _main():
# имитация 5 запросов для проверки работы с кэшэм
for i in range(5):
city = CityForecast('Moscow')
forecast = city.get_forecast()
pprint.pprint(forecast)
if __name__ == 'main':
_main()
Sending HTTP request
'Clouds 287.15 few clouds'
Sending HTTP request
'Clouds 287.15 few clouds'
Sending HTTP request
'Clouds 287.15 few clouds'
Sending HTTP request
'Clouds 287.15 few clouds'
Sending HTTP request
'Clouds 287.15 few clouds'
Sending HTTP request
'Clouds 287.15 few clouds'
'Looking for the city in my cache'
'Clouds 287.15 few clouds'
'Looking for the city in my cache'
'Clouds 287.15 few clouds'
'Looking for the city in my cache'
'Clouds 287.15 few clouds'
'Looking for the city in my cache'
url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid=b833e810d4ab6c298dc3b897f77acc23'
data = requests.get(url).json()
forecast1 = str(data['main']['temp_min'])
forecast2 = data['weather'][0]['main']
forecast3 = data['weather'][0]['description']
forecast = forecast2 + " " + " " + forecast1 + " " + forecast3
print('Sending HTTP request')
self._city_cache[city] = forecast
return forecast
def get(self, city):
if city in self._city_cache:
print(1')
return self._city_cache[city]
print('2')
self._city_cache[city] = forecast
return forecast
я понятия не имел, что существуют методы ord и assert
2)
Зато мое решение, в отличие от вашего, работает...
Дальше можно было не писать :D
3)Как я понял из задания, цифры просто так стоять не могут, они должны быть перед скобками или буквой