最近没写平台脚本,开源一个自己写的检测排课冲突
本帖最后由 xhtdtk 于 2021-6-5 22:35 编辑import xlrd
import re
import time
set_termstart='2021-03-01'#设置课程该范围时间的开始,若没有特殊要求,可以设置为学期初
set_termend='2021-07-04'#设置课程该范围时间的结束,若没有特殊要求,可以设置为学期末
set_course='软件工程'#设置需要可安排时间的课程
chat_freetime=''#可以设置星期一晚上、星期二晚上等,不设置默认全部
def basedata():#存储基础数据
#打开课程安排表,打开工作表
data_main=xlrd.open_workbook(r'C:\Users\Administrator\Desktop\xxxx大学排课\主要-开课课程计划.xlsx')
table_main=data_main.sheet_by_name('Sheet2')
#字典存储各课程上课班级
dict_CourseListClass={}
for table_main_row in range(1,table_main.nrows):
dict_CourseListClass=[]
for table_main_row in range(1,table_main.nrows):
dict_CourseListClass.append(table_main.cell_value(table_main_row,1))
#print(dict_CourseListClass)
#字典存储各课程上课老师
dict_CourseListTeacher={}
for table_main_row in range(1,table_main.nrows):
dict_CourseListTeacher=[]
for table_main_row in range(1,table_main.nrows):
dict_CourseListTeacher.append(table_main.cell_value(table_main_row,5))
#print(dict_CourseListTeacher)
return dict_CourseListClass,dict_CourseListTeacher
def return_allformday(table_allform_day):#总表日期
day=time.strftime('%Y-%m-%d', time.localtime((table_allform_day-25569) * 86400.0))
allformdayturntime=int(time.mktime(time.strptime(day, "%Y-%m-%d")))
return day,allformdayturntime
def return_setday(time_term):
setdayturntime=int(time.mktime(time.strptime(time_term, "%Y-%m-%d")))
return setdayturntime
def time_count():
#打开课程总表
data_allform=xlrd.open_workbook(r'C:\Users\Administrator\Desktop\xxxx大学排课\安排-课程总表.xlsx')
#字典存储教室可上课日期及时间
dict_timecount={}
list_classroom=['401','402','403','机房1','机房2','601','602','603','604','605','606','701','702','703','704','705']
list_weekday=['星期一晚上','星期二晚上','星期三晚上','星期四晚上','星期五晚上','星期六上午','星期六下午1','星期六下午2','星期六晚上','星期日上午','星期日下午1','星期日下午2','星期日晚上']
for num_classroom in list_classroom:
dict_timecount={}
for num_weekday in list_weekday:
dict_timecount=[]
#字典存储日期的上课课程
dict_othertimecount={}
#for遍历课程总表,存储可上课时间
for data_allform_sheet in range(1,19):
table_allform=data_allform.sheet_by_name(str(data_allform_sheet))
#for遍历教室
for table_allform_class in range(1,17):
try:
classroom=re.findall('\\d\\d\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))
except:
classroom=re.findall('机房\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))
#遍历星期时间段
for table_allform_weekday in range(2,15):
if table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday)=='':
dict_timecount.append(return_allformday(table_allform.cell_value(3,table_allform_weekday)))
else:
try:
dict_othertimecount+table_allform.cell_value(4,table_allform_weekday)].append(table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday))
except:
dict_othertimecount+table_allform.cell_value(4,table_allform_weekday)]=[]
dict_othertimecount+table_allform.cell_value(4,table_allform_weekday)].append(table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday))
return dict_timecount,dict_othertimecount
def time_othercount(dict_CourseListClass,CourseListTeacher,timecount):
#打开课程总表
data_allform=xlrd.open_workbook(r'C:\Users\Administrator\Desktop\xxxx大学排课\辅助-课程总表.xlsx')
#for遍历课程总表
for data_allform_sheet in range(1,19):
table_allform=data_allform.sheet_by_name(str(data_allform_sheet))
#for遍历教室
for table_allform_class in range(1,17):
try:
classroom=re.findall('\\d\\d\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))
except:
classroom=re.findall('机房\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))
#遍历星期时间段
for table_allform_weekday in range(2,15):
#如果在规定时间内
if return_setday(set_termstart)<=return_allformday(table_allform.cell_value(3,table_allform_weekday)) and return_setday(set_termend)>=return_allformday(table_allform.cell_value(3,table_allform_weekday)):
#如果当日有课且不是节假日
if table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday)!='' and (('放假' in table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday))==False):
day_eachday=return_allformday(table_allform.cell_value(3,table_allform_weekday))
day_eachcourse=table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday)
day_eachweekdayANDeachdaytime=table_allform.cell_value(2,table_allform_weekday)+table_allform.cell_value(4,table_allform_weekday)
try:
day_eachclass=re.findall('\\d\\d\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))
except:
day_eachclass=re.findall('机房\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))
#如果两门课的班级和老师不冲突
if list(set(dict_CourseListClass) & set(dict_CourseListClass))!=[] or list(set(CourseListTeacher) & set(CourseListTeacher))!=[]:
# print(day_eachcourse)
# print(day_eachday)
# print(day_eachweekdayANDeachdaytime)
# print(day_eachclass)
list_class=['401','402','403','机房1','机房2','601','602','603','604','605','606','701','702','703','704','705']
for each_class in list_class:
try:
timecount.remove(day_eachday)
except:
continue
return timecount
freetime=time_othercount(basedata(),basedata(),time_count())
#打印可安排时间
for chat_eachclassfreetime in ['401','402','403','机房1','机房2','601','602','603','604','605','606','701','702','703','704','705']:
if chat_freetime!='':
print(chat_eachclassfreetime+' '+chat_freetime)
print(freetime)
print('')
else:
for chat_eachdayfreetime in ['星期一晚上','星期二晚上','星期三晚上','星期四晚上','星期五晚上','星期六上午','星期六下午1','星期六下午2','星期六晚上','星期日上午','星期日下午1','星期日下午2','星期日晚上']:
print(chat_eachclassfreetime+' '+chat_eachdayfreetime)
print(freetime)
print('')
#打印已安排课程
#print(time_count())
效果
因为“JAVA语言”和“软件工程”是同一个班级,所以查到星期一晚上可安排时间就没有2021-03-01,其他就是可安排时间,然后手动将课程填入总表(工作簿以日期设了18个分表)并保存,继续下一个课程的检测。
检测冲突是为了解决:
1、同一班级在同一时间只能上一门课程
2、同一老师在同一时间只能上一门课程
3、同一教室在同一时间只能上一门课程 好东西,谢谢老板{:1_918:} 好东西,谢谢楼主无私分享 哪个学校一学期一门课 向大佬学习 是不是一行代码写的太长了 感谢分享 感谢博主分享,下载了,学习下。 点赞,下载学习 大佬 国开能再分享下吗
页:
[1]
2