Skip to content

Commit 55d26c9

Browse files
Feat: Added Itinerary class (itinerary.py) and itinerary serialization (file_handler.py).
1 parent 4cf4d44 commit 55d26c9

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pickle
2+
import os
3+
from pprint import pprint
4+
from src.itinerary import Itinerary
5+
6+
ITINERARY_FILE = "itineraries.bin"
7+
8+
9+
def load_itineraries():
10+
"""
11+
Load tasks from a binary file using the pickle module.
12+
13+
Returns:
14+
dictionary: A dictionary of Itinerary objects loaded from the binary file.
15+
If the file does not exist, an empty dictionary is returned.
16+
"""
17+
if os.path.exists(ITINERARY_FILE):
18+
with open(ITINERARY_FILE, "rb") as file:
19+
return pickle.load(file)
20+
return {}
21+
22+
23+
def save_itineraries(itineraries):
24+
"""
25+
Save a list of itineraries to a binary file using the pickle module.
26+
27+
Args:
28+
itineraries (dict): A dictionary containing Itinerary objects to save.
29+
30+
Side Effects:
31+
- Writes the serialized itinerary dictionary to ITINERARY_FILE.
32+
- Overwrites the file if it already exists.
33+
"""
34+
with open(ITINERARY_FILE, "wb") as file:
35+
pickle.dump(itineraries, file)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Itinerary:
2+
"""
3+
This class represents a single itinerary object that users can create.
4+
5+
Args:
6+
name (str): The name assigned to the itinerary.
7+
location (str): The main city/country the holiday takes place.
8+
summary (str, optional): A brief summary of the travel plan.
9+
start_date (str): The date the holiday begins in 'DD-MM-YYYY' format.
10+
end_date (str): The date the holiday ends in 'DD-MM-YYYY' format.
11+
flights (dict): A nested dictionary type containing flights and flight details:
12+
flight_name (key, dict type): Contains the flight name (departure-arrival location format, e.g. 'Perth to Sydney')
13+
departure_airport (str): Departure airport name.
14+
departure_date (datetime): Date & time of flight departure in 'DD-MM-YYYY HH:MM' format.
15+
arrival_airport (str): Arrival airport name.
16+
arrival_date (datetime): Date & time of flight arrival in 'DD-MM-YYYY HH:MM' format.
17+
attractions (dict): Nested dictionary of attractions. Each dictionary key (name of attraction) contains:
18+
attraction_name (str, dict): Name of attraction (key) and a dictionary containing attraction details:
19+
address (str): Address of attraction.
20+
attraction_summary(str): Short description of attraction.
21+
attraction_tags (list): List of tags (str type) that catergorise the attraction.
22+
"""
23+
24+
def __init__(self, name, location, summary, start_date, end_date, flights, attractions):
25+
self.name = name
26+
self.location = location
27+
self.summary = summary
28+
self.start_date = start_date
29+
self.end_date = end_date
30+
self.flights = flights
31+
self.attractions = attractions
32+
pass
33+
34+
def to_dict(self):
35+
"""
36+
Converts the Itinerary object into a dictionary.
37+
38+
Returns:
39+
dict: A dictionary containing itinerary details with these keys:
40+
'name', 'location', 'summary', 'start-date', 'end-date', 'flights', and 'attractions'.
41+
"""
42+
return {
43+
"name": self.name,
44+
"location": self.location,
45+
"summary": self.summary,
46+
"start_date": self.start_date,
47+
"end_date": self.end_date,
48+
"flights": self.flights,
49+
"attractions": self.attractions
50+
}
51+
52+
@staticmethod
53+
def from_dict(itinerary_info):
54+
"""
55+
Creates an Itinerary object from the dictionary representation.
56+
57+
Args:
58+
itinerary_info (dict): A dictionary containing itinerary info with these keys:
59+
'name', 'location', 'summary', 'start-date', 'end-date', 'flights', and 'attractions'.
60+
61+
Returns:
62+
Itinerary: A new Itinerary object created from the dictionary data.
63+
"""
64+
# TODO: Re-write to accommodate the dictionary type, and load its attributes according to their type.
65+
# Reference that might help: https://stackoverflow.com/questions/56640436/how-to-generically-serialize-and-de-serialize-objects-from-dictionaries
66+
67+
return Itinerary(
68+
itinerary_info["name"],
69+
itinerary_info["location"],
70+
itinerary_info["summary"],
71+
itinerary_info["start-date"],
72+
itinerary_info["end_date"],
73+
itinerary_info["flights"],
74+
itinerary_info["attractions"]
75+
)

0 commit comments

Comments
 (0)