Rakshitjan commited on
Commit
45378f8
·
verified ·
1 Parent(s): e29022e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -90
main.py CHANGED
@@ -1,103 +1,56 @@
1
- # # Additional imports
2
- # from typing import List, Dict
3
- # from fastapi import FastAPI, HTTPException
4
- # from pydantic import BaseModel
5
- # import json
6
- # from datetime import datetime
7
-
8
- # # Initialize FastAPI
9
- # app = FastAPI()
10
- # # Merge roadmaps
11
- # def merge_roadmaps(regular_roadmap: Dict, test_roadmap: Dict) -> Dict:
12
- # """Merge two roadmaps into one, combining overlapping and non-overlapping dates."""
13
- # def schedule_to_dict(schedule: List[Dict]) -> Dict:
14
- # """Convert a roadmap's schedule into a dictionary indexed by dates."""
15
- # return {day["date"]: day for day in schedule}
16
-
17
- # # Convert schedules to dictionaries for quick lookup
18
- # regular_schedule_dict = schedule_to_dict(regular_roadmap["schedule"])
19
- # test_schedule_dict = schedule_to_dict(test_roadmap["schedule"])
20
-
21
- # # Parse dates from schedules
22
- # regular_dates = [datetime.strptime(day["date"], "%Y-%m-%d") for day in regular_roadmap["schedule"]]
23
- # test_dates = [datetime.strptime(day["date"], "%Y-%m-%d") for day in test_roadmap["schedule"]]
24
-
25
- # # Identify start and end dates for both roadmaps
26
- # regular_start, regular_end = min(regular_dates), max(regular_dates)
27
- # test_start, test_end = min(test_dates), max(test_dates)
28
-
29
- # # Merge overlapping dates
30
- # for date, test_day in test_schedule_dict.items():
31
- # if date in regular_schedule_dict:
32
- # # Append test roadmap subjects to matching days in regular roadmap
33
- # regular_schedule_dict[date]["subjects"].extend(test_day["subjects"])
34
- # else:
35
- # # Add non-overlapping test roadmap dates
36
- # regular_schedule_dict[date] = test_day
37
-
38
- # # Convert the merged schedule dictionary back to a sorted list
39
- # merged_schedule = sorted(regular_schedule_dict.values(), key=lambda x: x["date"])
40
-
41
- # return {"schedule": merged_schedule}
42
-
43
- # # New endpoint for merging roadmaps
44
- # @app.post("/merge-roadmaps/")
45
- # def merge_roadmaps_endpoint(roadmap1: Dict, roadmap2: Dict):
46
- # """
47
- # Merge two roadmaps into one, combining overlapping and non-overlapping dates.
48
- # """
49
- # try:
50
- # merged_roadmap = merge_roadmaps(roadmap1, roadmap2)
51
- # return merged_roadmap
52
- # except Exception as e:
53
- # raise HTTPException(status_code=500, detail=f"Error merging roadmaps: {e}")
54
-
55
-
56
  from fastapi import FastAPI, HTTPException
57
  from pydantic import BaseModel
58
- from typing import List, Dict, Any
59
  import json
 
60
 
 
61
  app = FastAPI()
62
-
63
- class Roadmap(BaseModel):
64
- schedule: List[Dict[str, Any]]
65
-
66
- @app.post("/merge_roadmaps/")
67
- def merge_roadmaps_endpoint(regular_roadmap: str, test_roadmaps: List[str]):
68
- try:
69
- # Parse input strings to JSON
70
- regular_roadmap_json = json.loads(regular_roadmap)
71
- test_roadmaps_json = [json.loads(test_roadmap) for test_roadmap in test_roadmaps]
72
-
73
- # Perform the merge
74
- merged_roadmap = merge_roadmaps(regular_roadmap_json, test_roadmaps_json)
75
- return merged_roadmap
76
- except Exception as e:
77
- raise HTTPException(status_code=400, detail=str(e))
78
-
79
-
80
-
81
- # Helper function
82
- def merge_roadmaps(regular_roadmap, test_roadmaps):
83
- def schedule_to_dict(schedule):
84
  return {day["date"]: day for day in schedule}
