utils
The code contained in utils.py has three functions that are related to date and time calculations, as well as mapping English proficiency levels to numeric values.
1. is_today_in_range(response)
Purpose: This function checks if today's date falls within the range of an arrival and departure date specified in a response dictionary.
Steps:
- The function extracts the
arrivalanddeparturedates from theresponsedictionary (response['result']['data']['arrival']andresponse['result']['data']['departure']). - It assumes the dates are in the format
DD/MM/YYYY HH:MM, and it removes any extra characters (such as(timezone)or other metadata) from the string by splitting it at the first parenthesis. - The
arrivalanddeparturestrings are then parsed intodatetimeobjects usingdatetime.strptime()with the format'%d/%m/%Y %H:%M'. - The function gets the current date and time using
datetime.now(). - Finally, it checks whether today's date is between the
arrival_dateanddeparture_date(inclusive). It returnsTrueif today is within this range, otherwise, it returnsFalse.
Example:
If today's date is between the extracted arrival and departure dates, it will return True, otherwise False.
2. getLastMonday()
Purpose: This function calculates the most recent Monday relative to today.
Steps:
- The function gets the current date and time using
datetime.now(). - It then calculates the most recent Monday by subtracting the number of days that have passed since the last Monday. This is done using
today.weekday(), where Monday is0, Tuesday is1, and so on. timedelta(days=today.weekday())returns atimedeltaobject that represents how many days to subtract from today to get the last Monday.- The result is the
datetimeobject representing the most recent Monday.
Example:
If today is Thursday, today.weekday() would be 3, and today - timedelta(days=3) would give you the previous Monday's date.
3. englishClassToValue(english_class)
Purpose: This function maps an English proficiency class (represented as a string constant from the EnglishLevels class) to its corresponding numeric value (defined in the EnglishLevelsValues class).
Steps:
- A dictionary
level_to_valueis created that maps each string constant fromEnglishLevelsto the corresponding numeric value fromEnglishLevelsValues. - The function looks up the
english_classstring in thelevel_to_valuedictionary. - If the
english_classexists in the dictionary, the corresponding value is returned. If it doesn't exist, the function returnsNone.
Example:
- If
english_classisEnglishLevels.A1, the function will returnEnglishLevelsValues.A1. - If an unrecognized class is passed, such as a string that is not in the
EnglishLevelsconstants, the function will returnNone.
Summary
is_today_in_rangechecks if today's date falls between two dates (arrival and departure) in a response object.getLastMondayfinds the most recent Monday relative to the current date.englishClassToValueconverts an English proficiency level (from theEnglishLevelsenum) to a numeric value.