From f6d0db3f41894416f46c81b404044ec4c0a56fea Mon Sep 17 00:00:00 2001 From: JuliusFreudenberger Date: Sat, 19 Sep 2020 19:21:26 +0200 Subject: [PATCH] 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 --- weather_openweathermap.py | 73 ++++++++++++++------------------------- 1 file changed, 26 insertions(+), 47 deletions(-) diff --git a/weather_openweathermap.py b/weather_openweathermap.py index 6e0bfa8..a4bcf9e 100644 --- a/weather_openweathermap.py +++ b/weather_openweathermap.py @@ -38,26 +38,22 @@ class HourlyWeather: self.description = json_data['weather'][0]['description'] def __str__(self): - if self.probability_of_precipitation == 0: - return "*{time}*: {main} | {temperature}°C 🧑: {feels_like}°C" \ - " 💨: {wind_speed}m/s" \ - .format(time=self.time.strftime("%Hh"), main=self.main, - temperature=self.temperature, - feels_like=self.feels_like, - wind_speed=self.wind_speed) - else: - 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, - temperature=self.temperature, - feels_like=self.feels_like, - pop=(100 * self.probability_of_precipitation), - rain=self.rain, - wind_speed=self.wind_speed) + string = "" + string += "*{time}*: {main} | {temperature:.0f}°C 🧑: {feels_like:.0f}°C" \ + .format(time=self.time.strftime("%Hh"), + main=self.main, + temperature=self.temperature, + feels_like=self.feels_like) + if self.probability_of_precipitation != 0: + string += " ☔: {pop:.0f}% {rain}mm".format(pop=100 * self.probability_of_precipitation, rain=self.rain) + + string += " 💨{wind_speed:.1f}km/h".format(wind_speed=3.6 * self.wind_speed) + return string class DailyWeather: time: datetime - temp = dict() + temp: dict feels_like: float wind_speed: float probability_of_precipitation: float @@ -67,9 +63,7 @@ class DailyWeather: def __init__(self, json_data: json): self.time = datetime.fromtimestamp(json_data['dt']) - self.temp['min'] = json_data['temp']['min'] - self.temp['day'] = json_data['temp']['day'] - self.temp['max'] = json_data['temp']['max'] + self.temp = {'min': json_data['temp']['min'], 'day': json_data['temp']['day'], 'max': json_data['temp']['max']} self.feels_like = json_data['feels_like']['day'] self.wind_speed = json_data['wind_speed'] self.probability_of_precipitation = json_data['pop'] @@ -81,28 +75,18 @@ class DailyWeather: self.description = json_data['weather'][0]['description'] def __str__(self): - if self.probability_of_precipitation == 0: - return "*{time}*: {main} | {min} - {day} - {max} 🧑: {feels_like_day}" \ - " 💨: {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, - wind_speed=self.wind_speed) - else: - return "*{time}*: {main} | {min} - {day} - {max} 🧑: {feels_like_day}" \ - " ☔: {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) + string = "" + string += "*{time}*: {main} | {min}°C - {day}°C - {max}°C 🧑: {feels_like_day:.0f}°C" \ + .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) + if self.probability_of_precipitation != 0: + string += " ☔: {pop:.0f}% {rain}mm".format(pop=100 * self.probability_of_precipitation, rain=self.rain) + string += " 💨: {wind_speed:.1f}km/h".format(wind_speed=3.6 * self.wind_speed) + return string class TotalWeather: @@ -110,7 +94,7 @@ class TotalWeather: daily_weather = [] 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)) for single_hourly_weather in json_data['hourly']: 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'], 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(): with open("city.list.json", "r") as city_list_file: