我创建了一个Python脚本,用于构建日历(.xlsx
文件),并指派人员重写和改革我的大学教授的课程。
然而,代码相当“罗嗦”,而且几乎可以肯定,没有优化。我该怎么办?你有什么建议(包括我可以阅读的任何文字以增加我的知识)吗?
这是我的剧本:
import xlsxwriter
#use xlsxwriter functions to initializate\create .xlsx file
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
#Defining start informations
day = 24 #first day of the calendar
nday = 1 #nday stands for the week day (1=monday, ..., 7=sunday)
month = 10 #month number
trentuno = [1, 3, 5, 7, 8, 10, 12] #array that defines month that have 31 days
settimana = [1, 2, 3, 4 , 5] #array that defines working days
i=0
f=open('test.txt') #inside this file we have the names (one per line) of people that is going to be in the calendar
lines=f.readlines()
array = [] #void array that is going to be "the excel table"
array.append(["data", "ora", "sbobinatore", "controllore"])
festa = ["1/11", "8/12", "26/12", "27/12", "28/12", "29/12", "30/12", "2/1", "3/1", "4/1", "5/1", "6/1"] #holidays
due = ["9/1", "10/1", "11/1", "12/1", "13/1", "16/1", "17/1", "18/1", "19/1", "20/1"] #days with only 2h lessons
while i < len(lines):
data = "%d/%d" % (day,month)
if nday in settimana and not data in festa:
ora = "[9:00-11:00]"
sbobinatore1 = lines[i] # "sbobinatore" is the person who writes the lesson
controllore1 = lines[i+1] # "controllore" is the person who check the rewrited lesson
array.append([data, ora, sbobinatore1, controllore1])
i = i+2
ora2 = "[11:00-13:00]"
sbobinatore2 = lines[i]
controllore2 = lines[i+1]
array.append([data, ora, sbobinatore2, controllore2])
i = i+2
if month in trentuno:
if day == 31:
day = 1
nday = nday+1
month = month+1
else:
day = day+1
nday = nday+1
elif month == 2:
if day == 28:
day = 1
month = month+1
nday = nday+1
else:
day = day+1
nday = nday+1
else:
if day == 30:
day = 1
month = month+1
nday = nday+1
else:
day = day+1
nday = nday+1
#if the day is not a working day
else:
if month in trentuno:
if day == 31:
day = 1
if nday == 7:
nday = 1
else:
nday = nday+1
month = month+1
else:
day = day+1
if nday == 7:
nday = 1
else:
nday = nday+1
elif month == 2:
if day == 28:
day = 1
month = month+1
if nday == 7:
nday = 1
else:
nday = nday+1
else:
day = day+1
if nday == 7:
nday = 1
else:
nday = nday+1
else:
if day == 30:
day = 1
month = month+1
if nday == 7:
nday = 1
else:
nday = nday+1
else:
day = day+1
if nday == 7:
nday = 1
else:
nday = nday+1
#repeating the while with role invertion
i=0
while i < len(lines):
data = "%d/%d" % (day,month)
if nday in settimana and not data in festa:
ora = "[9:00-11:00]"
sbobinatore1 = lines[i+1]
controllore1 = lines[i]
array.append([data, ora, sbobinatore1, controllore1])
i = i+2
ora2 = "[11:00-13:00]"
sbobinatore2 = lines[i+1]
controllore2 = lines[i]
array.append([data, ora, sbobinatore2, controllore2])
i = i+2
if month in trentuno:
if day == 31:
day = 1
nday = nday+1
month = month+1
else:
day = day+1
nday = nday+1
elif month == 2:
if day == 28:
day = 1
month = month+1
nday = nday+1
else:
day = day+1
nday = nday+1
else:
if day == 30:
day = 1
month = month+1
nday = nday+1
else:
day = day+1
nday = nday+1
#if the day is not a working day or holiday
else:
if month in trentuno:
if day == 31:
day = 1
if nday == 7:
nday = 1
else:
nday = nday+1
month = month+1
else:
day = day+1
if nday == 7:
nday = 1
else:
nday = nday+1
elif month == 2:
if day == 28:
day = 1
month = month+1
if nday == 7:
nday = 1
else:
nday = nday+1
else:
day = day+1
if nday == 7:
nday = 1
else:
nday = nday+1
else:
if day == 30:
day = 1
month = month+1
if nday == 7:
nday = 1
else:
nday = nday+1
else:
day = day+1
if nday == 7:
nday = 1
else:
nday = nday+1
#sight check
print(array)
#defining starting point for the creation of the table inside the xlsx worksheet
row = 0
col = 0
#actual creation of the table
for a, b, c, d in (array):
worksheet.write(row, col, a)
worksheet.write(row, col + 1, b)
worksheet.write(row, col + 2, c)
worksheet.write(row, col + 3, d)
row += 1
workbook.close()