SHARE
TWEET

Untitled

a guest Oct 15th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """Create schedule from the given file."""
  2. import re
  3.  
  4.  
  5. def get_table_size(table):
  6.     sizes = []
  7.     for i in table:
  8.         sizes.append(len(i))
  9.     x = max(sizes) + 1
  10.     return x, sizes
  11.  
  12.  
  13. def create_table(dict1, keys):
  14.     table = []
  15.     new_keys = []
  16.     for i in keys:
  17.         if i not in new_keys:
  18.             new_keys.append(i)
  19.     for i in range(len(new_keys)):
  20.         table.append(new_keys[i] + " |")
  21.         for j in dict1[new_keys[i]][0][:-1]:
  22.             table[i] += " " + j + ","
  23.         table[i] += " " + dict1[new_keys[i]][0][-1]
  24.     return table
  25.  
  26.  
  27. def get_hour_min(keys, ):
  28.     new_keys = []
  29.     for i in keys:
  30.         if i == "0 AM":
  31.             new_keys.append("12:00")
  32.         else:
  33.             minutes = int(int(i[:3]) % 60)
  34.             hours = int((int(i[:3]) - minutes) / 60)
  35.             if hours == 0:
  36.                 hours = 12
  37.             if minutes < 10:
  38.                 new_keys.append(str(hours) + ":" + "0" + str(minutes))
  39.             else:
  40.                 new_keys.append(str(hours) + ":" + str(minutes))
  41.     for i in range(len(keys)):
  42.         if keys[i][-2:] == "AM":
  43.             new_keys[i] += "AM"
  44.         else:
  45.             new_keys[i] += "PM"
  46.     return new_keys
  47.  
  48.  
  49. def normalize_times(keys, values):
  50.     new_keys = []
  51.     new_keys2 = []
  52.     new_keys3 = []
  53.     for i in range(len(keys)):
  54.         for j in keys[i]:
  55.             if not j.isdigit():
  56.                 x = keys[i].replace(j, ":")
  57.                 keys.insert(i, x)
  58.                 del keys[i + 1]
  59.     for i in keys:
  60.         if i[-2] == ":":
  61.             new_keys.append(i[:-1] + "0" + i[-1])
  62.         else:
  63.             new_keys.append(i)
  64.     for i in new_keys:
  65.         if len(i) != 5:
  66.             new_keys2.append("0" + i)
  67.         else:
  68.             new_keys2.append(i)
  69.     for i in new_keys2:
  70.         x, y = i.split(":")
  71.         new_keys3.append(int(x) * 60 + int(y))
  72.     return new_keys3
  73.  
  74.  
  75. def normalize_keys2(new_keys3):
  76.     new_keys4 = []
  77.     new_keys3.sort()
  78.     for i in new_keys3:
  79.         if i > (12 * 60):
  80.             new_keys4.append(str(i - (12 * 60)) + " PM")
  81.         else:
  82.             new_keys4.append(str(i) + " AM")
  83.     return new_keys4
  84.  
  85.  
  86. def create_dict(keys, values):
  87.     dict1 = {}
  88.     for i in range(len(keys)):
  89.         dict1[keys[i]] = []
  90.     for i in range(len(keys)):
  91.         dict1[keys[i]].append(values[i])
  92.     return dict1
  93.  
  94.  
  95. def read_from_file(input_filename):
  96.     """Read the file."""
  97.     file_object = open(input_filename, 'r')  # Avame faili lugemiseks
  98.     results = file_object.readlines()
  99.     keys1 = ""
  100.     for i in range(len(results) - 1):
  101.         keys1 += (results[i][:-1])
  102.     keys1 += (results[-1])
  103.     return keys1
  104.  
  105.  
  106. def create_schedule_file(input_filename: str, output_filename: str) -> None:
  107.     """Create schedule file from the given input file."""
  108.     string = create_schedule_string(read_from_file(input_filename))
  109.     with open(output_filename, 'w', newline='') as file:
  110.         file.write(string)
  111.  
  112.  
  113. def create_schedule_string(input_string: str) -> str:
  114.     """Create schedule string from the given input string."""
  115.     results = ""
  116.     values = []
  117.     new_values = []
  118.     regex = r"(( )|^[0-9]|([1][0-9])|([2][0-3])(\D)([0-5][0-9])|[0-9]( )+[a-zA-Z]+)"
  119.     matches = re.finditer(regex, input_string, re.MULTILINE | re.UNICODE)
  120.     for i in matches:
  121.         if i.group(1) not in results:
  122.             results += i.group(1) + " "
  123.     if len(results) == 0:
  124.         return "------------------\n|  time | items  |\n------------------\n| No items found |\n------------------"
  125.     regex = r"([a-zA-Z]+)"
  126.     matches = re.finditer(regex, results, re.MULTILINE | re.UNICODE)
  127.     for i in matches:
  128.         values.append(i.group(1))
  129.     keys = []
  130.     regex = r"(( )|^[0-9]|([1][0-9])|([2][0-3])(\D)([0-5][0-9])|[0-9])"
  131.     matches = re.finditer(regex, results, re.MULTILINE | re.UNICODE)
  132.     for i in matches:
  133.         keys.append(i.group(1))
  134.     keys1 = normalize_times(keys, values)
  135.     dict1 = create_dict(keys1, values)
  136.     keys1.sort()
  137.     for i in keys1:
  138.         new_values.append(dict1[i])
  139.     dict2 = create_dict(get_hour_min(normalize_keys2(keys1)), new_values)
  140.     table = create_table(dict2, get_hour_min(normalize_keys2(keys1)))
  141.     final = ""
  142.     x, sizes = get_table_size(table)
  143.     start_end = "-" * (x + 4)
  144.     final += start_end + "\n"
  145.     final += "|" + " " * 4 + "time" + " |" + " items" + " " * (x - 14) + "|\n"
  146.     final += start_end + "\n"
  147.     for i in range(len(table)):
  148.         if table[i][1] == ":":
  149.             final += "|  " + table[i].lower() + " " * (x - 1 - sizes[i]) + " |\n"
  150.         else:
  151.             final += "| " + table[i].lower() + " " * (x - sizes[i]) + " |\n"
  152.     final += start_end + "\n"
  153.     return final
  154.  
  155.  
  156. if __name__ == '__main__':
  157.     print(create_schedule_string("wat 11:00 teine tekst 11:0 jah ei 10:00 pikktekst "))
  158.     create_schedule_file("schedule_input.txt", "schedule_output.txt")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top