velmurugan1122 commited on
Commit
fbed371
·
1 Parent(s): c2815ab

fix: changes

Browse files
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 API! Use /continent/{continent_name} to get population statistics."}
26
-
27
-
28
- @router.get("/continent/{continent_name}", response_model=ContinentPopulationStats)
29
- def get_continent_population_data(continent_name: str):
30
- try:
31
- continent_countries = get_continent_countries(continent_name)
32
-
33
- max_pop_country = continent_countries.loc[continent_countries["Population"].idxmax()]
34
- min_pop_country = continent_countries.loc[continent_countries["Population"].idxmin()]
35
- avg_population = continent_countries["Population"].mean()
36
- avg_pop_country = continent_countries.iloc[(continent_countries["Population"] - avg_population).abs().idxmin()]
37
-
38
- return {
39
- "continent": continent_name,
40
- "max_population": int(max_pop_country["Population"]),
41
- "min_population": int(min_pop_country["Population"]),
42
- "avg_population": round(avg_population, 2),
43
- "max_population_country": max_pop_country["Country"],
44
- "min_population_country": min_pop_country["Country"],
45
- "avg_population_country": avg_pop_country["Country"]
46
- }
47
- except Exception as e:
48
- logger.error(f"❌ Error: {str(e)}")
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
+