logeswari commited on
Commit
5ba984e
·
0 Parent(s):

first commit

Browse files
__init__.py ADDED
File without changes
__pycache__/__init__.cpython-313.pyc ADDED
Binary file (124 Bytes). View file
 
__pycache__/main.cpython-313.pyc ADDED
Binary file (683 Bytes). View file
 
api/__pycache__/routes.cpython-313.pyc ADDED
Binary file (644 Bytes). View file
 
api/routes.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter
2
+ from new.services.population import get_continent_population
3
+ from new.models.schemas import ContinentPopulationResponse
4
+
5
+ router = APIRouter()
6
+
7
+ @router.get("/continent/{continent_name}", response_model=ContinentPopulationResponse)
8
+ def continent_population(continent_name: str):
9
+ return get_continent_population(continent_name)
logs/app.log ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # Path to the logs directory
4
+ log_dir = "D:/pydantic/new/logs"
5
+ os.makedirs(log_dir, exist_ok=True) # Create the folder if it doesn't exist
6
+ 2025-02-07 21:06:23,364 - ERROR - Error loading CSV: [Errno 2] No such file or directory: 'data/world_population.csv'
7
+ 2025-02-07 21:08:17,879 - INFO - CSV file loaded successfully.
8
+ 2025-02-07 21:08:17,915 - INFO - Will watch for changes in these directories: ['D:\\pydantic\\new']
9
+ 2025-02-07 21:08:17,920 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
10
+ 2025-02-07 21:08:17,923 - INFO - Started reloader process [25348] using WatchFiles
11
+ 2025-02-07 21:08:21,018 - INFO - CSV file loaded successfully.
12
+ 2025-02-07 21:08:21,030 - INFO - Started server process [37976]
13
+ 2025-02-07 21:08:21,039 - INFO - Waiting for application startup.
14
+ 2025-02-07 21:08:21,043 - INFO - Application startup complete.
15
+ 2025-02-07 21:08:29,496 - INFO - Accessed home endpoint.
16
+ 2025-02-07 21:08:47,547 - INFO - Fetching data for continent: Asia
17
+ 2025-02-07 21:09:22,367 - INFO - Accessed home endpoint.
18
+ 2025-02-07 21:09:49,382 - INFO - 1 change detected
19
+ 2025-02-07 21:09:49,760 - INFO - 1 change detected
20
+ 2025-02-07 21:10:17,752 - INFO - Shutting down
21
+ 2025-02-07 21:10:17,860 - INFO - Waiting for application shutdown.
22
+ 2025-02-07 21:10:17,863 - INFO - Application shutdown complete.
23
+ 2025-02-07 21:10:17,865 - INFO - Finished server process [37976]
24
+ 2025-02-07 21:10:18,297 - INFO - Stopping reloader process [25348]
25
+ 2025-02-07 21:10:24,405 - INFO - CSV file loaded successfully.
26
+ 2025-02-07 21:10:24,442 - INFO - Will watch for changes in these directories: ['D:\\pydantic\\new']
27
+ 2025-02-07 21:10:24,445 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
28
+ 2025-02-07 21:10:24,448 - INFO - Started reloader process [27472] using WatchFiles
29
+ 2025-02-07 21:10:27,357 - INFO - CSV file loaded successfully.
30
+ 2025-02-07 21:10:27,362 - INFO - Started server process [48848]
31
+ 2025-02-07 21:10:27,370 - INFO - Waiting for application startup.
32
+ 2025-02-07 21:10:27,373 - INFO - Application startup complete.
33
+ 2025-02-07 21:32:06,543 - INFO - 1 change detected
34
+ 2025-02-07 21:32:06,638 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
35
+ 2025-02-07 21:32:06,877 - INFO - Shutting down
36
+ 2025-02-07 21:32:07,124 - INFO - Waiting for application shutdown.
37
+ 2025-02-07 21:32:07,213 - INFO - Application shutdown complete.
38
+ 2025-02-07 21:32:07,233 - INFO - Finished server process [48848]
39
+ 2025-02-07 21:32:08,725 - INFO - 1 change detected
40
+ 2025-02-07 21:32:09,087 - INFO - 1 change detected
41
+ 2025-02-07 21:32:09,092 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
42
+ 2025-02-07 21:32:13,038 - INFO - 1 change detected
43
+ 2025-02-07 21:32:13,040 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
44
+ 2025-02-07 21:32:14,210 - INFO - 1 change detected
45
+ 2025-02-07 21:32:16,888 - INFO - 1 change detected
46
+ 2025-02-07 21:32:17,252 - INFO - 1 change detected
47
+ 2025-02-07 21:32:22,092 - INFO - 1 change detected
48
+ 2025-02-07 21:32:22,094 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
49
+ 2025-02-07 21:32:25,459 - INFO - 1 change detected
50
+ 2025-02-07 21:32:25,816 - INFO - 1 change detected
51
+ 2025-02-07 21:32:33,449 - INFO - 1 change detected
52
+ 2025-02-07 21:32:33,450 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
53
+ 2025-02-07 21:32:36,354 - INFO - 1 change detected
54
+ 2025-02-07 21:32:36,355 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
55
+ 2025-02-07 21:32:37,490 - INFO - 6 changes detected
56
+ 2025-02-07 21:32:40,131 - INFO - 1 change detected
57
+ 2025-02-07 21:32:40,494 - INFO - 1 change detected
58
+ 2025-02-07 21:32:48,156 - INFO - 1 change detected
59
+ 2025-02-07 21:32:48,157 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
60
+ 2025-02-07 21:32:51,401 - INFO - 6 changes detected
61
+ 2025-02-07 21:32:51,769 - INFO - 1 change detected
62
+ 2025-02-07 21:32:57,409 - INFO - 1 change detected
63
+ 2025-02-07 21:32:57,410 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
64
+ 2025-02-07 21:33:01,058 - INFO - 6 changes detected
65
+ 2025-02-07 21:33:01,729 - INFO - 1 change detected
66
+ 2025-02-07 21:33:01,730 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
67
+ 2025-02-07 21:33:04,819 - INFO - 6 changes detected
68
+ 2025-02-07 21:33:05,178 - INFO - 1 change detected
69
+ 2025-02-07 21:33:21,444 - INFO - 1 change detected
70
+ 2025-02-07 21:33:21,445 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
71
+ 2025-02-07 21:33:24,947 - INFO - 1 change detected
72
+ 2025-02-07 21:33:25,308 - INFO - 1 change detected
73
+ 2025-02-07 21:33:26,223 - INFO - 1 change detected
74
+ 2025-02-07 21:33:26,225 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
75
+ 2025-02-07 21:33:29,368 - INFO - 6 changes detected
76
+ 2025-02-07 21:33:29,728 - INFO - 1 change detected
77
+ 2025-02-07 21:33:30,186 - INFO - 1 change detected
78
+ 2025-02-07 21:33:30,187 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
79
+ 2025-02-07 21:33:33,481 - INFO - 6 changes detected
80
+ 2025-02-07 21:33:33,843 - INFO - 1 change detected
81
+ 2025-02-07 21:33:37,029 - INFO - 1 change detected
82
+ 2025-02-07 21:33:37,030 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
83
+ 2025-02-07 21:33:40,443 - INFO - 6 changes detected
84
+ 2025-02-07 21:33:40,808 - INFO - 2 changes detected
85
+ 2025-02-07 21:33:40,809 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
86
+ 2025-02-07 21:33:43,045 - INFO - 1 change detected
87
+ 2025-02-07 21:33:43,046 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
88
+ 2025-02-07 21:33:44,598 - INFO - 6 changes detected
89
+ 2025-02-07 21:33:47,321 - INFO - 1 change detected
90
+ 2025-02-07 21:33:47,674 - INFO - 1 change detected
91
+ 2025-02-07 21:33:48,889 - INFO - 1 change detected
92
+ 2025-02-07 21:33:48,891 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
93
+ 2025-02-07 21:33:52,077 - INFO - 6 changes detected
94
+ 2025-02-07 21:33:52,436 - INFO - 1 change detected
95
+ 2025-02-07 21:33:53,501 - INFO - 1 change detected
96
+ 2025-02-07 21:33:53,507 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
97
+ 2025-02-07 21:33:55,948 - INFO - 1 change detected
98
+ 2025-02-07 21:33:55,949 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
99
+ 2025-02-07 21:33:57,621 - INFO - 6 changes detected
100
+ 2025-02-07 21:34:00,549 - INFO - 1 change detected
101
+ 2025-02-07 21:34:00,902 - INFO - 1 change detected
102
+ 2025-02-07 21:34:02,922 - INFO - 1 change detected
103
+ 2025-02-07 21:34:02,923 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
104
+ 2025-02-07 21:34:04,856 - INFO - 1 change detected
105
+ 2025-02-07 21:34:04,857 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
106
+ 2025-02-07 21:34:07,170 - INFO - 6 changes detected
107
+ 2025-02-07 21:34:09,944 - INFO - 1 change detected
108
+ 2025-02-07 21:34:10,299 - INFO - 1 change detected
109
+ 2025-02-07 21:34:14,248 - INFO - 1 change detected
110
+ 2025-02-07 21:34:14,249 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
111
+ 2025-02-07 21:34:17,595 - INFO - 6 changes detected
112
+ 2025-02-07 21:34:17,958 - INFO - 1 change detected
113
+ 2025-02-07 21:35:16,320 - INFO - 1 change detected
114
+ 2025-02-07 21:35:16,321 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
115
+ 2025-02-07 21:35:20,069 - INFO - 6 changes detected
116
+ 2025-02-07 21:35:20,428 - INFO - 1 change detected
117
+ 2025-02-07 21:45:10,763 - INFO - CSV file loaded successfully.
118
+ 2025-02-07 21:45:10,798 - INFO - Will watch for changes in these directories: ['D:\\pydantic\\new']
119
+ 2025-02-07 21:45:10,801 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
120
+ 2025-02-07 21:45:10,806 - INFO - Started reloader process [22468] using WatchFiles
121
+ 2025-02-07 21:45:13,898 - INFO - CSV file loaded successfully.
122
+ 2025-02-07 21:45:13,907 - INFO - Started server process [44660]
123
+ 2025-02-07 21:45:13,914 - INFO - Waiting for application startup.
124
+ 2025-02-07 21:45:13,923 - INFO - Application startup complete.
125
+ 2025-02-07 21:45:19,352 - INFO - Accessed home endpoint.
126
+ 2025-02-07 22:41:40,339 - INFO - 1 change detected
127
+ 2025-02-07 22:41:40,416 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
128
+ 2025-02-07 22:41:40,634 - INFO - Shutting down
129
+ 2025-02-07 22:41:40,798 - INFO - Waiting for application shutdown.
130
+ 2025-02-07 22:41:40,814 - INFO - Application shutdown complete.
131
+ 2025-02-07 22:41:40,822 - INFO - Finished server process [44660]
132
+ 2025-02-07 22:41:42,236 - INFO - 2 changes detected
133
+ 2025-02-07 22:41:42,239 - WARNING - WatchFiles detected changes in 'models\schemas.py'. Reloading...
134
+ 2025-02-07 22:41:47,204 - INFO - CSV file loaded successfully.
135
+ 2025-02-07 22:41:47,214 - INFO - Started server process [36856]
136
+ 2025-02-07 22:41:47,222 - INFO - Waiting for application startup.
137
+ 2025-02-07 22:41:47,229 - INFO - Application startup complete.
138
+ 2025-02-07 22:41:47,251 - INFO - 5 changes detected
139
+ 2025-02-07 22:49:17,706 - INFO - Shutting down
140
+ 2025-02-07 22:49:17,861 - INFO - Waiting for application shutdown.
141
+ 2025-02-07 22:49:17,872 - INFO - Application shutdown complete.
142
+ 2025-02-07 22:49:17,877 - INFO - Finished server process [36856]
143
+ 2025-02-07 22:49:18,562 - INFO - Stopping reloader process [22468]
144
+ 2025-02-07 22:58:26,778 - INFO - CSV file loaded successfully.
145
+ 2025-02-07 22:58:26,816 - INFO - Will watch for changes in these directories: ['D:\\pydantic\\new']
146
+ 2025-02-07 22:58:26,820 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
147
+ 2025-02-07 22:58:26,823 - INFO - Started reloader process [46736] using WatchFiles
148
+ 2025-02-07 22:58:29,766 - INFO - CSV file loaded successfully.
149
+ 2025-02-07 22:58:29,772 - INFO - Started server process [22336]
150
+ 2025-02-07 22:58:29,780 - INFO - Waiting for application startup.
151
+ 2025-02-07 22:58:29,790 - INFO - Application startup complete.
152
+ 2025-02-07 22:58:51,417 - INFO - Accessed home endpoint.
153
+ 2025-02-07 23:03:00,949 - INFO - Shutting down
154
+ 2025-02-07 23:03:01,074 - INFO - Waiting for application shutdown.
155
+ 2025-02-07 23:03:01,079 - INFO - Application shutdown complete.
156
+ 2025-02-07 23:03:01,083 - INFO - Finished server process [22336]
157
+ 2025-02-07 23:03:01,743 - INFO - Stopping reloader process [46736]
main.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from new.api.routes import router
3
+ from new.utils.logger import logger
4
+ import new
5
+
6
+ app = FastAPI()
7
+
8
+ # Include routes
9
+ app.include_router(router)
10
+
11
+ @app.get("/")
12
+ def home():
13
+ logger.info("Accessed home endpoint.")
14
+ return {"message": "Welcome to the World Population API"}
models/__pycache__/schemas.cpython-313.pyc ADDED
Binary file (1.71 kB). View file
 
