Files
RalllyParser/ralllyparser.py

69 lines
2.6 KiB
Python

import csv
import sys
def find_common_dates(dicts, names):
# Strip whitespace from names in the provided list
stripped_names = [name.strip() for name in names]
# Filter dictionaries based on the stripped names
filtered_dicts = [d for d in dicts if d['Name'].strip() in stripped_names]
# Check for names not found in the dictionaries
dict_names = {d['Name'].strip() for d in dicts}
not_found_names = set(stripped_names) - dict_names
# Print warning for names not found
if not_found_names:
print(f"Warning: The following names were not found in the dictionaries: {', '.join(not_found_names)}")
# If no dictionaries match the provided names, return an empty list
if not filtered_dicts:
return []
# Extract all dates from the first dictionary (assuming all dictionaries have the same dates)
dates = set(filtered_dicts[0].keys()) - {'Name'}
# Initialize a set with all dates
common_dates = dates.copy()
# Iterate through each filtered dictionary
for d in filtered_dicts:
# Find dates where the value is either 'Yes' or 'If need be'
available_dates = {date for date in dates if d[date] in {'Yes', 'If need be'}}
# Intersect with common_dates to keep only the common ones
common_dates &= available_dates
return list(common_dates)
while True:
print("\nWould you like to search by name or date, or would you like to quit?")
pathswitch=input("?:")
print("\n")
if pathswitch.lower() == "date":
desiredate=input("Please enter the date (format EG Sun 21 Jul 2024):")
with open(sys.argv[1], 'r') as datesheet:
reader = csv.DictReader(datesheet)
for row in reader:
if desiredate not in row.keys():
print("Date not found, please check that the date entered matches the format and is in the date range")
continue
if row[desiredate] == "Yes" or row[desiredate] == "If need be":
print(row['Name'])
elif pathswitch.lower() == "name":
#print("Not yet implemented")
desirenames=input("Please enter full names as found in calendar separated by comma:\n?:").split(',')
with open(sys.argv[1], 'r') as datesheet:
reader = csv.DictReader(datesheet)
listconv=list(reader)
dates = find_common_dates(listconv,desirenames)
for date in dates:
print(date)
elif pathswitch.lower() == "quit":
break
else:
print("Please enter either the word 'name', 'date', or quit")