Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. """Create schedule from the given file."""
  2. import re
  3.  
  4.  
  5. def create_schedule_file(input_filename: str, output_filename: str) -> None:
  6.     """
  7.    Create schedule file from the given input file.
  8.  
  9.    :param input_filename:
  10.    :param output_filename:
  11.    :return:
  12.    """
  13.     with open(input_filename, "r", encoding="utf-8") as file_in:
  14.         with open(output_filename, "w", encoding="utf-8") as file_out:
  15.             spreadsheet = create_schedule_string(file_in.read())
  16.             file_out.write(spreadsheet)
  17.  
  18.  
  19. def get_formatted_time(time: str) -> str:
  20.     """
  21.    Format 24 hour time to the 12 hour time.
  22.  
  23.    :param time:
  24.    :return:
  25.    """
  26.     hours = int(time[:2])
  27.     minutes = int(time[-2:])
  28.     if hours < 12:
  29.         ampm = "AM"
  30.         if hours == 0:
  31.             hours = 12
  32.     else:
  33.         ampm = "PM"
  34.         if hours > 12:
  35.             hours = hours - 12
  36.     return f"{hours}:{minutes} {ampm}"
  37.  
  38.  
  39. def create_schedule_string(input_string: str) -> str:
  40.     """
  41.    Create schedule string from the given input string.
  42.  
  43.    :param input_string:
  44.    :return:
  45.    """
  46.     words = {}
  47.     for result in re.finditer(r"\s([0-9]{1,2})\D([0-9]{1,2}) +([a-zA-Z]+)", input_string):
  48.         hours = int(result.group(1))
  49.         minutes = int(result.group(2))
  50.         word = result.group(3).lower()
  51.         if 0 <= hours < 24 and 0 <= minutes < 60:
  52.             time = f"{hours:02}:{minutes:02}"
  53.             if time in words:
  54.                 if word not in words[time]:
  55.                     words[time].append(word)
  56.             else:
  57.                 words[time] = [word]
  58.     sorted_words = sorted(words.items(), key=lambda x: x[0])
  59.     print(sorted_words)
  60.     layout_words = []
  61.     maximum_time = 0
  62.     maximum_words = 0
  63.     for words1 in sorted_words:
  64.         layout_time = get_formatted_time(words1[0])
  65.         string_words1 = ", ".join(words1[1])
  66.         layout_words.append((layout_time, string_words1))
  67.         maximum_words = max(maximum_words, len(string_words1))
  68.         maximum_time = max(maximum_time, len(layout_time))
  69.  
  70.     spreadsheet = []
  71.     spreadsheet.append("-" * (maximum_time + maximum_words + 7))
  72.     spreadsheet.append(f"|  {'time':>{maximum_time}} | {'items':<{maximum_words}}  |")
  73.     spreadsheet.append("-" * (maximum_time + maximum_words + 7))
  74.     for words1 in layout_words:
  75.         spreadsheet.append(f"|  {words1[0]:>{maximum_time}} | {words1[1]:<{maximum_words}}  |")
  76.     spreadsheet.append("-" * (maximum_time + maximum_words + 7))
  77.     print("\n".join(spreadsheet))
  78.     return "\n".join(spreadsheet)
  79.  
  80.  
  81. if __name__ == '__main__':
  82.     print(create_schedule_string("wat 11:00 teine tekst 11:0 jah ei 10:00 pikktekst "))
  83.     create_schedule_file("schedule_input.txt", "schedule_output.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement