-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight_parser.py
More file actions
331 lines (263 loc) · 9.23 KB
/
flight_parser.py
File metadata and controls
331 lines (263 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3
"""
Flight Schedule Parser and Query Tool
RTU Python assignment
"""
import argparse
import json
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List
# >>> CHANGE THESE TO YOUR REAL DATA <<<
STUDENT_ID = "241ADB008" # e.g. "12345"
FIRST_NAME = "Delwin"
LAST_NAME = "Biju"
DATE_FORMAT = "%Y-%m-%d %H:%M"
# Simple whitelist of airport codes based on the examples
VALID_AIRPORTS = {
"LHR",
"JFK",
"FRA",
"RIX",
"OSL",
"HEL",
"ARN",
"CDG",
"DXB",
"DOH",
"SYD",
"AMS",
"BRU",
"LAX",
}
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Flight schedule parser and query tool"
)
input_group = parser.add_mutually_exclusive_group()
input_group.add_argument(
"-i", "--input", metavar="FILE", help="Parse a single CSV file"
)
input_group.add_argument(
"-d", "--dir", metavar="DIR", help="Parse all .csv files in a folder"
)
parser.add_argument(
"-o",
"--output",
metavar="FILE",
help="Output JSON file for valid flights (default: db.json)",
)
parser.add_argument(
"-j",
"--json-db",
metavar="FILE",
help="Load existing JSON database instead of parsing CSVs",
)
parser.add_argument(
"-q", "--query", metavar="FILE", help="Execute queries from JSON file"
)
return parser
def parse_datetime(value: str, kind: str, errors: List[str]) -> datetime | None:
try:
return datetime.strptime(value, DATE_FORMAT)
except ValueError:
errors.append(f"invalid {kind} datetime")
return None
def validate_and_build_flight(
line: str,
lineno: int,
) -> tuple[Dict[str, Any] | None, str | None]:
"""
Validate one CSV line (already stripped, not empty, not comment).
Returns (flight_dict or None, error_message or None)
"""
parts = [p.strip() for p in line.split(",")]
if len(parts) != 6:
return None, f"Line {lineno}: {line} \u2192 missing required fields"
flight_id, origin, destination, dep_str, arr_str, price_str = parts
issues: List[str] = []
# flight_id: 2–8 alphanumeric
if not (2 <= len(flight_id) <= 8 and flight_id.isalnum()):
if len(flight_id) > 8:
issues.append("flight_id too long (more than 8 characters)")
elif len(flight_id) < 2:
issues.append("flight_id too short (less than 2 characters)")
else:
issues.append("invalid flight_id")
# origin and destination codes
def check_airport(code: str, kind: str) -> None:
if not (len(code) == 3 and code.isalpha() and code.isupper()):
issues.append(f"invalid {kind} code")
elif code not in VALID_AIRPORTS:
issues.append(f"invalid {kind} code")
check_airport(origin, "origin")
check_airport(destination, "destination")
# datetimes
dt_issues: List[str] = []
dep_dt = parse_datetime(dep_str, "departure", dt_issues)
arr_dt = parse_datetime(arr_str, "arrival", dt_issues)
issues.extend(dt_issues)
if dep_dt and arr_dt and arr_dt <= dep_dt:
issues.append("arrival before departure")
# price
try:
price = float(price_str)
if price < 0:
issues.append("negative price value")
elif price == 0:
issues.append("non-positive price value")
except ValueError:
price = None
issues.append("invalid price value")
if issues:
return None, f"Line {lineno}: {line} \u2192 " + ", ".join(issues)
flight: Dict[str, Any] = {
"flight_id": flight_id,
"origin": origin,
"destination": destination,
"departure_datetime": dep_str,
"arrival_datetime": arr_str,
"price": price,
}
return flight, None
def parse_csv_file(
path: Path,
valid_flights: List[Dict[str, Any]],
error_lines: List[str],
) -> None:
with path.open(encoding="utf-8") as f:
for lineno, raw_line in enumerate(f, start=1):
line = raw_line.rstrip("\n")
stripped = line.strip()
# ignore blank lines
if not stripped:
continue
# header line
if lineno == 1 and stripped.lower().startswith(
"flight_id,origin,destination,departure_datetime,arrival_datetime,price"
):
continue
# comment lines -> go to errors.txt as in example
if stripped.startswith("#"):
error_lines.append(
f"Line {lineno}: {stripped} \u2192 comment line, ignored for data parsing"
)
continue
# real data line
flight, err = validate_and_build_flight(line, lineno)
if err:
error_lines.append(err)
elif flight:
valid_flights.append(flight)
def save_json_db(flights: List[Dict[str, Any]], path: Path) -> None:
with path.open("w", encoding="utf-8") as f:
json.dump(flights, f, indent=2)
print(f"Saved {len(flights)} valid flights to {path}")
def save_errors(error_lines: List[str], path: Path) -> None:
with path.open("w", encoding="utf-8") as f:
for line in error_lines:
f.write(line + "\n")
print(f"Saved {len(error_lines)} error lines to {path}")
def load_json_db(path: Path) -> List[Dict[str, Any]]:
with path.open(encoding="utf-8") as f:
data = json.load(f)
if not isinstance(data, list):
raise ValueError("JSON DB must be an array of flight objects")
return data
def load_queries(path: Path) -> List[Dict[str, Any]]:
with path.open(encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict):
return [data]
if isinstance(data, list):
return data
raise ValueError("Query JSON must be an object or an array of objects")
def filter_flights(
flights: List[Dict[str, Any]],
query: Dict[str, Any],
) -> List[Dict[str, Any]]:
matches: List[Dict[str, Any]] = []
dep_filter_dt = None
arr_filter_dt = None
if "departure_datetime" in query:
dep_filter_dt = datetime.strptime(query["departure_datetime"], DATE_FORMAT)
if "arrival_datetime" in query:
arr_filter_dt = datetime.strptime(query["arrival_datetime"], DATE_FORMAT)
for flight in flights:
ok = True
# exact matches
for field in ("flight_id", "origin", "destination"):
if field in query and flight.get(field) != query[field]:
ok = False
break
if not ok:
continue
# departure_datetime >= query value
if dep_filter_dt is not None:
flight_dep = datetime.strptime(flight["departure_datetime"], DATE_FORMAT)
if flight_dep < dep_filter_dt:
continue
# arrival_datetime <= query value
if arr_filter_dt is not None:
flight_arr = datetime.strptime(flight["arrival_datetime"], DATE_FORMAT)
if flight_arr > arr_filter_dt:
continue
# price <= query value
if "price" in query:
if float(flight["price"]) > float(query["price"]):
continue
matches.append(flight)
return matches
def run_queries_and_save(
flights: List[Dict[str, Any]],
queries: List[Dict[str, Any]],
) -> Path:
responses: List[Dict[str, Any]] = []
for q in queries:
matches = filter_flights(flights, q)
responses.append(
{
"query": q,
"matches": matches,
}
)
timestamp = datetime.now().strftime("%Y%m%d_%H%M")
fname = FIRST_NAME.replace(" ", "")
lname = LAST_NAME.replace(" ", "")
filename = f"response_{STUDENT_ID}_{fname}_{lname}_{timestamp}.json"
path = Path(filename)
with path.open("w", encoding="utf-8") as f:
json.dump(responses, f, indent=2)
print(f"Saved query responses to {path}")
return path
def main() -> None:
parser = build_parser()
args = parser.parse_args()
has_csv_input = bool(args.input or args.dir)
if not has_csv_input and not args.json_db:
parser.error("You must provide either -j or one of -i / -d")
if args.json_db and has_csv_input:
parser.error("Use -j OR -i/-d, not both at the same time")
flights: List[Dict[str, Any]] = []
# --- Mode 1: parse CSVs ---
if has_csv_input:
errors: List[str] = []
if args.input:
parse_csv_file(Path(args.input), flights, errors)
if args.dir:
folder = Path(args.dir)
for csv_path in sorted(folder.glob("*.csv")):
parse_csv_file(csv_path, flights, errors)
output_path = Path(args.output) if args.output else Path("db.json")
save_json_db(flights, output_path)
save_errors(errors, Path("errors.txt"))
# --- Mode 2: load existing JSON db ---
if args.json_db:
flights = load_json_db(Path(args.json_db))
print(f"Loaded {len(flights)} flights from {args.json_db}")
# --- Optional: run queries ---
if args.query:
queries = load_queries(Path(args.query))
run_queries_and_save(flights, queries)
if __name__ == "__main__":
main()