import datetime
def decode_schedule(schedule_string):
"""
Decodes a schedule string into a list of tuples representing scheduled runs.
Each tuple contains the run time (datetime object) and the run identifier (string).
Args:
schedule_string: A string representing the schedule.
Format: "run1:YYYY-MM-DD HH:MM:SS,run2:YYYY-MM-DD HH:MM:SS,..."
Returns:
A list of tuples, where each tuple is (datetime object, run_id).
Returns an empty list if the input string is empty.
"""
if not schedule_string:
return []
runs = []
run_entries = schedule_string.split(',')
for entry in run_entries:
try:
run_id, time_str = entry.split(':')
run_id = run_id.strip()
time_str = time_str.strip()
run_time = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
runs.append((run_time, run_id))
except ValueError:
print(f"Warning: Invalid schedule entry: {entry}. Skipping.") #Handle potential errors gracefully
continue
return runs
if __name__ == '__main__':
schedule = "run1:2024-01-01 10:00:00,run2:2024-01-02 14:30:00,invalid_entry"
decoded_schedule = decode_schedule(schedule)
print(decoded_schedule)
schedule2 = ""
decoded_schedule2 = decode_schedule(schedule2)
print(decoded_schedule2)
Add your comment