Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Create schedule from the given file."""
- import re
- def create_schedule_file(input_filename: str, output_filename: str) -> None:
- """
- Create schedule file from the given input file.
- :param input_filename:
- :param output_filename:
- :return:
- """
- with open(input_filename, "r", encoding="utf-8") as file_in:
- with open(output_filename, "w", encoding="utf-8") as file_out:
- spreadsheet = create_schedule_string(file_in.read())
- file_out.write(spreadsheet)
- def get_formatted_time(time: str) -> str:
- """
- Format 24 hour time to the 12 hour time.
- :param time:
- :return:
- """
- hours = int(time[:2])
- minutes = int(time[-2:])
- if hours < 12:
- ampm = "AM"
- if hours == 0:
- hours = 12
- else:
- ampm = "PM"
- if hours > 12:
- hours = hours - 12
- return f"{hours}:{minutes} {ampm}"
- def create_schedule_string(input_string: str) -> str:
- """
- Create schedule string from the given input string.
- :param input_string:
- :return:
- """
- words = {}
- for result in re.finditer(r"\s([0-9]{1,2})\D([0-9]{1,2}) +([a-zA-Z]+)", input_string):
- hours = int(result.group(1))
- minutes = int(result.group(2))
- word = result.group(3).lower()
- if 0 <= hours < 24 and 0 <= minutes < 60:
- time = f"{hours:02}:{minutes:02}"
- if time in words:
- if word not in words[time]:
- words[time].append(word)
- else:
- words[time] = [word]
- sorted_words = sorted(words.items(), key=lambda x: x[0])
- print(sorted_words)
- layout_words = []
- maximum_time = 0
- maximum_words = 0
- for words1 in sorted_words:
- layout_time = get_formatted_time(words1[0])
- string_words1 = ", ".join(words1[1])
- layout_words.append((layout_time, string_words1))
- maximum_words = max(maximum_words, len(string_words1))
- maximum_time = max(maximum_time, len(layout_time))
- spreadsheet = []
- spreadsheet.append("-" * (maximum_time + maximum_words + 7))
- spreadsheet.append(f"| {'time':>{maximum_time}} | {'items':<{maximum_words}} |")
- spreadsheet.append("-" * (maximum_time + maximum_words + 7))
- for words1 in layout_words:
- spreadsheet.append(f"| {words1[0]:>{maximum_time}} | {words1[1]:<{maximum_words}} |")
- spreadsheet.append("-" * (maximum_time + maximum_words + 7))
- print("\n".join(spreadsheet))
- return "\n".join(spreadsheet)
- if __name__ == '__main__':
- print(create_schedule_string("wat 11:00 teine tekst 11:0 jah ei 10:00 pikktekst "))
- create_schedule_file("schedule_input.txt", "schedule_output.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement