Skip to content

Commit

Permalink
feat: uses worldometers and default source
Browse files Browse the repository at this point in the history
  • Loading branch information
nf1s committed Jul 20, 2024
1 parent 2b6a672 commit 406537c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions covid/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Options(str, Enum):
@app.command()
def get_data(
source: str = typer.Option(
"john_hopkins",
"worldometers",
"-s",
"--source",
help="select source, 'john_hopkins' or 'worldometers'",
Expand All @@ -33,7 +33,7 @@ def get_data(
None,
"--option",
"-o",
help="get total stats. options 'active', 'confimed', 'recovered' or 'deaths'",
help="get total stats. options 'active', 'confirmed', 'recovered' or 'deaths'",
),
list_countries: bool = typer.Option(
None,
Expand Down
8 changes: 5 additions & 3 deletions covid/john_hopkins/covid.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ def list_countries(self) -> list:
list[str]: list of country names
"""
cases = self.__get_all_cases()
return [CountryModel(**case["attributes"]).dict() for case in cases]
return [
CountryModel(**case["attributes"]).model_dump() for case in cases
]

def get_status_by_country_id(self, country_id) -> dict:
"""Method fetches and returns specific country information related to coronavirus
Expand All @@ -182,7 +184,7 @@ def get_status_by_country_id(self, country_id) -> dict:
"""

case = self.__get_total_cases_by_country_id(country_id)
return CovidModel(**case).dict()
return CovidModel(**case).model_dump()

def get_status_by_country_name(self, country_name) -> dict:
"""Method fetches and returns specific country information related to coronavirus
Expand Down Expand Up @@ -216,4 +218,4 @@ def get_status_by_country_name(self, country_name) -> dict:
f"There is no country called '{country_name}', to check available country names use `list_countries()`"
)
case = self.__get_total_cases_by_country_id(country["id"])
return CovidModel(**case).dict()
return CovidModel(**case).model_dump()
18 changes: 9 additions & 9 deletions covid/john_hopkins/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
class CovidModel(BaseModel):
"""Dataclass acts as a Model for Covid data"""

id: str = Field(..., alias="OBJECTID")
id: int = Field(..., alias="OBJECTID")
country: str = Field(..., alias="Country_Region")
confirmed: int = Field(None, alias="Confirmed")
active: int = Field(None, alias="Active")
deaths: int = Field(None, alias="Deaths")
recovered: int = Field(None, alias="Recovered")
latitude: float = Field(None, alias="Lat")
longitude: float = Field(None, alias="Long_")
last_update: int = Field(None, alias="Last_Update")
confirmed: int | None = Field(None, alias="Confirmed")
active: int | None = Field(None, alias="Active")
deaths: int | None = Field(None, alias="Deaths")
recovered: int | None = Field(None, alias="Recovered")
latitude: float | None = Field(None, alias="Lat")
longitude: float | None = Field(None, alias="Long_")
last_update: int | None = Field(None, alias="Last_Update")


class CountryModel(BaseModel):
"""Dataclass acts as a Model for Countries data"""

id: str = Field(..., alias="OBJECTID")
id: int = Field(..., alias="OBJECTID")
name: str = Field(..., alias="Country_Region")
6 changes: 4 additions & 2 deletions covid/worldometers/covid.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def get_data(self) -> list:
list: List of country data
"""
return [
CovidModel(**dict(zip(self.__headers, self.__format(val)))).dict()
CovidModel(
**dict(zip(self.__headers, self.__format(val)))
).model_dump()
for val in self.__data.values()
]

Expand All @@ -95,7 +97,7 @@ def get_status_by_country_name(self, country_name: str) -> dict:
raise ValueError(
f"There is no country called '{country_name}', to check available country names use `list_countries()`"
)
return CovidModel(**country_data).dict()
return CovidModel(**country_data).model_dump()

def list_countries(self) -> list:
return list(self.__data.keys())
Expand Down
8 changes: 6 additions & 2 deletions covid/worldometers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"""
from decimal import Decimal

from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, field_validator


class CovidModel(BaseModel):
"""Dataclass acts as a Model for Covid data"""

country: str = Field(..., alias="Country,Other")
country: str | int = Field(..., alias="Country,Other")
total_cases: int = Field(0, alias="TotalCases")
confirmed: int = Field(0, alias="TotalCases")
new_cases: int = Field(0, alias="NewCases")
Expand All @@ -29,3 +29,7 @@ class CovidModel(BaseModel):
Decimal(0), alias="Deaths/1M pop"
)
population: Decimal = Field(Decimal(0), alias="Population")

@field_validator("country")
def country_must_be_string(cls, value):
return str(value)

0 comments on commit 406537c

Please sign in to comment.