def departures(command): place1 = command.split()[1] place2 = command.split()[2] t1 = convert_time(command.split()[3]) t2 = convert_time(command.split()[4]) index = places.index(place2) times2 = data[index].split(",") converted_times = [convert_time(time.strip()) for time in times2] converted_times2 = [x for x in converted_times if x >= t1 and x <= t2] try: loc1 = converted_times.index(converted_times2[0]) loc2 = converted_times.index(converted_times2[-1]) #departures index = places.index(place1) times1 = data[index].split(",") converted_times1 = [convert_time(time.strip()) for time in times1] ouput_times = converted_times1[loc1:loc2 + 1] ouput_times = [convert_time_list(x) for x in ouput_times] if len(ouput_times) == 0: print("Sorry, no departures found") else: print( "Buses arriving at %s in that period depart from %s at" % (place2, place1), ', '.join(map(str, ouput_times))) except (IndexError): print("Sorry, no departures found")
def parse_schedule_table(table): # make sure we don't have any cells that span columns, since that would # be trickier assert len(table.cssselect('td[colspan],th[colspan]')) == 0 trs = table.cssselect('tr') header = trs[0] del trs[0] col_headers = [e.text_content() for e in header.cssselect('td,th')] del col_headers[0] # not a train number data = [] for tr in trs: stop_name = tr.cssselect('th')[0].text_content() tds = [e.text_content() for e in tr.cssselect('td')] prev_hour = 0 for (train_num, time) in zip(col_headers, tds): time = time.strip() match = re.match(r'(\d+):(\d+)', time) if match: (hour, minute) = match.groups() hour = int(hour) ; minute = int(minute) if hour < prev_hour and hour != 12: hour += 12 prev_hour = hour data.append( (train_num, stop_name, hour, minute) ) return data
def get(self): self.response.headers['Content-Type'] = 'text/plain' kind = self.request.GET['kind'].strip() bssid = self.request.GET['bssid'].strip() probe = self.request.get_all('probed') power = self.request.GET['power'].strip() essid = self.request.GET['essid'].strip() timeRanges = self.request.get_all('times') curTimes = [] probedEssid = [] for probed in probe: probed.encode('ascii','ignore') probed = probed.strip() probedEssid.append(probed) for time in timeRanges: time.encode('ascii','ignore') time = time.strip() curTimes.append(datetime.strptime(time, "%Y-%m-%d %H:%M:%S") ) power = int(power) if kind == "Router": r = NodeRecord(parent = device_key(bssid), kind = kind, BSSID = bssid, timeRanges = curTimes , lastSeen = curTimes[-1], power = power, ESSID = essid, probedESSID =probedEssid) else: r = NodeRecord(parent = device_key(bssid), kind = kind, BSSID = bssid, timeRanges = curTimes , lastSeen = curTimes[-1], power = power, AP = essid, probedESSID =probedEssid) r_key = r.put()
def arrival(command): place1 = command.split()[1] place2 = command.split()[3] t1 = convert_time(command.split()[2]) index = places.index(place1) times1 = data[index].split(",") converted_times = [convert_time(time.strip()) for time in times1] converted_times1 = [x for x in converted_times if x >= t1] index = places.index(place2) times2 = data[index].split(",") converted_times2 = [convert_time(time.strip()) for time in times2] arrival_time = converted_times2[-len(converted_times1):][0] if len(converted_times1) == 0: print("Sorry, no departures found") else: print("The next bus departing from %s arrives at %s at %s" % (place1, place2, arrival_time.strftime('%H:%M')))
def next_time(command): place = command.split()[1] t1 = convert_time(command.split()[2]) index = places.index(place) times = data[index].split(",") converted_times = [convert_time(time.strip()) for time in times] converted_times = [x for x in converted_times if x >= t1] if len(converted_times) == 0: print("Sorry, no departures found") else: print("The next bus departing from %s is at %s" % (place, converted_times[0].strftime('%H:%M')))
def __init__(self, name, date): # Object attributes self.name = name self.n = len(date) # nb char assert int(date[0:4]) in range(1990, 2100), "Check %s year format." % (name) self.yr = int(date[0:4]) # year assert int(date[4:6]) in range(1, 13), "Check %s month format." % (name) self.mo = int(date[4:6]) # month assert int(date[6:8]) in range(1, 32), "Check %s day format." % (name) self.dy = int(date[6:8]) # day # Assign hours, minutes, seconds if self.n > 8: time = date[8:] # second half of string time = time.strip('T') time = time.strip(':') # fmt self.hr = int(time[0:2]) # hours self.mn = int(time[2:4]) # minutes self.sc = int(time[4:6]) # seconds else: self.hr = 00 # hours self.mn = 00 # minutes self.sc = 00 # seconds
def get(self): self.response.headers['Content-Type'] = 'text/plain' update = dict() curTimes = [] probedEssid = [] try: self.response.headers['Content-Type'] = 'text/plain' update['kind'] = self.request.GET['kind'].strip() update['bssid'] = self.request.GET['bssid'].strip() update['power'] = self.request.GET['power'].strip() update['essid'] = self.request.GET['essid'].strip() probe = self.request.get_all('probed') timeRanges = self.request.get_all('times') for probed in probe: probed.encode('ascii','ignore') probed = probed.strip() probedEssid.append(probed) update['probed'] = probedEssid for time in timeRanges: time.encode('ascii','ignore') time = time.strip() curTimes.append(datetime.strptime(time, "%Y-%m-%d %H:%M:%S") ) except KeyError: #bail if there is no argument for 'devicename' submitted self.response.write ('Error with update parameters') else: nodeToUpdate = NodeRecord.updateNode(update) nodeToUpdate.power = int(update['power']) nodeToUpdate.lastSeen = curTimes[-1] # NOTE: Commenting out this because it seems redundant to strore ranges for the real time app. # Can always put it back for data analyis # nodeToUpdate.timeRanges = [] # for t in update['time']: # nodeToUpdate.timeRanges.append(t) if nodeToUpdate.kind == "Client": nodeToUpdate.AP = update['essid'] r_key = nodeToUpdate.put() self.response.write("Updated")
def get(self): self.response.headers['Content-Type'] = 'text/plain' update = dict() curTimes = [] probedEssid = [] try: self.response.headers['Content-Type'] = 'text/plain' update['kind'] = self.request.GET['kind'].strip() update['bssid'] = self.request.GET['bssid'].strip() update['power'] = self.request.GET['power'].strip() update['essid'] = self.request.GET['essid'].strip() probe = self.request.get_all('probed') timeRanges = self.request.get_all('times') for probed in probe: probed.encode('ascii', 'ignore') probed = probed.strip() probedEssid.append(probed) update['probed'] = probedEssid for time in timeRanges: time.encode('ascii', 'ignore') time = time.strip() curTimes.append(datetime.strptime(time, "%Y-%m-%d %H:%M:%S")) except KeyError: #bail if there is no argument for 'devicename' submitted self.response.write('Error with update parameters') else: nodeToUpdate = NodeRecord.updateNode(update) nodeToUpdate.power = int(update['power']) nodeToUpdate.lastSeen = curTimes[-1] # NOTE: Commenting out this because it seems redundant to strore ranges for the real time app. # Can always put it back for data analyis # nodeToUpdate.timeRanges = [] # for t in update['time']: # nodeToUpdate.timeRanges.append(t) if nodeToUpdate.kind == "Client": nodeToUpdate.AP = update['essid'] r_key = nodeToUpdate.put() self.response.write("Updated")
def get(self): self.response.headers['Content-Type'] = 'text/plain' kind = self.request.GET['kind'].strip() bssid = self.request.GET['bssid'].strip() probe = self.request.get_all('probed') power = self.request.GET['power'].strip() essid = self.request.GET['essid'].strip() timeRanges = self.request.get_all('times') curTimes = [] probedEssid = [] for probed in probe: probed.encode('ascii', 'ignore') probed = probed.strip() probedEssid.append(probed) for time in timeRanges: time.encode('ascii', 'ignore') time = time.strip() curTimes.append(datetime.strptime(time, "%Y-%m-%d %H:%M:%S")) power = int(power) if kind == "Router": r = NodeRecord(parent=device_key(bssid), kind=kind, BSSID=bssid, timeRanges=curTimes, lastSeen=curTimes[-1], power=power, ESSID=essid, probedESSID=probedEssid) else: r = NodeRecord(parent=device_key(bssid), kind=kind, BSSID=bssid, timeRanges=curTimes, lastSeen=curTimes[-1], power=power, AP=essid, probedESSID=probedEssid) r_key = r.put()