import json
import datetime
def aggregate_user_input(input_file, schedule_file):
"""
Aggregates user input from an input file based on a schedule defined in a schedule file.
Handles edge cases like missing data, invalid dates, and non-numeric input.
Args:
input_file (str): Path to the file containing user input data (JSON format).
schedule_file (str): Path to the file containing the schedule (JSON format).
Returns:
dict: A dictionary containing aggregated data, where keys are schedule entries
and values are dictionaries of aggregated user inputs.
Returns an empty dictionary if errors occur.
"""
try:
with open(input_file, 'r') as f:
user_data = json.load(f)
with open(schedule_file, 'r') as f:
schedule = json.load(f)
except FileNotFoundError as e:
print(f"Error: File not found: {e}")
return {}
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON format: {e}")
return {}
aggregated_data = {}
for schedule_entry in schedule:
schedule_time = datetime.datetime.strptime(schedule_entry['time'], '%Y-%m-%d %H:%M:%S') #parse schedule time
aggregated_data[schedule_entry['id']] = {} #initialize dictionary for each schedule entry
for user_id, input_value in user_data.items():
try:
#check if user_id exists in the scheduled entries
if user_id in schedule_entry['users']:
#check if the date matches the schedule time
if datetime.datetime.strptime(user_id['date'], '%Y-%m-%d') == schedule_time.date():
aggregated_data[schedule_entry['id']][user_id['key']] = float(input_value) #convert input to float
except (ValueError, TypeError) as e:
print(f"Warning: Invalid data for user {user_id}: {e}")
continue # Skip to the next user
return aggregated_data
Add your comment