diff --git a/ctabus.py b/ctabus.py index 833b05a..8c3175c 100644 --- a/ctabus.py +++ b/ctabus.py @@ -17,16 +17,16 @@ def get_data(type,api_key = api,**args): raise Exception(str(data["error"])) except KeyError: return data - + def get_times(stop_id,api_key = api): return get_data('getpredictions',api_key,stpid=stop_id) - + def get_routes(api_key = api): return get_data('getroutes',api_key) - + def get_directions(route,api_key = api): return get_data('getdirections',api_key,rt=route) - + def get_stops(route,direction,api_key = api): return get_data('getstops',api_key,rt = route,dir=direction) diff --git a/main.py b/main.py index 8793657..c128523 100644 --- a/main.py +++ b/main.py @@ -12,7 +12,7 @@ def numb_sort(str): except Exception as E: print(str) raise E - + def pprint_delta(delta): delta = str(delta) days= None @@ -39,7 +39,7 @@ def pprint_delta(delta): ret = days + ', ' if time else '' ret += time return ret - + def gen_list(objs,data,*displays,key = None,sort = 0,num_pic = True): k = displays[sort] display_data = {obj[k]:obj[data] for obj in objs} @@ -53,7 +53,7 @@ def gen_list(objs,data,*displays,key = None,sort = 0,num_pic = True): ) if num_pic: display = [[i] + data for i,data in enumerate(display)] - + opts = { 'spacer':' ', 'seperator':' ', @@ -79,7 +79,7 @@ def gen_list(objs,data,*displays,key = None,sort = 0,num_pic = True): except KeyError: pass return ret - + config = '''\ {route} - {end} ({direction}) {nm}, stop {stop_id} @@ -90,6 +90,7 @@ if __name__ == "__main__": parser.add_argument('arg',nargs = '+',metavar = '(stop-id | cross streets)') parser.add_argument('-r','--route',default = None) parser.add_argument('-d','--direction',default = None) + parser.add_argument('-l','--lucky',action='store_true',help = 'picks first result') args = parser.parse_args() args.arg = ' '.join(args.arg) if not args.arg.isdecimal(): @@ -109,7 +110,10 @@ if __name__ == "__main__": #direction stops = ctabus.get_stops(route,direction)['stops'] s = StopSearch(args.arg) - stop_id = gen_list(stops,'stpid','stpnm',key = s) + if args.lucky: + stop_id = sorted(stops,key=lambda stop: s(stop['stpnm']))[0]['stpid'] + else: + stop_id = gen_list(stops,'stpid','stpnm',key = s) else: stop_id = args.arg times = ctabus.get_times(stop_id)['prd'] diff --git a/print2d.py b/print2d.py index 39a545b..3efee86 100644 --- a/print2d.py +++ b/print2d.py @@ -7,7 +7,7 @@ def str_coerce(s,**kwargs): return str(s) def print2d(l,datetime_format = "%A, %B %e, %Y %H:%M:%S",seperator= ' | ',spacer = ' ',bottom = '=',l_end = '|',r_end = '|',interactive = False): l = [[str_coerce(s,datetime_format = datetime_format) for s in row] for row in l] - + max_col = [] for row in l: for i,col in enumerate(row): @@ -15,18 +15,18 @@ def print2d(l,datetime_format = "%A, %B %e, %Y %H:%M:%S",seperator= ' | ',spacer max_col[i] = max(max_col[i],len(col)) except IndexError: max_col.append(len(col)) - + fmt_row = '{content}' if l_end: fmt_row = f'{l_end} ' + fmt_row if r_end: fmt_row = fmt_row + f' {r_end}' - + done = [] for row in l: content = seperator.join(col.ljust(max_col[i],spacer if i < len(row)-1 or r_end else ' ') for i,col in enumerate(row)) done.append(fmt_row.format(content = content)) - + if bottom: bottom = bottom*len(done[0]) row_sep = ('\n'+bottom+'\n') diff --git a/search.py b/search.py index 03ea6c9..70bc920 100644 --- a/search.py +++ b/search.py @@ -28,6 +28,8 @@ class StopSearch(Search): if paren: paren = paren.group('data') ret.append(editdistance(self.query,paren)) + if self.raw_lower in stop: + ret = (item - 100 for item in ret) return min( ret ) @@ -40,4 +42,4 @@ if __name__ == "__main__": names = [stop['stpnm'] for stop in data['stops']] while True: q = StopSearch(input('Search: ')) - print('\n'.join(sorted(names,key=q))) \ No newline at end of file + print('\n'.join(sorted(names,key=q)),end='\n'*3) \ No newline at end of file