Python / Grade 1 / ООП
Основной класс
import requests
class GetWeather:
x = 'many'
def __init__(self, lat, lon):
self.lat = lat
self.lon = lon
self.json = requests.get(f'https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&'
'hourly=temperature_2m,relativehumidity_2m,apparent_temperature,windspeed_10m').json()
self.timezone = self.json['timezone']
def get_temperature(self):
temperature = self.json['hourly']['temperature_2m'][12]
return temperature
def get_hum(self):
temperature = self.json['hourly']['relativehumidity_2m'][12]
return temperature
def gps(self):
lat = self.json['latitude']
lon = self.json['longitude']
return lat, lon
# наследование
class TempF(GetWeather):
def temp_f(self):
temp_c = self.json['hourly']['temperature_2m'][12]
temp_f = (temp_c * (9 / 5)) + 32
return temp_f
# наследование и полиморфизм
class TempK(TempF):
def get_temperature(self):
temperature = self.json['hourly']['temperature_2m'][12]
return temperature * 0.1
Вызовы
from get_weather import GetWeather, TempF, TempK
weather = GetWeather(60, 60)
weather_data = {
'temp_c': weather.get_temperature(),
'hum': weather.get_hum(),
'gps': weather.gps(),
'timezone': weather.timezone,
}
print(weather_data)
weather = TempF(60, 60)
weather_data = {
'temp_c': weather.get_temperature(),
'temp_f': weather.temp_f(),
'hum': weather.get_hum(),
'gps': weather.gps(),
'timezone': weather.timezone,
}
print(weather_data)
weather = TempK(60, 60)
weather_data = {
'temp_k': weather.get_temperature(),
'temp_f': weather.temp_f(),
'hum': weather.get_hum(),
'gps': weather.gps(),
'timezone': weather.timezone,
}
print(weather_data)