89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
import time
|
|
import json
|
|
import datetime
|
|
import difflib
|
|
|
|
|
|
class Utils:
|
|
not_close = []
|
|
def __init__(self):
|
|
self.not_close_matches = []
|
|
|
|
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.
|
|
"""
|
|
try:
|
|
x = difflib.get_close_matches(word, possibilities, cutoff=cutoff)
|
|
print(word)
|
|
if x == []:
|
|
poss_acronyms = []
|
|
type_dropped = word[:-4]
|
|
for i in possibilities:
|
|
acronym = i.split(' ')
|
|
poss_acronyms.append(''.join([item[0] for item in acronym]))
|
|
if difflib.get_close_matches(type_dropped, poss_acronyms, n=1, cutoff=0) != []:
|
|
close_match = word
|
|
else:
|
|
close_match = difflib.get_close_matches(word, possibilities, cutoff=cutoff)
|
|
close_match = word
|
|
except Exception as e:
|
|
print("Close Matches Error")
|
|
print(e)
|
|
finally:
|
|
return close_match
|
|
|
|
|
|
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
|
|
|