85
 
 
86
  regular_schedule_dict = schedule_to_dict(regular_roadmap["schedule"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- for test_roadmap in test_roadmaps:
89
- test_schedule_dict = schedule_to_dict(test_roadmap["schedule"])
90
 
91
- for date, test_day in test_schedule_dict.items():
92
- if date in regular_schedule_dict:
93
- if "test_study" not in regular_schedule_dict[date]:
94
- regular_schedule_dict[date]["test_study"] = []
95
- regular_schedule_dict[date]["test_study"].extend(test_day["subjects"])
 
 
 
 
 
 
96
 
97
- for date, test_day in test_schedule_dict.items():
98
- if date not in regular_schedule_dict:
99
- regular_schedule_dict[date] = test_day
100
 
101
- merged_schedule = sorted(regular_schedule_dict.values(), key=lambda x: x["date"])
102
 
103
- return {"schedule": merged_schedule}
 
1
+ # Additional imports
2
+ from typing import List, Dict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from fastapi import FastAPI, HTTPException
4
  from pydantic import BaseModel
 
5
  import json
6
+ from datetime import datetime
7
 
8
+ # Initialize FastAPI
9
  app = FastAPI()
10
+ # Merge roadmaps
11
+ def merge_roadmaps(regular_roadmap: Dict, test_roadmap: Dict) -> Dict:
12
+ """Merge two roadmaps into one, combining overlapping and non-overlapping dates."""
13
+ def schedule_to_dict(schedule: List[Dict]) -> Dict:
14
+ """Convert a roadmap's schedule into a dictionary indexed by dates."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  return {day["date"]: day for day in schedule}
16
 
17
+ # Convert schedules to dictionaries for quick lookup
18
  regular_schedule_dict = schedule_to_dict(regular_roadmap["schedule"])
19
+ test_schedule_dict = schedule_to_dict(test_roadmap["schedule"])
20
+
21
+ # Parse dates from schedules
22
+ regular_dates = [datetime.strptime(day["date"], "%Y-%m-%d") for day in regular_roadmap["schedule"]]
23
+ test_dates = [datetime.strptime(day["date"], "%Y-%m-%d") for day in test_roadmap["schedule"]]
24
+
25
+ # Identify start and end dates for both roadmaps
26
+ regular_start, regular_end = min(regular_dates), max(regular_dates)
27
+ test_start, test_end = min(test_dates), max(test_dates)
28
+
29
+ # Merge overlapping dates
30
+ for date, test_day in test_schedule_dict.items():
31
+ if date in regular_schedule_dict:
32
+ # Append test roadmap subjects to matching days in regular roadmap
33
+ regular_schedule_dict[date]["subjects"].extend(test_day["subjects"])
34
+ else:
35
+ # Add non-overlapping test roadmap dates
36
+ regular_schedule_dict[date] = test_day
37
+
38
+ # Convert the merged schedule dictionary back to a sorted list
39
+ merged_schedule = sorted(regular_schedule_dict.values(), key=lambda x: x["date"])
40
 
41
+ return {"schedule": merged_schedule}
 
42
 
43
+ # New endpoint for merging roadmaps
44
+ @app.post("/merge-roadmaps/")
45
+ def merge_roadmaps_endpoint(roadmap1: Dict, roadmap2: Dict):
46
+ """
47
+ Merge two roadmaps into one, combining overlapping and non-overlapping dates.
48
+ """
49
+ try:
50
+ merged_roadmap = merge_roadmaps(roadmap1, roadmap2)
51
+ return merged_roadmap
52
+ except Exception as e:
53
+ raise HTTPException(status_code=500, detail=f"Error merging roadmaps: {e}")
54
 
 
 
 
55
 
 
56