1. import datetime
  2. def decode_schedule(schedule_string):
  3. """
  4. Decodes a schedule string into a list of tuples representing scheduled runs.
  5. Each tuple contains the run time (datetime object) and the run identifier (string).
  6. Args:
  7. schedule_string: A string representing the schedule.
  8. Format: "run1:YYYY-MM-DD HH:MM:SS,run2:YYYY-MM-DD HH:MM:SS,..."
  9. Returns:
  10. A list of tuples, where each tuple is (datetime object, run_id).
  11. Returns an empty list if the input string is empty.
  12. """
  13. if not schedule_string:
  14. return []
  15. runs = []
  16. run_entries = schedule_string.split(',')
  17. for entry in run_entries:
  18. try:
  19. run_id, time_str = entry.split(':')
  20. run_id = run_id.strip()
  21. time_str = time_str.strip()
  22. run_time = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
  23. runs.append((run_time, run_id))
  24. except ValueError:
  25. print(f"Warning: Invalid schedule entry: {entry}. Skipping.") #Handle potential errors gracefully
  26. continue
  27. return runs
  28. if __name__ == '__main__':
  29. schedule = "run1:2024-01-01 10:00:00,run2:2024-01-02 14:30:00,invalid_entry"
  30. decoded_schedule = decode_schedule(schedule)
  31. print(decoded_schedule)
  32. schedule2 = ""
  33. decoded_schedule2 = decode_schedule(schedule2)
  34. print(decoded_schedule2)

Add your comment