Browse Source

Added paging print2d function

no_compress
Raphael Roberts 7 years ago
parent
commit
6a35e23cbe
  1. 1
      .gitignore
  2. 7
      ctabus.py
  3. 40
      print2d.py
  4. 2
      requirements.txt

1
.gitignore

@ -1,4 +1,5 @@
/cta_api_key.json
/output
__pycache__
*.pyc
*_out.json

7
ctabus.py

@ -3,9 +3,11 @@ from requests import get
import json
import datetime
import argparse
from editdistance import eval as editdistance
test = False
test = True
api = "***REMOVED***"
def get_data(type,api_key = api,**args):
base_url = "http://www.ctabustracker.com/bustime/api/v2/{type}?{query}"
args['key'] = api_key
@ -17,16 +19,21 @@ def get_data(type,api_key = api,**args):
data = json.loads(response.text)
return data['bustime-response']
# print(url)
def print2d(values):
pass
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)
if __name__ == "__main__":
if test:
data = get_times('4752')

40
print2d.py

@ -0,0 +1,40 @@
import datetime
from pydoc import pager
def str_coerce(s,**kwargs):
if isinstance(s,datetime.datetime):
return s.strftime(kwargs['datetime_format'])
else:
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):
try:
max_col[i] = max(max_col[i],len(col))
except IndexError:
max_col.append(len(col))
if l_end and r_end:
fmt_row = f'{l_end} {{content}} {r_end}'
else:
fmt_row = '{content}'
done = []
for row in l:
content = seperator.join(col.ljust(max_col[i],spacer) 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')
else:
row_sep = '\n'
final = row_sep.join(done)
if bottom:
final = '\n'.join((bottom,final,bottom))
if interactive:
pager(final)
else:
return final

2
requirements.txt

@ -0,0 +1,2 @@
editdistance
requests
Loading…
Cancel
Save