Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. """Create schedule from the given file."""
  2. import re
  3.  
  4.  
  5. def create_schedule_string(input_string: str) -> str:
  6. """Create schedule string from the given input string."""
  7. results_list = []
  8. for match in re.finditer(r"((\d+)[^\d](\d+))(\s+(\w+))", input_string):
  9. if len(match[2]) < 3 and len(match[3]) < 3:
  10. hours = int(match[2])
  11. minutes = int(match[3])
  12. if 0 <= hours <= 24 and 0 <= minutes < 61:
  13. hours = hours * 60 + minutes
  14. item = match[5].lower()
  15. results_list.append(hours)
  16. results_list.append(item)
  17. schedule_string = str(' '.join(str(i) for i in results_list))
  18. return schedule_string
  19.  
  20.  
  21. def create_sorted_schedule_dict(input_string: str) -> dict:
  22. """Create table."""
  23. schedule_string = create_schedule_string(input_string)
  24. schedule_dict = {}
  25. for match in re.finditer(r"(\d+)\s+(\w+)", schedule_string):
  26. minutes = int(match[1])
  27. if len(schedule_dict) < 0:
  28. schedule_dict[minutes] = str(match[2])
  29. else:
  30. if minutes in schedule_dict.keys():
  31. schedule_dict[minutes] = schedule_dict.get(minutes), match[2]
  32. else:
  33. schedule_dict[minutes] = match[2]
  34. # sorted_schedule_dict = sorted(schedule_dict)
  35. sorted_schedule_dict = {}
  36. for i in sorted(schedule_dict):
  37. sorted_schedule_dict.update({i: schedule_dict[i]})
  38. return sorted_schedule_dict
  39.  
  40.  
  41. def get_table(input_string: str) -> dict:
  42. """Get the maximum sizes for table."""
  43. sorted_schedule_dict = create_sorted_schedule_dict(input_string)
  44. table = []
  45. if len(sorted_schedule_dict) == 0:
  46. table.append('------------------')
  47. table.append('| time | items |')
  48. table.append('------------------')
  49. table.append('| No items found |')
  50. table.append('------------------')
  51. length = 0
  52. for item in sorted_schedule_dict.items():
  53. if len(str(sorted_schedule_dict.get(item))) > length:
  54. length = len(str(sorted_schedule_dict.items()))
  55. mark = '-'
  56. table.append(mark * length)
  57. b = ' ' * (length - 20)
  58. table.append(f" Time | Item{b}")
  59. table.append(mark * length)
  60. for a in range(len(sorted_schedule_dict)):
  61. line = sorted_schedule_dict.popitem()
  62. table.append(f"{line}")
  63. table.append(mark * length)
  64. return table
  65.  
  66.  
  67. # def normalize(...):
  68. """Add missing 0's to the minutes and remove extra 0's from hours."""
  69.  
  70.  
  71. # def get_formatted_time(...):
  72. """Format 24 hour time to the 12 hour time."""
  73.  
  74.  
  75. def create_schedule_file(input_filename: str, output_filename: str) -> None:
  76. """Create schedule file from the given input file."""
  77. with open(input_filename, 'r') as file_object:
  78. string_from_file = file_object.read().splitlines()
  79. string = str(' '.join(str(i) for i in string_from_file))
  80. table = get_table(string)
  81. with open(output_filename, 'w') as file:
  82. for line in table:
  83. file.write(line + '\n')
  84. pass
  85.  
  86.  
  87. if __name__ == '__main__':
  88. print(create_schedule_string("wat 11:00 teine tekst 11:0 jah ei 10:00 pikktekst "))
  89. create_schedule_file("schedule_input.txt", "schedule_output.txt")
  90. print(create_sorted_schedule_dict("wat 11:00 teine tekst 11:0 jah ei 10:00 pikktekst "))
  91. 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