Browse Source

Results where original query is in result bumps it to the top; added "feeling lucky" option

no_compress
Raphael Roberts 7 years ago
parent
commit
1801f06749
  1. 8
      ctabus.py
  2. 14
      main.py
  3. 8
      print2d.py
  4. 4
      search.py

8
ctabus.py

@ -17,16 +17,16 @@ def get_data(type,api_key = api,**args):
raise Exception(str(data["error"])) raise Exception(str(data["error"]))
except KeyError: except KeyError:
return data return data
def get_times(stop_id,api_key = api): def get_times(stop_id,api_key = api):
return get_data('getpredictions',api_key,stpid=stop_id) return get_data('getpredictions',api_key,stpid=stop_id)
def get_routes(api_key = api): def get_routes(api_key = api):
return get_data('getroutes',api_key) return get_data('getroutes',api_key)
def get_directions(route,api_key = api): def get_directions(route,api_key = api):
return get_data('getdirections',api_key,rt=route) return get_data('getdirections',api_key,rt=route)
def get_stops(route,direction,api_key = api): def get_stops(route,direction,api_key = api):
return get_data('getstops',api_key,rt = route,dir=direction) return get_data('getstops',api_key,rt = route,dir=direction)

14
main.py

@ -12,7 +12,7 @@ def numb_sort(str):
except Exception as E: except Exception as E:
print(str) print(str)
raise E raise E
def pprint_delta(delta): def pprint_delta(delta):
delta = str(delta) delta = str(delta)
days= None days= None
@ -39,7 +39,7 @@ def pprint_delta(delta):
ret = days + ', ' if time else '' ret = days + ', ' if time else ''
ret += time ret += time
return ret return ret
def gen_list(objs,data,*displays,key = None,sort = 0,num_pic = True): def gen_list(objs,data,*displays,key = None,sort = 0,num_pic = True):
k = displays[sort] k = displays[sort]
display_data = {obj[k]:obj[data] for obj in objs} 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: if num_pic:
display = [[i] + data for i,data in enumerate(display)] display = [[i] + data for i,data in enumerate(display)]
opts = { opts = {
'spacer':' ', 'spacer':' ',
'seperator':' ', 'seperator':' ',
@ -79,7 +79,7 @@ def gen_list(objs,data,*displays,key = None,sort = 0,num_pic = True):
except KeyError: except KeyError:
pass pass
return ret return ret
config = '''\ config = '''\
{route} - {end} ({direction}) {route} - {end} ({direction})
{nm}, stop {stop_id} {nm}, stop {stop_id}
@ -90,6 +90,7 @@ if __name__ == "__main__":
parser.add_argument('arg',nargs = '+',metavar = '(stop-id | cross streets)') parser.add_argument('arg',nargs = '+',metavar = '(stop-id | cross streets)')
parser.add_argument('-r','--route',default = None) parser.add_argument('-r','--route',default = None)
parser.add_argument('-d','--direction',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 = parser.parse_args()
args.arg = ' '.join(args.arg) args.arg = ' '.join(args.arg)
if not args.arg.isdecimal(): if not args.arg.isdecimal():
@ -109,7 +110,10 @@ if __name__ == "__main__":
#direction #direction
stops = ctabus.get_stops(route,direction)['stops'] stops = ctabus.get_stops(route,direction)['stops']
s = StopSearch(args.arg) 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: else:
stop_id = args.arg stop_id = args.arg
times = ctabus.get_times(stop_id)['prd'] times = ctabus.get_times(stop_id)['prd']

8
print2d.py

@ -7,7 +7,7 @@ def str_coerce(s,**kwargs):
return str(s) return str(s)
def print2d(l,datetime_format = "%A, %B %e, %Y %H:%M:%S",seperator= ' | ',spacer = ' ',bottom = '=',l_end = '|',r_end = '|',interactive = False): 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] l = [[str_coerce(s,datetime_format = datetime_format) for s in row] for row in l]
max_col = [] max_col = []
for row in l: for row in l:
for i,col in enumerate(row): 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)) max_col[i] = max(max_col[i],len(col))
except IndexError: except IndexError:
max_col.append(len(col)) max_col.append(len(col))
fmt_row = '{content}' fmt_row = '{content}'
if l_end: if l_end:
fmt_row = f'{l_end} ' + fmt_row fmt_row = f'{l_end} ' + fmt_row
if r_end: if r_end:
fmt_row = fmt_row + f' {r_end}' fmt_row = fmt_row + f' {r_end}'
done = [] done = []
for row in l: 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)) 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)) done.append(fmt_row.format(content = content))
if bottom: if bottom:
bottom = bottom*len(done[0]) bottom = bottom*len(done[0])
row_sep = ('\n'+bottom+'\n') row_sep = ('\n'+bottom+'\n')

4
search.py

@ -28,6 +28,8 @@ class StopSearch(Search):
if paren: if paren:
paren = paren.group('data') paren = paren.group('data')
ret.append(editdistance(self.query,paren)) ret.append(editdistance(self.query,paren))
if self.raw_lower in stop:
ret = (item - 100 for item in ret)
return min( return min(
ret ret
) )
@ -40,4 +42,4 @@ if __name__ == "__main__":
names = [stop['stpnm'] for stop in data['stops']] names = [stop['stpnm'] for stop in data['stops']]
while True: while True:
q = StopSearch(input('Search: ')) q = StopSearch(input('Search: '))
print('\n'.join(sorted(names,key=q)))
print('\n'.join(sorted(names,key=q)),end='\n'*3)
Loading…
Cancel
Save