def main(): # try to open yaml config file strConfig = open( configFile ) if not strConfig: print "Error reading configfile", configFile exit(1) # try to parse yaml config string cfg = yaml.load( strConfig ) if not cfg: print "Error parsing yaml config file" exit(1) # check if all required config keys are available for key in ['mysql_host', 'mysql_user', 'mysql_pass', 'mysql_name']: if key not in cfg: print "Missing parameter", key, "in configuration" exit(1) # create database connection dbcron = MySQLdb.connect ( host=cfg['mysql_host'], user=cfg['mysql_user'], passwd=cfg['mysql_pass'], db=cfg['mysql_name'], cursorclass=MySQLdb.cursors.SSDictCursor ) dbcron2 = MySQLdb.connect ( host=cfg['mysql_host'], user=cfg['mysql_user'], passwd=cfg['mysql_pass'], db=cfg['mysql_name'], cursorclass=MySQLdb.cursors.SSDictCursor ) dbcursor = dbcron.cursor() dbcursor.execute('SELECT nickname FROM mac_to_nick GROUP by nickname') for entry in dbcursor: objTimetable = TimeTable( entry['nickname'], dbcron2 ) print "Result for", entry['nickname'], ":", objTimetable.getTimePresent() dbcursor.close() dbcron.close()
def print_timetable(chat_id, day_or_week, timetable_format, week_num): data = give_timetable(chat_id) week = give_week() t = TimeTable(data, week) if day_or_week == 'week': text = t.print_week(timetable_format, week_num) else: text = t.print_day(t.give_day(day_or_week), timetable_format, week_num) return text
def main(): # try to open yaml config file strConfig = open(configFile) if not strConfig: print "Error reading configfile", configFile exit(1) # try to parse yaml config string cfg = yaml.load(strConfig) if not cfg: print "Error parsing yaml config file" exit(1) # check if all required config keys are available for key in ['mysql_host', 'mysql_user', 'mysql_pass', 'mysql_name']: if key not in cfg: print "Missing parameter", key, "in configuration" exit(1) # create database connection dbcron = MySQLdb.connect(host=cfg['mysql_host'], user=cfg['mysql_user'], passwd=cfg['mysql_pass'], db=cfg['mysql_name'], cursorclass=MySQLdb.cursors.SSDictCursor) dbcron2 = MySQLdb.connect(host=cfg['mysql_host'], user=cfg['mysql_user'], passwd=cfg['mysql_pass'], db=cfg['mysql_name'], cursorclass=MySQLdb.cursors.SSDictCursor) dbcursor = dbcron.cursor() collector = {} dbcursor.execute("SELECT DISTINCT nickname FROM mac_to_nick") for entry in dbcursor: nickname = str.lower(entry['nickname']) objTimetable = TimeTable(nickname, dbcron2) collector[nickname] = objTimetable.getTimeTable() dbcursor.close() dbcron.close() for nick, table in collector.iteritems(): getRelation(nick, collector)
def main(): # try to open yaml config file strConfig = open(configFile) if not strConfig: print "Error reading configfile", configFile exit(1) # try to parse yaml config string cfg = yaml.load(strConfig) if not cfg: print "Error parsing yaml config file" exit(1) # check if all required config keys are available for key in ['mysql_host', 'mysql_user', 'mysql_pass', 'mysql_name']: if key not in cfg: print "Missing parameter", key, "in configuration" exit(1) # create database connection dbcron = MySQLdb.connect(host=cfg['mysql_host'], user=cfg['mysql_user'], passwd=cfg['mysql_pass'], db=cfg['mysql_name'], cursorclass=MySQLdb.cursors.SSDictCursor) dbcron2 = MySQLdb.connect(host=cfg['mysql_host'], user=cfg['mysql_user'], passwd=cfg['mysql_pass'], db=cfg['mysql_name'], cursorclass=MySQLdb.cursors.SSDictCursor) dbcursor = dbcron.cursor() dbcursor.execute('SELECT nickname FROM mac_to_nick GROUP by nickname') for entry in dbcursor: objTimetable = TimeTable(entry['nickname'], dbcron2) print "Result for", entry[ 'nickname'], ":", objTimetable.getTimePresent() dbcursor.close() dbcron.close()
def make_course_times(course_name, texts: List[List], first_day_of_semester: datetime, time_table=TimeTable()): """ texts格式: [ ["a,b,c...":周数, D[e-f节], 教室号], [...] ] e.g.: [['1,2,3', '四[3-4节]', 'D1151'] ['11,12,13', '六[3-4节]', 'D1138']] return: Dict{ 教室号: [(上课时间, 下课时间), ...] } """ course_time = {} for text in texts: # weeks 数组, 第几周 : int weeks = map(int, text[0].split(",")) reg_res = regex.findall(r"(.)\[(\d+)-(\d+)节\]", text[1])[0] if len(reg_res) == 0: raise (BaseException("正则表达式查找出错!\n{}".format(text[1]))) day, st, ed = reg_res # day: 星期几 : int day = char2int[day] # st, ed: 上课,下课时间 : time try: st = time_table[int(st)][0] ed = time_table[int(ed)][1] except KeyError as err: raise (KeyError(f"{course_name} 课程节次输入错误,请检查!")) # loc: 教室 loc = text[2] course_time.setdefault(loc, list()) for week in weeks: st_offset = timedelta(weeks=week - 1, days=day - 1, hours=st.hour, minutes=st.minute, seconds=st.second) ed_offset = timedelta(weeks=week - 1, days=day - 1, hours=ed.hour, minutes=ed.minute, seconds=ed.second) course_time[loc].append((first_day_of_semester + st_offset, first_day_of_semester + ed_offset)) # print(f"{loc}: {course_time[loc][-1]}") return course_time
def main(): # try to open yaml config file strConfig = open( configFile ) if not strConfig: print "Error reading configfile", configFile exit(1) # try to parse yaml config string cfg = yaml.load( strConfig ) if not cfg: print "Error parsing yaml config file" exit(1) # check if all required config keys are available for key in ['mysql_host', 'mysql_user', 'mysql_pass', 'mysql_name']: if key not in cfg: print "Missing parameter", key, "in configuration" exit(1) # create database connection dbcron = MySQLdb.connect ( host=cfg['mysql_host'], user=cfg['mysql_user'], passwd=cfg['mysql_pass'], db=cfg['mysql_name'], cursorclass=MySQLdb.cursors.SSDictCursor ) dbcron2 = MySQLdb.connect ( host=cfg['mysql_host'], user=cfg['mysql_user'], passwd=cfg['mysql_pass'], db=cfg['mysql_name'], cursorclass=MySQLdb.cursors.SSDictCursor ) dbcursor = dbcron.cursor() collector = {} dbcursor.execute("SELECT DISTINCT nickname FROM mac_to_nick") for entry in dbcursor: nickname = str.lower( entry['nickname'] ) objTimetable = TimeTable( nickname, dbcron2 ) collector[ nickname ] = objTimetable.getTimeTable() dbcursor.close() dbcron.close() for nick, table in collector.iteritems(): getRelation( nick, collector )
class Data: Paris = City("Paris",48.85,2.35) Berlin = City("Berlin",52.52,13.40) Hamburg = City("Hamburg",53.55,9.99) Munich = City("Munich",48.13,11.58) Amsterdam = City("Amsterdam",52.36,4.89) Brussels = City("Brussels",50.85,4.35) Warsaw = City("Warsaw",52.22,21.01) Flights1 = Flight("11:30","1:30",1,["tue","fri"]) Flights2 = Flight("8:00","10:00",2,["wed","sat"]) Flights3 = Flight("2:00","3:00",3,["tue","thu"]) Flights4 = Flight("9:00","10:00",4,["fri","sat"]) Flights5 = Flight("8:00","10:00",5,["tue","wed"]) Flights6 = Flight("4:00","5:30",6,["tue","wed"]) Flights7 = Flight("1:00","2:00",7,["wed","sat"]) Flights8 = Flight("2:00","5:00",8,["wed"]) Flights9 = Flight("6:00","9:00",9,["wed"]) Flights10 = Flight("6:00","8:00",10,["wed","fri"]) Paris_Berlin = TimeTable(Paris,Berlin,[Flights1,Flights2]) Paris_Brussels = TimeTable(Paris,Brussels,[Flights9]) Brussels_Amsterdam = TimeTable(Brussels,Amsterdam,[Flights3,Flights4]) Amsterdam_Berlin = TimeTable(Amsterdam,Berlin,[Flights3,Flights5]) Amsterdam_Hamburg = TimeTable(Amsterdam,Hamburg,[Flights6,Flights5]) Berlin_Munich = TimeTable(Berlin,Munich,[Flights7,Flights8]) Hamburg_Warsaw = TimeTable(Hamburg,Warsaw,[Flights1,Flights9]) Munich_Warsaw = TimeTable(Munich,Warsaw,[Flights2,Flights10]) Cities = [Paris, Berlin, Hamburg, Munich, Amsterdam, Brussels, Warsaw] TimeTables = [Paris_Berlin, Paris_Brussels, Brussels_Amsterdam, Amsterdam_Berlin, Amsterdam_Hamburg, Berlin_Munich, Hamburg_Warsaw, Munich_Warsaw] def calculateDistance(self,departure,destination): earth_radius = 6373.0 latitude1 = math.radians(departure.latitude) latitude2 = math.radians(destination.latitude) longitude1 = math.radians(departure.longitude) longitude2 = math.radians(destination.longitude) difference_latitude = abs(latitude1-latitude2) difference_longitude = abs(longitude1-longitude2) dummy = math.sin(difference_latitude / 2) ** 2 + math.cos(latitude1) * math.cos(latitude2) * math.sin(difference_longitude / 2) ** 2 dummy2 = 2 * math.atan2(math.sqrt(dummy), math.sqrt(1 - dummy)) return earth_radius*dummy2 def calculateTime(self,time1,time2): time1_list = time1.split(":") time2_list = time2.split(":") dummy1 = abs(int(time1_list[0]) - int(time2_list[0])) dummy2 = abs(int(time1_list[1]) - int(time2_list[1]))/60 return dummy1 + dummy2 def mapDays(self,Day): if Day == "Sunday": return "sun" elif Day == "Monday": return "mon" elif Day == "Tuesday": return "tue" elif Day == "Wednesday": return "wed" elif Day == "Thursday": return "thu" elif Day == "Friday": return "fri" else: return "sat" def getRange(self,Day1,Day2): daysArray = ["sat","sun","mon","tue","wed","thu","fri"] index1 = daysArray.index(Day1) index2 = daysArray.index(Day2) retList = [] retList2 = [] if index1 == index2: retList = [Day1] return retList elif index1 < index2: for i in range(len(daysArray)): if i>=index1 and i<=index2: retList.append(daysArray[i]) return retList else: for i in range(len(daysArray)): if i>=index2 and i<=index1: retList.append(daysArray[i]) retList2.append(Day1) for i in range(len(daysArray)): if daysArray[i] not in retList: retList2.append(daysArray[i]) retList2.append(Day2) return retList2 def getPrevious(self,Day): if Day == "sat": return "fri" elif Day == "sun": return "sat" elif Day == "mon": return "sun" elif Day == "tue": return "mon" elif Day == "wed": return "tue" elif Day == "thu": return "wed" elif Day == "fri": return "thu" def getNext(self,Day): if Day == "sat": return "sun" elif Day == "sun": return "mon" elif Day == "mon": return "tue" elif Day == "tue": return "wed" elif Day == "wed": return "thu" elif Day == "thu": return "fri" elif Day == "fri": return "sat"
def get_data(): parser = argparse.ArgumentParser(description = 'TimeTable Scheduler') parser.add_argument('path', default = "", help = 'Absolute path to the file') parser.add_argument('-v', '--verbose', action = 'count', help = 'increase output verbosity') args = parser.parse_args() path = args.path if not os.path.isfile(path): print("Not a valid file path!") exit(1) fileName, fileExtension = os.path.splitext(path) if not fileName or fileExtension != ".csv": print(os.path.basename(path), "is not a csv") exit(1) df = pd.read_csv(path) if args.verbose: print("\n\033[0;34mDataframe: \033[0m\n\n",df) ##initialize teachers teachers = df['teacher'].unique() for i, teacher in enumerate(teachers): teachers[i] = Teacher(i, teacher) if args.verbose: print("\n\033[0;34mTeachers: \033[0m\n") for teacher in teachers: print(teacher.id, teacher.name, teacher) ##initialize subjects subjects = df['subject'] for i, row in enumerate(df.values): teacher = None for t in teachers: if t.name == row[1]: teacher = t break tHours = row[2] pHours = row[3] subjects[i] = Subject(i, row[0], teacher, tHours, pHours) #TODO: fix warning - (enable warning at end of code) if args.verbose: print("\n\033[0;34mSubjects: \033[0m\n") for subject in subjects: print(subject.id, subject.name, subject.teacher.name, subject.theoryHours, subject.practicalHours) ##initialize course #TODO : dynamically calculate unique_courses unique_courses = ['MCA-1','MCA-3','MCS-1','MCS-3'] courses = [] for i, course in enumerate(unique_courses): regex = r'^' + re.escape(course) temp_subs = [subject for subject in subjects if re.match(regex, subject.name)] courses.append(Course(i, course, temp_subs)) if args.verbose: print("\n\033[0;34mCourses: \033[0m\n") for course in courses: print(course.id, course.name, [subject.name for subject in course.subjects]) ##initialize timetables timetables = [] for course in courses: timetables.append(TimeTable(course)) if args.verbose: print("\n\033[0;34mTimetables: \033[0m\n") for timetable in timetables: print(timetable.course.name, [slot.subject.name \ for slot in timetable.slots if slot.subject is not None] + [slot.subject for slot in timetable.slots if slot.subject is None]) return teachers, subjects, unique_courses, courses, timetables
from timetable import TimeTable TimeTable()
def read_data(): ''' this function is used to read data from a csv file which has data of type subject teacher theory lab MCA-101 NK 2 3 ''' path="data.csv" data_frame = pd.read_csv(path) #initializing teachers teachers = data_frame['teacher'].unique() for i, teacher in enumerate(teachers): teachers[i] = Teacher(i, teacher) #converting teachers list of strings into list of objects #displaying teachers for teacher in teachers: print(teacher.id, teacher.name) #initializing subjects subjects = data_frame['subject'] for i, row in enumerate(data_frame.values): teacher = None for p in teachers: if p.name == row[1]: teacher = p break theory_slots = row[2] practical_slots = row[3] subjects[i] = Subject(i, row[0], teacher, theory_slots, practical_slots) #subject objects list with each subject having the teacher theory and practical hours. #displaying subjects for subject in subjects: print(subject.id, subject.name, subject.teacher.name, subject.theory_slots, subject.practical_slots) ##initialize course #TODO : dynamically calculate unique_courses unique_courses = ['MCA-1','MCA-3','MCS-1','MCS-3'] courses = [] for i, course in enumerate(unique_courses): reg = r'^' + re.escape(course) subs = [sub for sub in subjects if re.match(reg, sub.name)] courses.append(Course(i, course, subs)) #displaying courses for course in courses: print(course.id, course.name, [subject.name for subject in course.subjects]) #initializing timetables by assigning courses as it is timetables = [] for course in courses: timetables.append(TimeTable(course)) #displaying timetables for timetable in timetables: print(timetable.course.name, [slot.subject.name \ for slot in timetable.slots if slot.subject is not None] + [slot.subject for slot in timetable.slots if slot.subject is None]) return teachers, subjects, unique_courses, courses, timetables, path
from timetable import TimeTable timetable1 = TimeTable() timetable2 = TimeTable() timetable1.monday = ["7 to 12", "13 to 15", "16 to 18"] timetable2.monday = ["8 to 10", "14 to 16", "17 to 18"] NUMBER_OF_HOURS = 11 hour_taken_array = [] # initialize hour_taken_array to zeros for one_hour_period in range(NUMBER_OF_HOURS): hour_taken_array.append(0) hours_string_prompt = "" for hour in range(7, 19): hours_string_prompt = hours_string_prompt + str(hour) + "\t" # split the scheduled periods to one hour periods def split_into_one_hour_components(day_schedule): # arrays to store the one hour periods schedule_time_pairs = [] for schedule in day_schedule: # first split into given hours timeScheduleList = schedule.split() startHour = int(timeScheduleList[0])