models/schemas.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel, Field
2
+
3
+ class CountryPopulation(BaseModel):
4
+ country: str
5
+ population: int
6
+
7
+ class ContinentPopulationResponse(BaseModel):
8
+ continent: str = Field(..., description="Name of the continent")
9
+ total_population: int = Field(..., description="Total population of the continent")
10
+ total_area: int = Field(..., description="Total area of the continent")
11
+ continent_population_density: float = Field(..., description="Population density of the continent")
12
+ max_population: CountryPopulation = Field(..., description="Country with the maximum population")
13
+ min_population: CountryPopulation = Field(..., description="Country with the minimum population")
14
+ avg_population: int = Field(..., description="Average population of the continent")
15
+
16
+ # Schema-level configuration
17
+ model_config = {
18
+ "json_schema_extra": {
19
+ "examples": [
20
+ {
21
+ "continent": "Asia",
22
+ "total_population": 4641054775,
23
+ "total_area": 44579000,
24
+ "continent_population_density": 104.1,
25
+ "max_population": {"country": "China", "population": 1411778724},
26
+ "min_population": {"country": "Maldives", "population": 521874},
27
+ "avg_population": 92821192
28
+ }
29
+ ]
30
+ }
31
+ }
services/__pycache__/population.cpython-313.pyc ADDED
Binary file (3.41 kB). View file
 
