Last active
April 17, 2022 20:30
-
-
Save V3ntus/59a74598730b89f9342888a7264f5dcf to your computer and use it in GitHub Desktop.
OpenWeather Python data model/serializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OpenWeatherModel(object): | |
__slots__ = "_json", "cod", "message", "base", "weather", "main", "name", "wind", "sys", "cod", "temp", "feels_like", "temp_min", \ | |
"temp_max", "pressure", "humidity", "speed", "deg", "gust", "sunrise", "sunset", "country", "timezone", \ | |
"visibility" | |
def __init__(self, **kwargs): | |
self._json = kwargs | |
for key in kwargs: | |
if key in self.__slots__: | |
if key in ("main", "wind", "sys"): | |
setattr(self, key, kwargs[key]) | |
for _key in kwargs[key]: | |
if _key in self.__slots__: | |
setattr(self, _key, kwargs[key][_key]) | |
elif not isinstance(kwargs[key], list) or not isinstance(kwargs[key], dict): | |
setattr(self, key, kwargs[key]) | |
elif key == "weather": | |
self.weather = kwargs["weather"][0] | |
if hasattr(self, "__slots__"): | |
for _attr in self.__slots__: | |
if not hasattr(self, _attr): | |
setattr(self, _attr, None) | |
if self.sunrise: | |
self.sunrise = time.localtime(self.sunrise) | |
if self.sunset: | |
self.sunset = time.localtime(self.sunset) | |
if self.speed: | |
self.speed = round(self.speed * 2.23, 1) | |
if self.gust: | |
self.gust = round(self.gust * 2.23, 1) | |
# idk what i'm doing here | |
# edit: i know what i'm doing now! | |
# edit 2: nevermind | |
@staticmethod | |
def _f_calc(_c=None, _k=None): | |
if _k: | |
_c = _k - 273.15 | |
return round(_c * (9 / 5) + 32, 1) | |
@property | |
def temp_c(self): | |
return self.temp - 273.15 | |
@property | |
def temp_f(self): | |
return self._f_calc(_c=self.temp_c) | |
@property | |
def feels_like_c(self): | |
return self.feels_like - 273.15 | |
@property | |
def feels_like_f(self): | |
return self._f_calc(_c=self.feels_like_c) | |
@property | |
def temp_min_c(self): | |
return self.temp_min - 273.15 | |
@property | |
def temp_min_f(self): | |
return self._f_calc(_c=self.temp_min_c) | |
@property | |
def temp_max_c(self): | |
return self.temp_max - 273.15 | |
@property | |
def temp_max_f(self): | |
return self._f_calc(_c=self.temp_max_c) | |
@property | |
def deg_alpha(self): | |
if self.deg >= 350 or self.deg <= 10: | |
return "N" | |
elif 281 <= self.deg <= 349: | |
return "NW" | |
elif 261 <= self.deg <= 280: | |
return "W" | |
elif 191 <= self.deg <= 260: | |
return "SW" | |
elif 171 <= self.deg <= 190: | |
return "S" | |
elif 101 <= self.deg <= 170: | |
return "SE" | |
elif 81 <= self.deg <= 100: | |
return "E" | |
elif 11 <= self.deg <= 80: | |
return "NE" | |
else: | |
return None | |
@property | |
def sunset_f(self, fmt: str = "%H:%M:%S"): | |
return time.strftime(fmt, self.sunset) | |
@property | |
def sunrise_f(self, fmt: str = "%H:%M:%S"): | |
return time.strftime(fmt, self.sunrise) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: