From 5e2bf56c98395e4c13b5271fd96c7a3da5153191 Mon Sep 17 00:00:00 2001 From: Kira Date: Fri, 26 Jan 2024 14:18:06 +0800 Subject: [PATCH] use `StrEnum` and `IntEnum` instead of `Enum` we change from `Enum` to `StrEnum` and `IntEnum because it extend the enum functionality. Also we add example docstring to enum classes. Lastly we update the `get_weather_forecast` to use the new `Province` class. --- src/bmkg/weather_forecast/enums/cardinal.py | 20 +++++++++++++++---- src/bmkg/weather_forecast/enums/province.py | 20 +++++++++++++++---- src/bmkg/weather_forecast/enums/sexa.py | 20 +++++++++++++++---- src/bmkg/weather_forecast/enums/type.py | 20 +++++++++++++++---- src/bmkg/weather_forecast/enums/weather.py | 20 +++++++++++++++---- src/bmkg/weather_forecast/weather_forecast.py | 2 +- 6 files changed, 81 insertions(+), 21 deletions(-) diff --git a/src/bmkg/weather_forecast/enums/cardinal.py b/src/bmkg/weather_forecast/enums/cardinal.py index 04329ea..0721813 100644 --- a/src/bmkg/weather_forecast/enums/cardinal.py +++ b/src/bmkg/weather_forecast/enums/cardinal.py @@ -1,14 +1,14 @@ """ -Module to store an enum class representation cardinal directions. +Module to store a str enum class representation cardinal directions. """ -from enum import Enum +from enum import StrEnum __all__ = ["Cardinal"] -class Cardinal(Enum): +class Cardinal(StrEnum): """ - An enum class that define valid cardinal direction. + A str enum class that define valid cardinal direction. Attributes: NORTH: `"N"` @@ -29,6 +29,18 @@ class Cardinal(Enum): NORTH_NORTHWEST: `"NNW"` VARIABLE: `"VARIABLE"` + Examples: + >>> Cardinal("N") + + >>> Cardinal["NORTH"] + + >>> Cardinal.NORTH + + >>> Cardinal.NORTH == "N" + True + >>> print(Cardinal.NORTH) + N + Note: `VARIABLE` direction mean as its name suggest, the direction can't be determined. diff --git a/src/bmkg/weather_forecast/enums/province.py b/src/bmkg/weather_forecast/enums/province.py index 74e2d0b..14e7edf 100644 --- a/src/bmkg/weather_forecast/enums/province.py +++ b/src/bmkg/weather_forecast/enums/province.py @@ -1,14 +1,14 @@ """ -Module to store an enum class representation the name of the province. +Module to store a str enum class representation the name of the province. """ -from enum import Enum +from enum import StrEnum __all__ = ["Province"] -class Province(Enum): +class Province(StrEnum): """ - An enum class that define valid province. + A str enum class that define valid province. Attributes: ACEH: `"Aceh"` @@ -47,6 +47,18 @@ class Province(Enum): SUMATERA_UTARA: `"SumateraUtara"` INDONESIA: `"Indonesia"` + Examples: + >>> Province("Aceh") + + >>> Province["ACEH"] + + >>> Province.ACEH + + >>> Province.ACEH == "Aceh" + True + >>> print(Province.ACEH) + Aceh + Note: `INDONESIA` is the country name. """ diff --git a/src/bmkg/weather_forecast/enums/sexa.py b/src/bmkg/weather_forecast/enums/sexa.py index eb66447..b2b1e75 100644 --- a/src/bmkg/weather_forecast/enums/sexa.py +++ b/src/bmkg/weather_forecast/enums/sexa.py @@ -1,14 +1,14 @@ """ -Module to store an enum class representation sexa directions. +Module to store a str enum class representation sexa directions. """ -from enum import Enum +from enum import StrEnum __all__ = ["Sexa"] -class Sexa(Enum): +class Sexa(StrEnum): """ - An enum class that define valid sexa direction. + A str enum class that define valid sexa direction. Attributes: NORTH_NORTHEAST: `"2230"` @@ -28,6 +28,18 @@ class Sexa(Enum): NORTH_NORTHWEST: `"33730"` VARIABLE: `"000"` + Examples: + >>> Sexa("2230") + + >>> Sexa["NORTH_NORTHEAST"] + + >>> Sexa.NORTH_NORTHEAST + + >>> Sexa.NORTH_NORTHEAST == "2230" + True + >>> print(Sexa.NORTH_NORTHEAST) + 2230 + Note: `VARIABLE` direction mean as it's name suggest, the direction can't be determined. diff --git a/src/bmkg/weather_forecast/enums/type.py b/src/bmkg/weather_forecast/enums/type.py index 7bcd746..fe31eca 100644 --- a/src/bmkg/weather_forecast/enums/type.py +++ b/src/bmkg/weather_forecast/enums/type.py @@ -1,18 +1,30 @@ """ -Module to store an enum class representation area type. +Module to store a str enum class representation area type. """ -from enum import Enum +from enum import StrEnum __all__ = ["Type"] -class Type(Enum): +class Type(StrEnum): """ - An enum class that define valid area type. + A str enum class that define valid area type. Attributes: LAND: `"land"` SEA: `"sea"` + + Examples: + >>> Type("land") + + >>> Type["LAND"] + + >>> Type.LAND + + >>> Type.LAND == "land" + True + >>> print(Type.LAND) + land """ LAND: str = "land" diff --git a/src/bmkg/weather_forecast/enums/weather.py b/src/bmkg/weather_forecast/enums/weather.py index 0abcc6d..f90c827 100644 --- a/src/bmkg/weather_forecast/enums/weather.py +++ b/src/bmkg/weather_forecast/enums/weather.py @@ -1,14 +1,14 @@ """ -Module to store an enum class representation of the weather code. +Module to store an int enum class representation of the weather code. """ -from enum import Enum +from enum import IntEnum __all__ = ["Weather"] -class Weather(Enum): +class Weather(IntEnum): """ - An enum class that define valid weather. + An int enum class that define valid weather. Attributes: CLEAR_SKIES: `0` @@ -26,6 +26,18 @@ class Weather(Enum): SEVERE_THUNDERSTORM: `95` SEVERE_THUNDERSTORM2: `97` + Examples: + >>> Weather(0) + + >>> Weather["CLEAR_SKIES"] + + >>> Weather.CLEAR_SKIES + + >>> Weather.CLEAR_SKIES == 0 + True + >>> print(Weather.CLEAR_SKIES) + 0 + Note: There is `PARTLY_CLOUDY` and `PARTLY_CLOUDY2`, the weather condition is equal only the number representation is different. This is diff --git a/src/bmkg/weather_forecast/weather_forecast.py b/src/bmkg/weather_forecast/weather_forecast.py index 6b89394..6f51404 100644 --- a/src/bmkg/weather_forecast/weather_forecast.py +++ b/src/bmkg/weather_forecast/weather_forecast.py @@ -35,6 +35,6 @@ async def get_weather_forecast(self, province: Province) -> WeatherForecastData: WeatherForecast(data=Data(source=...) """ # noqa: E501 async with self._session.get( - f"{self.base_url}{self.url}DigitalForecast-{province.value}.xml" + f"{self.base_url}{self.url}DigitalForecast-{province}.xml" ) as response: return parse_weather_forecast_data(await response.read())