services/population.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+
4
+ src_directory=os.path.abspath(os.path.join(os.path.dirname(__file__),"../..","pydantic"))
5
+ sys.path.append(src_directory)
6
+
7
+
8
+
9
+ import pandas as pd
10
+ from new.utils.logger import logger
11
+ from fastapi import HTTPException
12
+ from new.models.schemas import ContinentPopulationResponse, CountryPopulation
13
+
14
+ file_path = "D:/pandas/world_population (1).csv"
15
+
16
+ # Load the CSV file
17
+ try:
18
+ countries_df = pd.read_csv(file_path)
19
+ countries_df["Population"] = pd.to_numeric(countries_df["Population"], errors="coerce")
20
+ countries_df["Area"] = pd.to_numeric(countries_df["Area"], errors="coerce")
21
+ logger.info("CSV file loaded successfully.")
22
+ except Exception as e:
23
+ logger.error(f"Error loading CSV: {e}")
24
+ raise RuntimeError("Failed to load CSV file.")
25
+
26
+ def get_continent_population(continent_name: str) -> ContinentPopulationResponse:
27
+ try:
28
+ logger.info(f"Fetching data for continent: {continent_name}")
29
+ continent_group = countries_df[countries_df["Continent"] == continent_name].dropna(subset=["Population", "Area"])
30
+
31
+ if continent_group.empty:
32
+ raise HTTPException(status_code=404, detail="Continent not found.")
33
+
34
+ total_population = int(continent_group["Population"].sum())
35
+ total_area = int(continent_group["Area"].sum())
36
+ if total_area == 0:
37
+ raise HTTPException(status_code=400, detail="Total area is zero.")
38
+
39
+ max_row = continent_group.loc[continent_group["Population"].idxmax()]
40
+ min_row = continent_group.loc[continent_group["Population"].idxmin()]
41
+ avg_population = int(continent_group["Population"].mean())
42
+
43
+ return ContinentPopulationResponse(
44
+ continent=continent_name,
45
+ total_population=total_population,
46
+ total_area=total_area,
47
+ continent_population_density=total_population / total_area,
48
+ max_population=CountryPopulation(
49
+ country=max_row["Country"], population=int(max_row["Population"])
50
+ ),
51
+ min_population=CountryPopulation(
52
+ country=min_row["Country"], population=int(min_row["Population"])
53
+ ),
54
+ avg_population=avg_population
55
+ )
56
+ except HTTPException as http_err:
57
+ logger.error(f"HTTP exception: {http_err.detail}")
58
+ raise http_err
59
+ except Exception as e:
60
+ logger.error(f"Unexpected error: {e}")
61
+ raise HTTPException(status_code=500, detail="Internal server error.")
utils/__pycache__/logger.cpython-313.pyc ADDED
Binary file (680 Bytes). View file
 
utils/logger.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import os
3
+
4
+ # Ensure the 'logs' directory exists
5
+ log_dir = "D:/pydantic/new/logs"
6
+ os.makedirs(log_dir, exist_ok=True)
7
+
8
+ # Configure logging
9
+ logging.basicConfig(
10
+ level=logging.INFO,
11
+ format="%(asctime)s - %(levelname)s - %(message)s",
12
+ handlers=[
13
+ logging.FileHandler(f"{log_dir}/app.log"), # Save logs in 'logs/app.log'
14
+ logging.StreamHandler(), # Also print logs to console
15
+ ],
16
+ )
17
+ logger = logging.getLogger(__name__)