66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
import time
|
|
import json
|
|
import datetime
|
|
import difflib
|
|
|
|
|
|
class Utils:
|
|
def is_future_date(service_date):
|
|
"""
|
|
Converts service_date in format "Mon Day, Year" to epoch time
|
|
and compares it to today's date.
|
|
Args:
|
|
service_date (str): Date string in format "Mon Day, Year" (e.g., "Jun 15, 2025")
|
|
Returns:
|
|
bool: True if service_date is in the future, False otherwise
|
|
"""
|
|
try:
|
|
date_obj = datetime.datetime.strptime(service_date, "%B %d, %Y")
|
|
service_epoch = int(date_obj.timestamp())
|
|
current_epoch = int(time.time())
|
|
return service_epoch > current_epoch
|
|
except ValueError as e:
|
|
print(f"Error parsing date: {e}")
|
|
return True
|
|
|
|
def find_close_matches(word, possibilities, cutoff=0.4):
|
|
"""
|
|
Finds close matches for a word from a list of possibilities.
|
|
Args:
|
|
word (str): The word to find close matches for.
|
|
possibilities (list): A list of possible matches.
|
|
cutoff (float, optional): The minimum similarity ratio for a word to be considered a close match. Defaults to 0.6.
|
|
Returns:
|
|
list: A list of close matches.
|
|
"""
|
|
return difflib.get_close_matches(word, possibilities, cutoff=cutoff)
|
|
|
|
def load_json_from_file(file_path):
|
|
"""
|
|
Reads and loads JSON data from a file.
|
|
|
|
Args:
|
|
file_path (str): Path to the JSON file
|
|
|
|
Returns:
|
|
dict/list: Parsed JSON data
|
|
|
|
Raises:
|
|
FileNotFoundError: If the specified file doesn't exist
|
|
json.JSONDecodeError: If the file contains invalid JSON
|
|
"""
|
|
try:
|
|
with open(file_path, 'r') as file:
|
|
data = json.load(file)
|
|
return data
|
|
except FileNotFoundError:
|
|
print(f"Error: File '{file_path}' not found")
|
|
raise
|
|
except json.JSONDecodeError as e:
|
|
print(f"Error: Invalid JSON format in file '{file_path}': {str(e)}")
|
|
raise
|
|
except Exception as e:
|
|
print(f"Error: An unexpected error occurred: {str(e)}")
|
|
raise
|
|
|