Skip to content

Commit

Permalink
use StrEnum and IntEnum instead of Enum
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kiraware committed Jan 26, 2024
1 parent 0192bb8 commit 5e2bf56
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 21 deletions.
20 changes: 16 additions & 4 deletions src/bmkg/weather_forecast/enums/cardinal.py
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -29,6 +29,18 @@ class Cardinal(Enum):
NORTH_NORTHWEST: `"NNW"`
VARIABLE: `"VARIABLE"`
Examples:
>>> Cardinal("N")
<Cardinal.NORTH: 'N'>
>>> Cardinal["NORTH"]
<Cardinal.NORTH: 'N'>
>>> Cardinal.NORTH
<Cardinal.NORTH: 'N'>
>>> Cardinal.NORTH == "N"
True
>>> print(Cardinal.NORTH)
N
Note:
`VARIABLE` direction mean as its name suggest, the direction can't be
determined.
Expand Down
20 changes: 16 additions & 4 deletions src/bmkg/weather_forecast/enums/province.py
Original file line number Diff line number Diff line change
@@ -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"`
Expand Down Expand Up @@ -47,6 +47,18 @@ class Province(Enum):
SUMATERA_UTARA: `"SumateraUtara"`
INDONESIA: `"Indonesia"`
Examples:
>>> Province("Aceh")
<Province.ACEH: 'Aceh'>
>>> Province["ACEH"]
<Province.ACEH: 'Aceh'>
>>> Province.ACEH
<Province.ACEH: 'Aceh'>
>>> Province.ACEH == "Aceh"
True
>>> print(Province.ACEH)
Aceh
Note:
`INDONESIA` is the country name.
"""
Expand Down
20 changes: 16 additions & 4 deletions src/bmkg/weather_forecast/enums/sexa.py
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -28,6 +28,18 @@ class Sexa(Enum):
NORTH_NORTHWEST: `"33730"`
VARIABLE: `"000"`
Examples:
>>> Sexa("2230")
<Sexa.NORTH_NORTHEAST: '2230'>
>>> Sexa["NORTH_NORTHEAST"]
<Sexa.NORTH_NORTHEAST: '2230'>
>>> Sexa.NORTH_NORTHEAST
<Sexa.NORTH_NORTHEAST: '2230'>
>>> 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.
Expand Down
20 changes: 16 additions & 4 deletions src/bmkg/weather_forecast/enums/type.py
Original file line number Diff line number Diff line change
@@ -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: 'land'>
>>> Type["LAND"]
<Type.LAND: 'land'>
>>> Type.LAND
<Type.LAND: 'land'>
>>> Type.LAND == "land"
True
>>> print(Type.LAND)
land
"""

LAND: str = "land"
Expand Down
20 changes: 16 additions & 4 deletions src/bmkg/weather_forecast/enums/weather.py
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -26,6 +26,18 @@ class Weather(Enum):
SEVERE_THUNDERSTORM: `95`
SEVERE_THUNDERSTORM2: `97`
Examples:
>>> Weather(0)
<Weather.CLEAR_SKIES: 0>
>>> Weather["CLEAR_SKIES"]
<Weather.CLEAR_SKIES: 0>
>>> Weather.CLEAR_SKIES
<Weather.CLEAR_SKIES: 0>
>>> 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
Expand Down
2 changes: 1 addition & 1 deletion src/bmkg/weather_forecast/weather_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

0 comments on commit 5e2bf56

Please sign in to comment.