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_string(input_string: str) -> str:
- """Create schedule string from the given input string."""
- results_list = []
- for match in re.finditer(r"((\d+)[^\d](\d+))(\s+(\w+))", input_string):
- if len(match[2]) < 3 and len(match[3]) < 3:
- hours = int(match[2])
- minutes = int(match[3])
- if 0 <= hours <= 24 and 0 <= minutes < 61:
- hours = hours * 60 + minutes
- item = match[5].lower()
- results_list.append(hours)
- results_list.append(item)
- schedule_string = str(' '.join(str(i) for i in results_list))
- return schedule_string
- def create_sorted_schedule_dict(input_string: str) -> dict:
- """Create table."""
- schedule_string = create_schedule_string(input_string)
- schedule_dict = {}
- for match in re.finditer(r"(\d+)\s+(\w+)", schedule_string):
- minutes = int(match[1])
- if len(schedule_dict) < 0:
- schedule_dict[minutes] = str(match[2])
- else:
- if minutes in schedule_dict.keys():
- schedule_dict[minutes] = schedule_dict.get(minutes), match[2]
- else:
- schedule_dict[minutes] = match[2]
- # sorted_schedule_dict = sorted(schedule_dict)
- sorted_schedule_dict = {}
- for i in sorted(schedule_dict):
- sorted_schedule_dict.update({i: schedule_dict[i]})
- return sorted_schedule_dict
- def get_table(input_string: str) -> dict:
- """Get the maximum sizes for table."""
- sorted_schedule_dict = create_sorted_schedule_dict(input_string)
- table = []
- if len(sorted_schedule_dict) == 0:
- table.append('------------------')
- table.append('| time | items |')
- table.append('------------------')
- table.append('| No items found |')
- table.append('------------------')
- length = 0
- for item in sorted_schedule_dict.items():
- if len(str(sorted_schedule_dict.get(item))) > length:
- length = len(str(sorted_schedule_dict.items()))
- mark = '-'
- table.append(mark * length)
- b = ' ' * (length - 20)
- table.append(f" Time | Item{b}")
- table.append(mark * length)
- for a in range(len(sorted_schedule_dict)):
- line = sorted_schedule_dict.popitem()
- table.append(f"{line}")
- table.append(mark * length)
- return table
- # def normalize(...):
- """Add missing 0's to the minutes and remove extra 0's from hours."""
- # def get_formatted_time(...):
- """Format 24 hour time to the 12 hour time."""
- def create_schedule_file(input_filename: str, output_filename: str) -> None:
- """Create schedule file from the given input file."""
- with open(input_filename, 'r') as file_object:
- string_from_file = file_object.read().splitlines()
- string = str(' '.join(str(i) for i in string_from_file))
- table = get_table(string)
- with open(output_filename, 'w') as file:
- for line in table:
- file.write(line + '\n')
- pass
- 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")
- print(create_sorted_schedule_dict("wat 11:00 teine tekst 11:0 jah ei 10:00 pikktekst "))
- print(get_table("wat 11:00 teine tekst 11:0 jah ei 10:00 pikktekst "))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement