Some code tweaks and improvements

str-function for weather now more modular
daily weather for first two days is not included anymore
Fixed bug: each daily weather now includes the real temperatures, not the ones from the last day
This commit is contained in:
JuliusFreudenberger 2020-09-19 19:21:26 +02:00
parent e623b317c9
commit f6d0db3f41

View file

@ -38,26 +38,22 @@ class HourlyWeather:
self.description = json_data['weather'][0]['description'] self.description = json_data['weather'][0]['description']
def __str__(self): def __str__(self):
if self.probability_of_precipitation == 0: string = ""
return "*{time}*: {main} | {temperature}°C 🧑: {feels_like}°C" \ string += "*{time}*: {main} | {temperature:.0f}°C 🧑: {feels_like:.0f}°C" \
" 💨: {wind_speed}m/s" \ .format(time=self.time.strftime("%Hh"),
.format(time=self.time.strftime("%Hh"), main=self.main, main=self.main,
temperature=self.temperature, temperature=self.temperature,
feels_like=self.feels_like, feels_like=self.feels_like)
wind_speed=self.wind_speed) if self.probability_of_precipitation != 0:
else: string += " ☔: {pop:.0f}% {rain}mm".format(pop=100 * self.probability_of_precipitation, rain=self.rain)
return "*{time}*: {main} | {temperature}°C 🧑: {feels_like}°C ☔: {pop:.0f}% {rain}mm 💨: {wind_speed}m/s" \
.format(time=self.time.strftime("%Hh"), main=self.main, string += " 💨{wind_speed:.1f}km/h".format(wind_speed=3.6 * self.wind_speed)
temperature=self.temperature, return string
feels_like=self.feels_like,
pop=(100 * self.probability_of_precipitation),
rain=self.rain,
wind_speed=self.wind_speed)
class DailyWeather: class DailyWeather:
time: datetime time: datetime
temp = dict() temp: dict
feels_like: float feels_like: float
wind_speed: float wind_speed: float
probability_of_precipitation: float probability_of_precipitation: float
@ -67,9 +63,7 @@ class DailyWeather:
def __init__(self, json_data: json): def __init__(self, json_data: json):
self.time = datetime.fromtimestamp(json_data['dt']) self.time = datetime.fromtimestamp(json_data['dt'])
self.temp['min'] = json_data['temp']['min'] self.temp = {'min': json_data['temp']['min'], 'day': json_data['temp']['day'], 'max': json_data['temp']['max']}
self.temp['day'] = json_data['temp']['day']
self.temp['max'] = json_data['temp']['max']
self.feels_like = json_data['feels_like']['day'] self.feels_like = json_data['feels_like']['day']
self.wind_speed = json_data['wind_speed'] self.wind_speed = json_data['wind_speed']
self.probability_of_precipitation = json_data['pop'] self.probability_of_precipitation = json_data['pop']
@ -81,28 +75,18 @@ class DailyWeather:
self.description = json_data['weather'][0]['description'] self.description = json_data['weather'][0]['description']
def __str__(self): def __str__(self):
if self.probability_of_precipitation == 0: string = ""
return "*{time}*: {main} | {min} - {day} - {max} 🧑: {feels_like_day}" \ string += "*{time}*: {main} | {min}°C - {day}°C - {max}°C 🧑: {feels_like_day:.0f}°C" \
" 💨: {wind_speed}m/s" \ .format(time=self.time.strftime("%d"),
.format(time=self.time.strftime("%d"), main=self.main,
main=self.main, min=self.temp['min'],
min=self.temp['min'], day=self.temp['day'],
day=self.temp['day'], max=self.temp['max'],
max=self.temp['max'], feels_like_day=self.feels_like)
feels_like_day=self.feels_like, if self.probability_of_precipitation != 0:
wind_speed=self.wind_speed) string += " ☔: {pop:.0f}% {rain}mm".format(pop=100 * self.probability_of_precipitation, rain=self.rain)
else: string += " 💨: {wind_speed:.1f}km/h".format(wind_speed=3.6 * self.wind_speed)
return "*{time}*: {main} | {min} - {day} - {max} 🧑: {feels_like_day}" \ return string
" ☔: {pop:.0f}% {rain}mm 💨: {wind_speed}m/s" \
.format(time=self.time.strftime("%d"),
main=self.main,
min=self.temp['min'],
day=self.temp['day'],
max=self.temp['max'],
feels_like_day=self.feels_like,
pop=(100 * self.probability_of_precipitation),
rain=self.rain,
wind_speed=self.wind_speed)
class TotalWeather: class TotalWeather:
@ -110,7 +94,7 @@ class TotalWeather:
daily_weather = [] daily_weather = []
def __init__(self, json_data: json): def __init__(self, json_data: json):
for single_daily_weather in json_data['daily']: for single_daily_weather in json_data['daily'][2:]:
self.daily_weather.append(DailyWeather(single_daily_weather)) self.daily_weather.append(DailyWeather(single_daily_weather))
for single_hourly_weather in json_data['hourly']: for single_hourly_weather in json_data['hourly']:
self.hourly_weather.append(HourlyWeather(single_hourly_weather)) self.hourly_weather.append(HourlyWeather(single_hourly_weather))
@ -173,11 +157,6 @@ def get_city_by_name(city_name: str) -> City:
return City(cid=city_list[0]['id'], name=city_list[0]['name'], lat=city_list[0]['coord']['lat'], return City(cid=city_list[0]['id'], name=city_list[0]['name'], lat=city_list[0]['coord']['lat'],
lon=city_list[0]['coord']['lon']) lon=city_list[0]['coord']['lon'])
# city_lat = city['coord']['lat']
# city_lon = city['coord']['lon']
#
# print(str(city_lat) + " " + str(city_lon))
def check_city_list(): def check_city_list():
with open("city.list.json", "r") as city_list_file: with open("city.list.json", "r") as city_list_file: