Purpose of current project is to get attendance to "Kiwi.com Python Weekend" in 2022 march 4-6.
Project focuses on develop a search script runable on terminal (without GUI) due to requirements (raised in https://github.com/kiwicom/python-weekend-entry-task).
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
At least Python 3.8.10 be installed on your computer.
Please visit python.org and download it via the link below.
https://www.python.org/downloads/
Only the standard libraries are necessary to execute the script, no need 3rd party packages, notebooks, specialized distros (Conda) etc. The code should run as is, no environment setup is required.
Extract the received ZIP file in case of getting it via email or downloading it from git repo in that format. Using (Ubuntu) Linux OS, such as:
unzip flight_search-master.zip
Extract the received ZIP file in case of using Windows OS command line:
tar -xf flight_search-master.zip
You have to get the following directory structure within flight_search-master folder:
โโโ example
โ โโโ example0.csv
โ โโโ example1.csv
โ โโโ example2.csv
โ โโโ example3.csv
โโโ modules
โ โโโ data_validation.py
โ โโโ flights.py
โ โโโ __init__.py
โโโ README.md
โโโ solution.py
example/*.csv
:
- Input files for testing purpose
solution.py
:
- It gets the parameters (mandatory & optionals - see in further subchapters below) from console and includes the execution function of the search-engine module, so called
flights
. It callssearch function
fromflights
module and validate the parameters got from terminal. If any of the mandatory or optional parameters is wrong the following error message retruns, without keep running:
# ERROR MESSAGE
One of the arguments is invalid!!
modules/flights.py
:
- It contains those functions which are necessary to derive proper search result(s):
- set_result --> writes the result set into
json_data.json
and to the terminal with the number of flights found. - get_traveltime --> converts string timestamp into ISO format and returns how much time the travel take.
- get_overlay --> calculates time between flight changes
- read_csv --> read csv input file and validate input data records according to definitions in
modules/data_validation.py
- get_flights --> based on search parameters, this function is the core-engine of the task. It calls
get_traveltime
andget_overlay
functions. - search --> calles direct or indirect way every function in this module by splitting the return and non-return searches.
modules/data_validation.py
:
- Functions of current module go through the atributes in input file and flag if any error would be detected:
- flight_no_checker --> returns error message if fligh number is not equal to 5 (supposing according to - example - input files this is the standard length) or its characters massed up.
# ERROR MESSAGE
Input file contains invalid flight number such as '[FLIGHT_NO]'!
- geo_checkor --> validates if airport codes correctness. If airport code is not 3 character long and it is not string following error message returns:
# ERROR MESSAGE
'[GEO_CODE]' is not a valid geo code to '[ARRIVAL / DEPARTURE]' airport. Please fix adequate field of input file!
- timestamp_checker --> handles error of incorrect format of timestamp regarding arrival / departure time
# ERROR MESSAGE
Sorry but date format of '[ARRIVAL / DEPARTURE TIMESTAMP]' is unacceptable in the field '[ARRIVAL / DEPARTURE TIME COLUMN]' of input file: '[VALUE ERROR MESSAGE]'
- integer_checker --> returns error message if called column of input file contains data type apart of integer. It validates string representation of integers.
# ERROR MESSAGE
[NUMBER] is not a number in '[COLUMNNAME]' field. Please fix adequate field of input file!
- float_checker --> regarding float number validation and error handling
# ERROR MESSAGE
'[NUMBER]' is not a number. Please fix adequate field of input file!
Step into the folder where solution.py
script is:
List of mandatory arguments of the solution python module:
Argument name | type | Description | Notes |
---|---|---|---|
filepath |
string | filepath of csv file, which has proper structure with all necessary input data (see an example file attached) | Mandatory |
origin |
string | Origin (departure) site where You would like to travel from | Mandatory |
destination |
string | Destination (arrival) site where You would like to travel to | Mandatory |
If you just want to run a search on airports code without any additional filter, run the following command:
python -m solution example/example3.csv BPZ WTN
There are two other optional argument which can be used to tune up Your search:
Argument name | type | Description | Notes |
---|---|---|---|
bags |
integer | Number of requested bags | Optional (defaults to 0) |
return |
boolean | Is it a return flight? | Optional (defaults to false) |
a) scenario, if you want to search for flights which allow at least one lagguage to take. NOTE: Changing flights take a look at what is the maxium number of bag can be taken because of "bag allowance". It can reduce your results. (e.g. your 1st flight allows 2 bags, but changing to the 2nd one it just allows 1)
python -m solution example/example3.csv BPZ WTN --bags=1
#OR
python -m solution example/example3.csv BPZ WTN -b 1
b) scenario, if you want to search return flights as well with one lagguage.
- NOTE: Take into account overlay limitations are also used in case of return flights and the lagguage limitation as well.
python -m solution example/example3.csv BPZ WTN --bags=1 --return
#OR
python -m solution example/example3.csv BPZ WTN -b 1 -r
After running the parameterized module, the search result will be printed to console and saved in json_data.json file in the current
directory.
Providing bags
with greater than 0 value, it will be utilized in case of flight changes, so it has to be less than or equal to smaller bag allowance limitation.
Providing return
argument, the result set will include the "ways there" and "return journeys" as well.
python -m solution example/example0.csv WIW ECV --bags=1 --return
RESULT SET is a json-compatible structured list of trips sorted by total_price
(ascending order alias cheapest is first):
[
{
"flights": [
{
"flight_no": "ZH151",
"origin": "WIW",
"destination": "ECV",
"departure": "2021-09-01T07:25:00",
"arrival": "2021-09-01T12:35:00",
"base_price": "245.0",
"bag_price": "12",
"bags_allowed": "2"
}
],
"bags_allowed": "2",
"bags_count": 1,
"destination": "ECV",
"origin": "WIW",
"total_price": 257.0,
"travel_time": "5:10:00"
},
{
"flights": [
{
"flight_no": "ZH151",
"origin": "WIW",
"destination": "ECV",
"departure": "2021-09-02T07:25:00",
"arrival": "2021-09-02T12:35:00",
"base_price": "245.0",
"bag_price": "12",
"bags_allowed": "2"
}
],
"bags_allowed": "2",
"bags_count": 1,
"destination": "ECV",
"origin": "WIW",
"total_price": 257.0,
"travel_time": "5:10:00"
},
{
"flights": [
{
"flight_no": "ZH151",
"origin": "WIW",
"destination": "ECV",
"departure": "2021-09-06T07:25:00",
"arrival": "2021-09-06T12:35:00",
"base_price": "245.0",
"bag_price": "12",
"bags_allowed": "2"
}
],
"bags_allowed": "2",
"bags_count": 1,
"destination": "ECV",
"origin": "WIW",
"total_price": 257.0,
"travel_time": "5:10:00"
},
{
"flights": [
{
"flight_no": "ZH151",
"origin": "WIW",
"destination": "ECV",
"departure": "2021-09-11T07:25:00",
"arrival": "2021-09-11T12:35:00",
"base_price": "245.0",
"bag_price": "12",
"bags_allowed": "2"
}
],
"bags_allowed": "2",
"bags_count": 1,
"destination": "ECV",
"origin": "WIW",
"total_price": 257.0,
"travel_time": "5:10:00"
},
{
"flights": [
{
"flight_no": "ZH151",
"origin": "ECV",
"destination": "WIW",
"departure": "2021-09-01T15:35:00",
"arrival": "2021-09-01T20:45:00",
"base_price": "245.0",
"bag_price": "12",
"bags_allowed": "2"
}
],
"bags_allowed": "2",
"bags_count": 1,
"destination": "WIW",
"origin": "ECV",
"total_price": 257.0,
"travel_time": "5:10:00"
},
{
"flights": [
{
"flight_no": "ZH151",
"origin": "ECV",
"destination": "WIW",
"departure": "2021-09-02T15:35:00",
"arrival": "2021-09-02T20:45:00",
"base_price": "245.0",
"bag_price": "12",
"bags_allowed": "2"
}
],
"bags_allowed": "2",
"bags_count": 1,
"destination": "WIW",
"origin": "ECV",
"total_price": 257.0,
"travel_time": "5:10:00"
},
{
"flights": [
{
"flight_no": "ZH151",
"origin": "ECV",
"destination": "WIW",
"departure": "2021-09-06T15:35:00",
"arrival": "2021-09-06T20:45:00",
"base_price": "245.0",
"bag_price": "12",
"bags_allowed": "2"
}
],
"bags_allowed": "2",
"bags_count": 1,
"destination": "WIW",
"origin": "ECV",
"total_price": 257.0,
"travel_time": "5:10:00"
},
{
"flights": [
{
"flight_no": "ZH151",
"origin": "ECV",
"destination": "WIW",
"departure": "2021-09-11T15:35:00",
"arrival": "2021-09-11T20:45:00",
"base_price": "245.0",
"bag_price": "12",
"bags_allowed": "2"
}
],
"bags_allowed": "2",
"bags_count": 1,
"destination": "WIW",
"origin": "ECV",
"total_price": 257.0,
"travel_time": "5:10:00"
}
]
- Ubuntu 20.04 LTS
- Python 3.8
- CSV file storage
- @stnorbi - Initial work
- @BusinessWatcher - Blog