Skip to main content

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 arrival and departure dates from the response dictionary (response['result']['data']['arrival'] and response['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 arrival and departure strings are then parsed into datetime objects using datetime.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_date and departure_date (inclusive). It returns True if today is within this range, otherwise, it returns False.

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 is 0, Tuesday is 1, and so on.
  • timedelta(days=today.weekday()) returns a timedelta object that represents how many days to subtract from today to get the last Monday.
  • The result is the datetime object 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_value is created that maps each string constant from EnglishLevels to the corresponding numeric value from EnglishLevelsValues.
  • The function looks up the english_class string in the level_to_value dictionary.
  • If the english_class exists in the dictionary, the corresponding value is returned. If it doesn't exist, the function returns None.

Example:

  • If english_class is EnglishLevels.A1, the function will return EnglishLevelsValues.A1.
  • If an unrecognized class is passed, such as a string that is not in the EnglishLevels constants, the function will return None.

Summary

  • is_today_in_range checks if today's date falls between two dates (arrival and departure) in a response object.
  • getLastMonday finds the most recent Monday relative to the current date.
  • englishClassToValue converts an English proficiency level (from the EnglishLevels enum) to a numeric value.