| 1 | from datetime import datetime |
| 2 | from enum import Enum, auto |
| 3 | |
| 4 | |
| 5 | class AutoName(Enum): |
| 6 | def _generate_next_value_(name, start, count, last_values): |
| 7 | return name.lower() |
| 8 | |
| 9 | |
| 10 | class Condition(AutoName): |
| 11 | fog = auto() |
| 12 | hazemoon = auto() |
| 13 | hazesun = auto() |
| 14 | lightning = auto() |
| 15 | mist = auto() |
| 16 | moon = auto() |
| 17 | mooncloud = auto() |
| 18 | rain = auto() |
| 19 | sun = auto() |
| 20 | suncloud = auto() |
| 21 | storm = auto() |
| 22 | |
| 23 | |
| 24 | class Weather: |
| 25 | """ |
| 26 | A class encapsulating raw weather data. |
| 27 | |
| 28 | :param reference_time: Timestamp of weather measurement |
| 29 | :type reference_time: `datetime.datetime` |
| 30 | :param condition: Single-word weather condition |
| 31 | :type condition: `meteoclimatic.Condition` or `str` |
| 32 | :param temp_current: Current temperature in Celsius |
| 33 | :type temp_current: `float` |
| 34 | :param temp_max: Maximum temperature in Celsius for the past 24 hours |
| 35 | :type temp_max: `float` |
| 36 | :param temp_min: Minimum temperature in Celsius for the past 24 hours |
| 37 | :type temp_min: `float` |
| 38 | :param humidity_current: Current humidity in percentage points |
| 39 | :type humidity_current: `float` |
| 40 | :param humidity_max: Maximum humidity in percentage points for the past 24 hours |
| 41 | :type humidity_max: `float` |
| 42 | :param humidity_min: Minimum humidity in percentage points for the past 24 hours |
| 43 | :type humidity_min: `float` |
| 44 | :param pressure_current: Current atmospheric pressure in hPa units |
| 45 | :type pressure_current: `float` |
| 46 | :param pressure_max: Maximum atmospheric pressure in hPa units for the past 24 hours |
| 47 | :type pressure_max: `float` |
| 48 | :param pressure_min: Minimum atmospheric pressure in hPa units for the past 24 hours |
| 49 | :type pressure_min: `float` |
| 50 | :param wind_current: Current wind speed in km/h units |
| 51 | :type wind_current: `float` |
| 52 | :param wind_max: Maximum wind speed in km/h units for the past 24 hours |
| 53 | :type wind_max: `float` |
| 54 | :param wind_bearing: Wind bearing in degree units |
| 55 | :type wind_bearing: `float` |
| 56 | :param rain: Precipitation in mm units for the past 24 hours |
| 57 | :type rain: `float` |
| 58 | :returns: a *Weather* instance |
| 59 | :raises: *ValueError* when invalid values are provided for non-negative or ranged |
| 60 | quantities |
| 61 | """ |
| 62 | |
| 63 | def __init__(self, reference_time: datetime, condition: Condition, |
| 64 | temp_current: float, temp_max: float, temp_min: float, |
| 65 | humidity_current: float, humidity_max: float, humidity_min: float, |
| 66 | pressure_current: float, pressure_max: float, pressure_min: float, |
| 67 | wind_current: float, wind_max: float, wind_bearing: float, |
| 68 | rain: float): |
| 69 | """Initialize the class.""" |
| 70 | if not isinstance(reference_time, datetime): |
| 71 | raise ValueError( |
| 72 | "reference_time is not an instance of datetime.datetime") |
| 73 | self.reference_time = reference_time |
| 74 | |
| 75 | self.condition = condition |
| 76 | |
| 77 | self.temp_current = temp_current |
| 78 | self.temp_max = temp_max |
| 79 | self.temp_min = temp_min |
| 80 | |
| 81 | for humidity in [humidity_current, humidity_max, humidity_min]: |
| 82 | if humidity is not None and (humidity < 0.0 or humidity > 100.0): |
| 83 | raise ValueError("humidity must be between 0 and 100") |
| 84 | self.humidity_current = humidity_current |
| 85 | self.humidity_max = humidity_max |
| 86 | self.humidity_min = humidity_min |
| 87 | |
| 88 | self.pressure_current = pressure_current |
| 89 | self.pressure_max = pressure_max |
| 90 | self.pressure_min = pressure_min |
| 91 | |
| 92 | for wind in [wind_current, wind_max]: |
| 93 | if wind is not None and wind < 0.0: |
| 94 | raise ValueError("wind must be greatear than 0") |
| 95 | self.wind_current = wind_current |
| 96 | self.wind_max = wind_max |
| 97 | |
| 98 | if wind_bearing is not None and (wind_bearing < 0.0 or wind_bearing > 360.0): |
| 99 | raise ValueError("wind bearing must be between 0 and 360") |
| 100 | self.wind_bearing = wind_bearing |
| 101 | |
| 102 | if rain is not None and rain < 0.0: |
| 103 | raise ValueError("rain must be greatear than 0") |
| 104 | self.rain = rain |
| 105 | |
| 106 | def __eq__(self, other): |
| 107 | if not isinstance(other, Weather): |
| 108 | return NotImplemented |
| 109 | prop_names = list(self.__dict__) |
| 110 | for prop in prop_names: |
| 111 | if self.__dict__[prop] != other.__dict__[prop]: |
| 112 | return False |
| 113 | return True |
| 114 | |
| 115 | def __repr__(self): |
| 116 | return "%s(%r)" % (self.__class__, self.__dict__) |