Advertisement
mennakhaled6

study plan

Jun 29th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 25.39 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Apr 18 19:06:15 2017
  4.  
  5. @author: Menna
  6. """
  7. import pymysql
  8. import pandas as pd
  9. import numpy as np
  10. #import sys
  11. import math
  12.  
  13. input_student = input('Choose an ID:')
  14.  
  15. db = pymysql.connect(host="127.0.0.1",user="root",passwd="",db="registration_system")
  16. cur = db.cursor()
  17. cur1 = db.cursor()
  18. cur2 = db.cursor()
  19. cur3 = db.cursor()
  20. cur4 = db.cursor()
  21. cur5 = db.cursor()
  22. cur6 = db.cursor()
  23. cur.execute("SELECT  student_id , course_code , grade FROM STUDENT_COURSE  ")
  24. query_result = cur.fetchall()
  25. cur1.execute("SELECT course_code,course_creditHours  FROM COURSE  ")
  26. courses = cur1.fetchall()
  27. cur2.execute("SELECT student_id , total_hours  FROM STUDENT  ")
  28. Total_Hours = cur2.fetchall()
  29. cur3.execute("SELECT *  FROM PREREQUISITES ")
  30. prerequisites = cur3.fetchall()
  31. cur4.execute("SELECT COUNT(DISTINCT student_semester)  FROM student_course WHERE student_id="+ input_student+" AND student_semester LIKE 'F%'")
  32. FallCount = cur4.fetchone()
  33. cur5.execute("SELECT COUNT(DISTINCT student_semester)  FROM student_course WHERE student_id="+ input_student+" AND student_semester LIKE 'Spr%'")
  34. SpringCount = cur5.fetchone()
  35. cur6.execute("SELECT * FROM student_course WHERE student_id="+input_student)
  36. query_result6 = cur6.fetchall()
  37. db.close()
  38.  
  39. all_students = pd.DataFrame()
  40. Courses = pd.DataFrame()
  41. OutputDataFrame = pd.DataFrame()
  42. FinalDataFrame = pd.DataFrame()
  43. AssociationDataFrame = pd.DataFrame()
  44. TotalHoursDataFrame = pd.DataFrame()
  45. Prerequisite_Course = pd.DataFrame()
  46.  
  47. for (i,row) in enumerate(query_result):
  48.     current_student = pd.DataFrame({'ID':[int(row[0])],'Course':[row[1]],'Grade':[row[2]]},index=[i])
  49.     all_students = all_students.append(current_student)
  50. #print(all_students)
  51.  
  52. for (i,row) in enumerate(Total_Hours):
  53.     totalhoursofstudent = pd.DataFrame({'ID':[int(row[0])],'Total_hours':[row[1]]},index=[i])
  54.     TotalHoursDataFrame = TotalHoursDataFrame.append(totalhoursofstudent)
  55.  
  56.  
  57. for (i,row) in enumerate(courses):
  58.     current_course = pd.DataFrame({'Course':[row[0]]}, index =[i])
  59.     current_course['i'] = current_course.index
  60.     Courses = Courses.append(current_course)
  61.  
  62. for (i,row) in enumerate(prerequisites):
  63.     prerequisitecourse = pd.DataFrame({'Course':[row[0]],'Prerequisite':[row[1]]}, index =[i])
  64.     Prerequisite_Course = Prerequisite_Course.append(prerequisitecourse)
  65.  
  66.  
  67. OutputDataFrame = all_students.set_index('Course').join(Courses.set_index('Course'))
  68.  
  69. Courses = None
  70. query_result = None
  71. OutputDataFrame = OutputDataFrame.sort_index(by=['ID'], ascending=[True])
  72.  
  73. #print(OutputDataFrame)
  74.  
  75. OutputDataFrame = OutputDataFrame.replace('A',10)
  76. OutputDataFrame = OutputDataFrame.replace('A-',9)
  77. OutputDataFrame = OutputDataFrame.replace('B+',8)
  78. OutputDataFrame = OutputDataFrame.replace('B',7)
  79. OutputDataFrame = OutputDataFrame.replace('B-',6)
  80. OutputDataFrame = OutputDataFrame.replace('C+',5)
  81. OutputDataFrame = OutputDataFrame.replace('C',4)
  82. OutputDataFrame = OutputDataFrame.replace('C-',3)
  83. OutputDataFrame = OutputDataFrame.replace('D+',2)
  84. OutputDataFrame = OutputDataFrame.replace('D',1)
  85. OutputDataFrame = OutputDataFrame.replace('W',0)
  86. OutputDataFrame = OutputDataFrame.replace('F',0)
  87. OutputDataFrame = OutputDataFrame.replace('Ab',0)
  88. OutputDataFrame = OutputDataFrame.replace('',0)
  89. OutputDataFrame = OutputDataFrame.replace('I',0)
  90. OutputDataFrame = OutputDataFrame.replace('E',0)
  91. OutputDataFrame = OutputDataFrame.replace('AU',0)
  92.  
  93.  
  94. for(i,row) in enumerate(OutputDataFrame.ID.unique()):
  95.     Data = OutputDataFrame[ OutputDataFrame['ID'] == row]
  96.     array = np.zeros(88)
  97.     for(j,row1) in enumerate(Data['i']):
  98.         Indices = row1
  99.         array[Indices] = Data['Grade'][j]
  100.     current_output = pd.DataFrame({'ID':row,'Array':[array]},index=[i])
  101.     FinalDataFrame = FinalDataFrame.append(current_output)
  102. #with pd.option_context('display.max_rows', None, 'display.max_columns', 3,'display.max_colwidth', 1000):
  103.    #print(FinalDataFrame) #to print whole data frame
  104.  
  105.  
  106. from sklearn.cluster import KMeans
  107. X = list(FinalDataFrame.Array)
  108. kmeans_model = KMeans(n_clusters=13, random_state = 22).fit(X) # root(333/2)
  109. labels = kmeans_model.labels_ # el array of 333 bytl3 feha kol wa7ed belongs le anhi cluster
  110. centers = kmeans_model.cluster_centers_ # btegb el centers bt3t el clusters
  111.  
  112. xx=FinalDataFrame.loc[FinalDataFrame['ID'] == int(input_student)] #search in dataframe
  113. cluster_number = labels[xx.index]
  114.  
  115.  
  116. #x=kmeans_model.predict(FinalDataFrame.Array[1])
  117. #xx=FinalDataFrame.loc[FinalDataFrame['ID'] == 2222] #search in dataframe
  118. #labels[xx.index]
  119.  
  120. def ClusterIndicesNumpy(clustNum, labels_array): #numpy
  121.     return np.where(labels_array == clustNum)[0]
  122. np.set_printoptions(threshold=np.inf) #to print whole array
  123. ElementsInCluster=ClusterIndicesNumpy(int(cluster_number), labels)
  124.  
  125. ClusterOfClusters = []
  126. for i in ElementsInCluster:
  127.     ClusterOfClusters.append(X[i])
  128.  
  129.  
  130. IDsInCluster = []
  131. for i in ElementsInCluster:
  132.     IDsInCluster.append(FinalDataFrame.iloc[i]['ID'])
  133.    
  134. n = len(ClusterOfClusters)
  135. LengthOfNewCluster = math.ceil(math.sqrt(n/2))
  136.  
  137. kmeans_clusterofclusters = KMeans(n_clusters=LengthOfNewCluster, random_state = 22).fit(ClusterOfClusters) # root(333/2)
  138. labels_clusterofclusters = kmeans_clusterofclusters.labels_  
  139. centers_clusterofclusters = kmeans_clusterofclusters.cluster_centers_ # btegb el centers bt3t el clusters  
  140.  
  141. for (i,row) in enumerate(IDsInCluster):
  142.     if(IDsInCluster[i]==int(input_student)):
  143.         Index = i
  144. cluster_number_clusterofclusters = labels_clusterofclusters[Index]
  145.  
  146. def ClusterIndicesNumpy(clustNum, labels_array): #numpy
  147.     return np.where(labels_array == clustNum)[0]
  148. np.set_printoptions(threshold=np.inf) #to print whole array
  149. ElementsInCluster_clusterofcluster = ClusterIndicesNumpy(int(cluster_number_clusterofclusters), labels_clusterofclusters)
  150.  
  151. indicesinClusterofCluster = []
  152. for i in ElementsInCluster_clusterofcluster:
  153.     indicesinClusterofCluster.append(ElementsInCluster[i])
  154.  
  155. IDsInClusterOfClusters = []
  156. for i in indicesinClusterofCluster:
  157.     IDsInClusterOfClusters.append(FinalDataFrame.iloc[i]['ID'])
  158.  
  159.    
  160. for(i,row) in enumerate(IDsInClusterOfClusters):
  161.   Data = OutputDataFrame[ OutputDataFrame['ID'] == row]
  162.   courses_array = np.array(88)
  163.   courses_array = Data.index.values
  164.   association_input = pd.DataFrame({'Courses':[courses_array]},index=[i])
  165.   AssociationDataFrame=AssociationDataFrame.append(association_input)
  166. ArrayOfCourses = list(AssociationDataFrame.Courses)
  167.  
  168.  
  169. import pyfpgrowth
  170. support = math.floor(len(ArrayOfCourses)*0.75)
  171. patterns = pyfpgrowth.find_frequent_patterns(ArrayOfCourses, support)
  172. rules = pyfpgrowth.generate_association_rules(patterns, 0.6)
  173. #sys.setrecursionlimit(1000)
  174. #rules = list(rules)
  175.  
  176. FetchingCoursesOfID = OutputDataFrame[ OutputDataFrame['ID'] == int(input_student)]
  177. courses_Of_Given_ID = FetchingCoursesOfID.index.values
  178. courses_Of_Given_ID = set(courses_Of_Given_ID)
  179.  
  180. keys = list(rules.keys())
  181. values = list(rules.values())
  182. keys = [set(elem) for elem in keys]
  183. #values = [list(elem) for elem in values]
  184.  
  185.  
  186. New_values=[]
  187. for (k,rows) in enumerate(keys):
  188.    Output_intersection=courses_Of_Given_ID.intersection(keys[k])
  189.    match=len(Output_intersection)/len(keys[k])
  190.    if (match >= 0.6):
  191.          New_values.append(values[k])
  192.  
  193.  
  194. OutputRulesCourses = []
  195. for (i,row) in enumerate(New_values):
  196.     OutputRulesCourses.append(New_values[i][0])
  197. flattened  = [val for sublist in OutputRulesCourses for val in sublist]
  198. flattened  = set(flattened)
  199.  
  200. ToBeRecommended = flattened - courses_Of_Given_ID
  201. ToBeRecommended = list(ToBeRecommended)
  202.  
  203. addit = []
  204. nextterm = []
  205. mustbeadded = []
  206. for row in ToBeRecommended:
  207.    pre = set(Prerequisite_Course.loc[Prerequisite_Course['Course'] == row]['Prerequisite'])
  208.    condition = all(x in courses_Of_Given_ID for x in pre)
  209.    if not pre:
  210.          addit.append(row)
  211.    elif(condition):
  212.          addit.append(row)
  213.    elif(not condition):
  214.          pre =list(pre - courses_Of_Given_ID)
  215.          mustbeadded.extend(pre)
  216.          nextterm.append(row)
  217.      
  218. addit = list(set(addit) - set(mustbeadded))
  219. mustbeadded.extend(addit)
  220. addit = mustbeadded
  221. nextterm = list(set(nextterm))
  222.  
  223. FetchingTotalhoursofID = TotalHoursDataFrame[ TotalHoursDataFrame['ID'] == int(input_student)]
  224. FetchingTotalhoursofID = list(FetchingTotalhoursofID['Total_hours'])
  225. FetchingTotalhoursofID = FetchingTotalhoursofID[0]
  226.  
  227.  
  228. CurrentTermNo=int(FallCount[0])+int(SpringCount[0]) #el last term
  229. NextTermNo=CurrentTermNo+1 #el term el mfrod a-recommendelo
  230.  
  231. FirstYear = 35
  232. SecondYear = 72
  233. ThirdYear = 106
  234. FourthYear = 140
  235. FifthYear = 175
  236.  
  237. if (CurrentTermNo == 1 or CurrentTermNo == 2):
  238.     Year=1
  239. elif (CurrentTermNo == 3 or CurrentTermNo == 4):
  240.     Year=2
  241. elif (CurrentTermNo == 5 or CurrentTermNo == 6):
  242.     Year=3
  243. elif (CurrentTermNo == 7 or CurrentTermNo == 8):
  244.     Year=4
  245. elif (CurrentTermNo == 9 or CurrentTermNo == 10):
  246.     Year=5
  247.  
  248. if (Year ==1):
  249.     RemainingHours = FirstYear - FetchingTotalhoursofID
  250. elif (Year ==2):
  251.     RemainingHours = SecondYear - FetchingTotalhoursofID
  252. elif (Year ==3):
  253.     RemainingHours = ThirdYear - FetchingTotalhoursofID
  254. elif (Year ==4):
  255.     RemainingHours = FourthYear - FetchingTotalhoursofID
  256. elif (Year ==5):
  257.     RemainingHours = FifthYear - FetchingTotalhoursofID
  258.  
  259.  
  260.  
  261. CoursesCreditHr = pd.DataFrame()
  262. for (i,row) in enumerate(courses):
  263.     Courses_CreditHr = pd.DataFrame({'Course':[row[0]],'CreditHr':[row[1]]}, index =[i])
  264.     CoursesCreditHr = CoursesCreditHr.append(Courses_CreditHr)
  265.  
  266. #totalHrsRec = 0
  267. #hr1 = 0
  268. #hr2 = 0
  269. #CurrentSemester = [] #NextTermNo
  270. #NextSemester = []    #NextTermNo+1
  271. #NextSemester.extend(nextterm)
  272. #for (i,row) in enumerate(addit):
  273. #    CourseCredit=CoursesCreditHr.loc[CoursesCreditHr['Course'] == addit[i]]
  274. #    HrPerCourse = int(CourseCredit['CreditHr'])
  275. #    totalHrsRec=totalHrsRec+HrPerCourse
  276. #    if (totalHrsRec <= 20):
  277. #        CurrentSemester.append(addit[i])
  278. #        hr1 = hr1+HrPerCourse
  279. #    elif(totalHrsRec > 20):
  280. #        NextSemester.append(addit[i])
  281. #        hr2 = hr2+HrPerCourse
  282.      
  283. #TookTerms=''
  284. #if(not NextSemester):
  285. #     db = pymysql.connect(host="127.0.0.1",user="root",passwd="",db="registration_system")
  286. #     cur7 = db.cursor()
  287. #     TookTerms=str(NextTermNo)
  288. #     cur7.execute("SELECT * FROM fixed_studyplan WHERE term <="+TookTerms)
  289. #     query_result7 = cur7.fetchall()
  290. #     db.close()
  291. #elif(NextSemester):
  292. #    db = pymysql.connect(host="127.0.0.1",user="root",passwd="",db="registration_system")
  293. #    cur7 = db.cursor()
  294. #    TookTerms=str(int(NextTermNo)+1)
  295. #    cur7.execute("SELECT * FROM fixed_studyplan WHERE term <="+TookTerms)
  296. #    query_result7 = cur7.fetchall()
  297. #    db.close()
  298. Courses_Taken_ID = pd.DataFrame()  
  299. for (i,row) in enumerate(query_result6):
  300.     CoursesTakenID = pd.DataFrame({'CoursesTaken':[row[1]]}, index =[i])
  301.     Courses_Taken_ID = Courses_Taken_ID.append(CoursesTakenID)
  302. Courses_Taken_ID = list(Courses_Taken_ID.CoursesTaken)
  303. recommended = []
  304. CurrentSemester = []
  305. NextSemester = []
  306. remaining = []
  307. hr2 = 0
  308. should_Take_Courses = pd.DataFrame()
  309.  
  310. TotalHrsID = FetchingTotalhoursofID
  311. RemHr = RemainingHours
  312.  
  313. for i in range(NextTermNo,6):
  314.     CR_HRS = 0
  315.    
  316.     if(i == 3 and (RemHr==0 or RemHr < 0)) :
  317.         RemHr = 0
  318.         CR_HRS = 20
  319.         TotalHrsID = TotalHrsID + CR_HRS
  320.         if (i == 1 or i == 2):
  321.             Y=1
  322.         elif (i == 3 or i == 4):
  323.             Y=2
  324.         elif (i == 5 or i == 6):
  325.             Y=3
  326.         elif (i == 7 or i == 8):
  327.             Y=4
  328.         elif (i == 9 or i == 10):
  329.             Y=5
  330.         if (Y ==1):
  331.             RemHr = 35 - TotalHrsID
  332.         elif (Y ==2):
  333.             RemHr = 72 - TotalHrsID
  334.         elif (Y ==3):
  335.             RemHr = 106 - TotalHrsID
  336.         elif (Y ==4):
  337.             RemHr = 140 - TotalHrsID
  338.         elif (Y ==5):
  339.             RemHr = 175 - TotalHrsID
  340.  
  341.     elif(RemHr > 2 and not i % 2 == 0 ):
  342.         RemHr = 0
  343.         CR_HRS = 20
  344.                
  345.         if (i == NextTermNo):
  346.             totalHrsRec = 0
  347.             hr1 = 0
  348.                        
  349.             NextSemester.extend(nextterm)
  350.             for (k,row) in enumerate(nextterm):
  351.                 CourseCredit=CoursesCreditHr.loc[CoursesCreditHr['Course'] == nextterm[k]]
  352.                 HrPerCourse = int(CourseCredit['CreditHr'])
  353.                 totalHrsRec=totalHrsRec+HrPerCourse
  354.             hr2 = totalHrsRec
  355.             HrPerCourse = 0
  356.             totalHrsRec = 0
  357.             for (j,row) in enumerate(addit):
  358.                 CourseCredit=CoursesCreditHr.loc[CoursesCreditHr['Course'] == addit[j]]
  359.                 HrPerCourse = int(CourseCredit['CreditHr'])
  360.                 totalHrsRec=totalHrsRec+HrPerCourse
  361.                 if (totalHrsRec <= CR_HRS):
  362.                     CurrentSemester.append(addit[j])
  363.                     hr1 = hr1+HrPerCourse
  364.                 elif(totalHrsRec > CR_HRS):
  365.                     NextSemester.append(addit[j])
  366.                     hr2 = hr2+HrPerCourse
  367.                     totalHrsRec = totalHrsRec - HrPerCourse
  368.             CR_HRS =hr1
  369.             print("Term : "+str(i))
  370.             print(CurrentSemester)
  371.             recommended.extend(CurrentSemester)
  372.             print("with "+str(hr1)+" credit hours")
  373.             #fadel lw el addit feha mwad olyla
  374.         elif (i == (NextTermNo+1)):
  375.             print("not now")
  376.         else:
  377.             if(NextSemester):
  378.                 if (hr2 < CR_HRS ):
  379.                     db = pymysql.connect(host="127.0.0.1",user="root",passwd="",db="registration_system")
  380.                     cur8 = db.cursor()
  381.                     TookTerms=str(i)
  382.                     cur8.execute("SELECT * FROM fixed_studyplan WHERE term <=(%s) ORDER BY term",(TookTerms))
  383.                     query_result8 = cur8.fetchall()
  384.                     db.close()
  385.                     should_Take_Courses = None
  386.                     should_Take_Courses = pd.DataFrame()
  387.                     for (j,row) in enumerate(query_result8):
  388.                             shouldTakeCourses = pd.DataFrame({'Course':[row[0]],'term':[row[2]]}, index =[j])
  389.                             should_Take_Courses = should_Take_Courses.append(shouldTakeCourses)
  390.                             should_Take_Courses = should_Take_Courses.drop_duplicates().reset_index(drop=True)
  391.                             #should_Take_Courses = should_Take_Courses.sort_index(by=['term'], ascending=[True])
  392.                     should_Take_Courses = list(should_Take_Courses.Course)
  393.                     CurrentSemester = []
  394.                     CurrentSemester.extend(NextSemester)
  395.                     remaining = list(set(should_Take_Courses)-set(Courses_Taken_ID)-set(recommended))
  396.                     remaining = list(set(remaining)-set(NextSemester))
  397.                     NextSemester = []
  398.                     hr1=hr2
  399.                     HrPerCourse = 0
  400.                     totalHrsRec = hr2
  401.                     hr2=0
  402.                     for (j,row) in enumerate(remaining):
  403.                             CourseCredit=CoursesCreditHr.loc[CoursesCreditHr['Course'] == remaining[j]]
  404.                             HrPerCourse = int(CourseCredit['CreditHr'])
  405.                             totalHrsRec=totalHrsRec+HrPerCourse
  406.                             if (totalHrsRec <= CR_HRS):
  407.                                 CurrentSemester.append(remaining[j])
  408.                                 hr1 = hr1+HrPerCourse
  409.                             elif(totalHrsRec > CR_HRS):
  410.                                 NextSemester.append(remaining[j])
  411.                                 hr2 = hr2+HrPerCourse
  412.                                 totalHrsRec = totalHrsRec - HrPerCourse
  413.                     CR_HRS =hr1
  414.                     print("Term : "+str(i))
  415.                     print(CurrentSemester)
  416.                     recommended.extend(CurrentSemester)
  417.                     remaining = list(set(remaining)-set(recommended))
  418.                     print("with "+str(hr1)+" credit hours")
  419.                 elif(hr2 >= CR_HRS):
  420.                    print("not now")
  421.             elif(not NextSemester):
  422.                 print("not now")
  423.            
  424.         TotalHrsID = TotalHrsID + CR_HRS
  425.         if (i == 1 or i == 2):
  426.             Y=1
  427.         elif (i == 3 or i == 4):
  428.             Y=2
  429.         elif (i == 5 or i == 6):
  430.             Y=3
  431.         elif (i == 7 or i == 8):
  432.             Y=4
  433.         elif (i == 9 or i == 10):
  434.             Y=5
  435.         if (Y ==1):
  436.             RemHr = 35 - TotalHrsID
  437.         elif (Y ==2):
  438.             RemHr = 72 - TotalHrsID
  439.         elif (Y ==3):
  440.             RemHr = 106 - TotalHrsID
  441.         elif (Y ==4):
  442.             RemHr = 140 - TotalHrsID
  443.         elif (Y ==5):
  444.             RemHr = 175 - TotalHrsID
  445.        
  446.     elif(RemHr > 0 and RemHr < 3 and not i % 2 == 0 ):
  447.         CR_HRS = 17 + RemHr
  448.         RemHr = 0
  449.         TotalHrsID = TotalHrsID + CR_HRS
  450.         if (i == 1 or i == 2):
  451.             Y=1
  452.         elif (i == 3 or i == 4):
  453.             Y=2
  454.         elif (i == 5 or i == 6):
  455.             Y=3
  456.         elif (i == 7 or i == 8):
  457.             Y=4
  458.         elif (i == 9 or i == 10):
  459.             Y=5
  460.         if (Y ==1):
  461.             RemHr = 35 - TotalHrsID
  462.         elif (Y ==2):
  463.             RemHr = 72 - TotalHrsID
  464.         elif (Y ==3):
  465.             RemHr = 106 - TotalHrsID
  466.         elif (Y ==4):
  467.             RemHr = 140 - TotalHrsID
  468.         elif (Y ==5):
  469.             RemHr = 175 - TotalHrsID
  470.        
  471.     elif(RemHr <= 12 and i % 2 == 0):
  472.         RemHr = 0
  473.         CR_HRS = 12
  474.         TotalHrsID = TotalHrsID + CR_HRS
  475.         if (i == 1 or i == 2):
  476.             Y=1
  477.         elif (i == 3 or i == 4):
  478.             Y=2
  479.         elif (i == 5 or i == 6):
  480.             Y=3
  481.         elif (i == 7 or i == 8):
  482.             Y=4
  483.         elif (i == 9 or i == 10):
  484.             Y=5
  485.         if (Y ==1):
  486.             RemHr = 35 - TotalHrsID
  487.         elif (Y ==2):
  488.             RemHr = 72 - TotalHrsID
  489.         elif (Y ==3):
  490.             RemHr = 106 - TotalHrsID
  491.         elif (Y ==4):
  492.             RemHr = 140 - TotalHrsID
  493.         elif (Y ==5):
  494.             RemHr = 175 - TotalHrsID
  495.     elif(RemHr > 12 and RemHr <= 20 and i % 2 == 0 ):#must be <=20
  496.         CR_HRS = RemHr
  497.         RemHr = 0
  498.         TotalHrsID = TotalHrsID + CR_HRS
  499.         if (i == 1 or i == 2):
  500.             Y=1
  501.         elif (i == 3 or i == 4):
  502.             Y=2
  503.         elif (i == 5 or i == 6):
  504.             Y=3
  505.         elif (i == 7 or i == 8):
  506.             Y=4
  507.         elif (i == 9 or i == 10):
  508.             Y=5
  509.         if (Y ==1):
  510.             RemHr = 35 - TotalHrsID
  511.         elif (Y ==2):
  512.             RemHr = 72 - TotalHrsID
  513.         elif (Y ==3):
  514.             RemHr = 106 - TotalHrsID
  515.         elif (Y ==4):
  516.             RemHr = 140 - TotalHrsID
  517.         elif (Y ==5):
  518.             RemHr = 175 - TotalHrsID
  519.  
  520.     elif(RemHr > 20 and  i % 2 == 0 ):
  521.         RemHr = 0
  522.         CR_HRS = 20
  523.        
  524.         if (i == NextTermNo):
  525.             print("not now")
  526.        
  527.         elif (i == (NextTermNo+1)):
  528.             if(NextSemester):
  529.                 if (hr2 < CR_HRS ):
  530.                     db = pymysql.connect(host="127.0.0.1",user="root",passwd="",db="registration_system")
  531.                     cur7 = db.cursor()
  532.                     TookTerms=str(i)
  533.                     cur7.execute("SELECT * FROM fixed_studyplan WHERE term <=(%s) ORDER BY term",(TookTerms))
  534.                     query_result7 = cur7.fetchall()
  535.                     db.close()
  536.                    
  537.                     for (j,row) in enumerate(query_result7):
  538.                         shouldTakeCourses = pd.DataFrame({'Course':[row[0]],'term':[row[2]]}, index =[j])
  539.                         should_Take_Courses = should_Take_Courses.append(shouldTakeCourses)
  540.                         should_Take_Courses = should_Take_Courses.drop_duplicates().reset_index(drop=True)
  541.                         #should_Take_Courses = should_Take_Courses.sort_index(by=['term'], ascending=[True])
  542.                     should_Take_Courses = list(should_Take_Courses.Course)
  543.                     remaining = list(set(should_Take_Courses)-set(Courses_Taken_ID)-set(recommended))
  544.                     remaining = list(set(remaining)-set(NextSemester))
  545.                     CurrentSemester = []
  546.                     CurrentSemester.extend(NextSemester)
  547.                     NextSemester = []
  548.                     hr1=hr2
  549.                     HrPerCourse = 0
  550.                     totalHrsRec = hr2
  551.                     hr2=0
  552.                     for (j,row) in enumerate(remaining):
  553.                         CourseCredit=CoursesCreditHr.loc[CoursesCreditHr['Course'] == remaining[j]]
  554.                         HrPerCourse = int(CourseCredit['CreditHr'])
  555.                         totalHrsRec=totalHrsRec+HrPerCourse
  556.                         if (totalHrsRec <= CR_HRS):
  557.                             CurrentSemester.append(remaining[j])
  558.                             hr1 = hr1+HrPerCourse
  559.                         elif(totalHrsRec > CR_HRS):
  560.                             NextSemester.append(remaining[j])
  561.                             hr2 = hr2+HrPerCourse
  562.                             totalHrsRec = totalHrsRec - HrPerCourse
  563.                     CR_HRS =hr1
  564.                     print("Term : "+str(i))
  565.                     print(CurrentSemester)
  566.                     recommended.extend(CurrentSemester)
  567.                     remaining = list(set(remaining)-set(recommended))
  568.                     print("with "+str(hr1)+" credit hours")
  569.                 elif(hr2 >= CR_HRS):
  570.                     print("not now")
  571.             elif(not NextSemester):
  572.                 print("not now")
  573.         else:
  574.             if(NextSemester):
  575.                 if (hr2 < CR_HRS ):
  576.                     db = pymysql.connect(host="127.0.0.1",user="root",passwd="",db="registration_system")
  577.                     cur9 = db.cursor()
  578.                     TookTerms=str(i)
  579.                     cur9.execute("SELECT * FROM fixed_studyplan WHERE term <=(%s) ORDER BY term",(TookTerms))
  580.                     query_result9 = cur9.fetchall()
  581.                     db.close()
  582.                     should_Take_Courses = None
  583.                     should_Take_Courses = pd.DataFrame()
  584.                     for (j,row) in enumerate(query_result9):
  585.                             shouldTakeCourses = pd.DataFrame({'Course':[row[0]],'term':[row[2]]}, index =[j])
  586.                             should_Take_Courses = should_Take_Courses.append(shouldTakeCourses)
  587.                             should_Take_Courses = should_Take_Courses.drop_duplicates().reset_index(drop=True)
  588.                             #should_Take_Courses = should_Take_Courses.sort_index(by=['term'], ascending=[True])
  589.                     should_Take_Courses = list(should_Take_Courses.Course)
  590.                     CurrentSemester = []
  591.                     CurrentSemester.extend(NextSemester)
  592.                     remaining = list(set(should_Take_Courses)-set(Courses_Taken_ID)-set(recommended))
  593.                     remaining = list(set(remaining)-set(NextSemester))
  594.                     NextSemester = []
  595.                     hr1=hr2
  596.                     HrPerCourse = 0
  597.                     totalHrsRec = hr2
  598.                     hr2=0
  599.                     for (j,row) in enumerate(remaining):
  600.                             CourseCredit=CoursesCreditHr.loc[CoursesCreditHr['Course'] == remaining[j]]
  601.                             HrPerCourse = int(CourseCredit['CreditHr'])
  602.                             totalHrsRec=totalHrsRec+HrPerCourse
  603.                             if (totalHrsRec <= CR_HRS):
  604.                                 CurrentSemester.append(remaining[j])
  605.                                 hr1 = hr1+HrPerCourse
  606.                             elif(totalHrsRec > CR_HRS):
  607.                                 NextSemester.append(remaining[j])
  608.                                 hr2 = hr2+HrPerCourse
  609.                                 totalHrsRec = totalHrsRec - HrPerCourse
  610.                     CR_HRS =hr1
  611.                     print("Term : "+str(i))
  612.                     print(CurrentSemester)
  613.                     recommended.extend(CurrentSemester)
  614.                     remaining = list(set(remaining)-set(recommended))
  615.                     print("with "+str(hr1)+" credit hours")
  616.                 elif(hr2 >= CR_HRS):
  617.                    print("not now")
  618.             elif(not NextSemester):
  619.                 print("not now")
  620.            
  621.                
  622.            
  623.         TotalHrsID = TotalHrsID + CR_HRS
  624.         if (i == 1 or i == 2):
  625.             Y=1
  626.         elif (i == 3 or i == 4):
  627.             Y=2
  628.         elif (i == 5 or i == 6):
  629.             Y=3
  630.         elif (i == 7 or i == 8):
  631.             Y=4
  632.         elif (i == 9 or i == 10):
  633.             Y=5
  634.         if (Y ==1):
  635.             RemHr = 35 - TotalHrsID
  636.         elif (Y ==2):
  637.             RemHr = 72 - TotalHrsID
  638.         elif (Y ==3):
  639.             RemHr = 106 - TotalHrsID
  640.         elif (Y ==4):
  641.             RemHr = 140 - TotalHrsID
  642.         elif (Y ==5):
  643.             RemHr = 175 - TotalHrsID
  644.  
  645.     elif(not i % 2 == 0 and not i ==3 and (RemHr==0 or RemHr < 0)):
  646.         RemHr = 0
  647.         CR_HRS = 17
  648.         TotalHrsID = TotalHrsID + CR_HRS
  649.         if (i == 1 or i == 2):
  650.             Y=1
  651.         elif (i == 3 or i == 4):
  652.             Y=2
  653.         elif (i == 5 or i == 6):
  654.             Y=3
  655.         elif (i == 7 or i == 8):
  656.             Y=4
  657.         elif (i == 9 or i == 10):
  658.             Y=5
  659.         if (Y ==1):
  660.             RemHr = 35 - TotalHrsID
  661.         elif (Y ==2):
  662.             RemHr = 72 - TotalHrsID
  663.         elif (Y ==3):
  664.             RemHr = 106 - TotalHrsID
  665.         elif (Y ==4):
  666.             RemHr = 140 - TotalHrsID
  667.         elif (Y ==5):
  668.             RemHr = 175 - TotalHrsID
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement