Spaces:
Sleeping
Sleeping
Commit
·
fbed371
1
Parent(s):
c2815ab
fix: changes
Browse files- logs/app.log +0 -0
- models/__pycache__/continent.cpython-313.pyc +0 -0
- models/continent.py +18 -1
- models/country.py +27 -0
- routes/__pycache__/population_routes.cpython-313.pyc +0 -0
- routes/population_routes.py +27 -32
logs/app.log
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
models/__pycache__/continent.cpython-313.pyc
CHANGED
|
Binary files a/models/__pycache__/continent.cpython-313.pyc and b/models/__pycache__/continent.cpython-313.pyc differ
|
|
|
models/continent.py
CHANGED
|
@@ -2,4 +2,21 @@ import pandas as pd
|
|
| 2 |
|
| 3 |
def read_country_data(path_world):
|
| 4 |
"""Reads CSV data into a Pandas DataFrame"""
|
| 5 |
-
return pd.read_csv(path_world)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def read_country_data(path_world):
|
| 4 |
"""Reads CSV data into a Pandas DataFrame"""
|
| 5 |
+
return pd.read_csv(path_world)
|
| 6 |
+
|
| 7 |
+
def get_continents(data_frame):
|
| 8 |
+
continents = data_frame["Continent"].unique()
|
| 9 |
+
return continents
|
| 10 |
+
|
| 11 |
+
def get_continent_stats(data_frame, attribute, stat_type):
|
| 12 |
+
continents_stats = data_frame.groupby("Continent")[attribute].agg(total_attribute = 'sum')
|
| 13 |
+
if stat_type == "highest":
|
| 14 |
+
continent = continents_stats.idxmax().item()
|
| 15 |
+
value = continents_stats.max().item()
|
| 16 |
+
return {continent: value}
|
| 17 |
+
elif stat_type == "lowest":
|
| 18 |
+
continent = continents_stats.idxmin().item()
|
| 19 |
+
value = continents_stats.min().item()
|
| 20 |
+
return {continent: value}
|
| 21 |
+
|
| 22 |
+
|
models/country.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
def get_country(data_frame):
|
| 3 |
+
country = data_frame["Country"].values
|
| 4 |
+
return country
|
| 5 |
+
|
| 6 |
+
def get_country_stats(data_frame, attribute, stat_type):
|
| 7 |
+
if stat_type == "highest":
|
| 8 |
+
idx = data_frame[attribute].idxmax()
|
| 9 |
+
if stat_type == "lowest":
|
| 10 |
+
idx = data_frame[attribute].idxmin()
|
| 11 |
+
|
| 12 |
+
country= data_frame["Country"][idx]
|
| 13 |
+
attribute_result = data_frame[attribute][idx]
|
| 14 |
+
result = {country: attribute_result.item()}
|
| 15 |
+
return result
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# continents_stats = data_frame.[attribute].gg(total_attribute = 'sum')
|
| 20 |
+
# if stat_type == "highest":
|
| 21 |
+
# continent = continents_stats.idxmax().item()
|
| 22 |
+
# value = continents_stats.max().item()
|
| 23 |
+
# return {continent: value}
|
| 24 |
+
# elif stat_type == "lowest":
|
| 25 |
+
# continent = continents_stats.idxmin().item()
|
| 26 |
+
# value = continents_stats.min().item()
|
| 27 |
+
# return {continent: value}
|
routes/__pycache__/population_routes.cpython-313.pyc
CHANGED
|
Binary files a/routes/__pycache__/population_routes.cpython-313.pyc and b/routes/__pycache__/population_routes.cpython-313.pyc differ
|
|
|
routes/population_routes.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
from fastapi import APIRouter, HTTPException
|
| 2 |
import logging
|
| 3 |
import pandas as pd
|
| 4 |
-
from models.continent import read_country_data
|
| 5 |
from models.validation_population import ContinentPopulationStats
|
|
|
|
|
|
|
| 6 |
|
| 7 |
router = APIRouter()
|
| 8 |
|
|
@@ -13,37 +15,30 @@ DATA_PATH = "data/world_population.csv"
|
|
| 13 |
countries_df = read_country_data(DATA_PATH)
|
| 14 |
logger.info("✅ Data loaded successfully from CSV.")
|
| 15 |
|
| 16 |
-
def get_continent_countries(continent_name: str):
|
| 17 |
-
filtered_countries = countries_df[countries_df["Continent"].str.lower() == continent_name.lower()]
|
| 18 |
-
if filtered_countries.empty:
|
| 19 |
-
logger.warning(f"⚠️ Continent '{continent_name}' not found.")
|
| 20 |
-
raise HTTPException(status_code=404, detail=f"Continent '{continent_name}' not found.")
|
| 21 |
-
return filtered_countries
|
| 22 |
|
| 23 |
@router.get("/")
|
| 24 |
def home():
|
| 25 |
-
return {"message": "Welcome to the World Population
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
@router.get("/
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
raise HTTPException(status_code=500, detail="Internal Server Error")
|
|
|
|
| 1 |
from fastapi import APIRouter, HTTPException
|
| 2 |
import logging
|
| 3 |
import pandas as pd
|
| 4 |
+
from models.continent import read_country_data, get_continents, get_continent_stats
|
| 5 |
from models.validation_population import ContinentPopulationStats
|
| 6 |
+
from models.country import get_country, get_country_stats
|
| 7 |
+
|
| 8 |
|
| 9 |
router = APIRouter()
|
| 10 |
|
|
|
|
| 15 |
countries_df = read_country_data(DATA_PATH)
|
| 16 |
logger.info("✅ Data loaded successfully from CSV.")
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
@router.get("/")
|
| 20 |
def home():
|
| 21 |
+
return {"message": f"Welcome to the World Population"}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@router.get("/continents")
|
| 25 |
+
def display_continents():
|
| 26 |
+
result = get_continents(countries_df)
|
| 27 |
+
return result.tolist()
|
| 28 |
+
@router.get("/country")
|
| 29 |
+
def display_country():
|
| 30 |
+
res_country =get_country(countries_df)
|
| 31 |
+
return res_country.tolist()
|
| 32 |
+
|
| 33 |
+
@router.get("/continent_stats/{attribute}/{stat_type}")
|
| 34 |
+
def display_stats(attribute :str, stat_type: str):
|
| 35 |
+
continent_stars = get_continent_stats(countries_df, attribute, stat_type )
|
| 36 |
+
return continent_stars
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@router.get("/country_stats/{attribute}/{stat_type}")
|
| 40 |
+
def display_stats(attribute :str, stat_type: str):
|
| 41 |
+
country_stars = get_country_stats(countries_df, attribute, stat_type )
|
| 42 |
+
return country_stars
|
| 43 |
+
|
| 44 |
+
|
|
|