Advertisement
Guest User

main.py

a guest
Aug 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.84 KB | None | 0 0
  1. #! python3
  2. """
  3. XLSX-SCHEDULE MAKER
  4.  
  5. This program is processes schedule text from txt file and puts it into xlsx file
  6.  
  7. MODULES USED: re, openpyxl, datetime
  8.  
  9. PLAN:
  10. step1 - read schedule from the file
  11. step2 - analyze schedule with re and datetime, find the first day date
  12. step3 - create data structure Schedule to easily use it later
  13. step4 - import data into xlsx file
  14. """
  15.  
  16. from pprint import pprint as pp
  17. import re
  18. import datetime
  19. import Schedule
  20. # import openpyxl
  21.  
  22.  
  23. def import_from_file(directory):
  24.     """
  25.    function reads file and assigns its contents to variable schedule
  26.    :param directory: str
  27.    :return schedule: str
  28.    """
  29.     txt = open(directory, "r")
  30.     schedule = txt.read()
  31.     txt.close()
  32.     return schedule
  33.  
  34.  
  35. def extract_info(data):
  36.     # splitting schedule into sections (0 - general info, days: 1-5)
  37.     structure = re.split('-{2,}\n', data)
  38.  
  39.     schedule_info = get_header_info(structure[0])
  40.  
  41.     create_structure(schedule_info)
  42.  
  43.     n = split_to_periods(structure[2])
  44.  
  45.     # list of disciplines
  46.     # d = re.split('\n\* ', n[1])
  47.  
  48.     # k = re.split(r'\n(?=\d пара)', n[2])
  49.     # print(k)
  50.  
  51.  
  52. def get_header_info(data):
  53.     """
  54.    function extracts data from header using regex module
  55.    :param data: text from header
  56.    :return tuple: group, year, semester, start_date
  57.    """
  58.     group = re.search(re.compile(r'(?<=Група )\w{2,3}(-\d{2})*'), data).group()
  59.     year = re.search(re.compile(r'\d{4}-\d{4} н.р.'), data).group()
  60.     semester = 1 if re.search(re.compile(r'\w+(?= семестр)'), data).group() is 'І' else 2
  61.     first_monday = re.search(re.compile(r'\d{2}.\d{2} '), data).group()[:-1]
  62.     start_date = datetime.date(int(year[0:4]) if semester == 1 else int(year[5:9]),
  63.                                int(first_monday[3:]), int(first_monday[:2]))
  64.     print('Extracting header data...\ngroup: {}\nyear: {}\nsemester: {}\nfirst week monday date: {}'
  65.           .format(group, year, semester, start_date))
  66.     return group, year, semester, start_date
  67.  
  68.  
  69. def split_to_periods(day):
  70.     # function for splitting weekday into periods
  71.     return re.split('\n(?=\d пара)', day)
  72.  
  73.  
  74. def get_disciplines(period):
  75.     """
  76.  
  77.    :param period: text containing info about all disciplines of this period
  78.    :return: list with information about each discipline
  79.    """
  80.  
  81.  
  82. def create_structure(semester_info):
  83.     schedule = Schedule.Schedule  # creating object Schedule
  84.     # adding info
  85.     schedule.group = semester_info[0]
  86.     schedule.year = semester_info[1]
  87.     schedule.semester = semester_info[2]
  88.     schedule.start = semester_info[3]
  89.     cont = schedule.weeks
  90.     # week dates
  91.     week_shift = datetime.timedelta(days=7)
  92.     curr_date = schedule.start
  93.     for i in range(17):
  94.         curr_w = cont[i]
  95.         for j in range(5):
  96.             curr_w
  97.  
  98.     return schedule
  99.  
  100.  
  101.  
  102.  
  103.  
  104. """
  105. Program
  106. """
  107.  
  108. direct = "GІР.txt"
  109. dat = import_from_file(direct)
  110. extract_info(dat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement