Browse Source

Extensive testing of disk_cache and new terminal tables

no_compress
Raphael Roberts 7 years ago
parent
commit
d5dae075d3
  1. 10
      ctabus.py
  2. 6
      disk_cache.py
  3. 13
      main.py
  4. 16
      print2d.py

10
ctabus.py

@ -38,4 +38,12 @@ def get_directions(route, api_key=api, timeout=None):
@disk_cache
def get_stops(route, direction, api_key=api, timeout=None):
return get_data('getstops', api_key, rt=route, dir=direction, timeout=timeout)
return get_data('getstops', api_key, rt=route, dir=direction,
timeout=timeout)
@disk_cache
def get_name_from_direction(route, direction, api_key=api, timeout=None):
test_stop = get_stops(route, direction, api_key=api_key,
timeout=timeout)['stops'][0]['stpid']
return get_times(test_stop, api_key=api, timeout=timeout)['prd'][0]['des']

6
disk_cache.py

@ -13,8 +13,8 @@ def make_key(*args, **kwargs):
class disk_cache:
"""Decorator to make persistent cache"""
caches = []
"""Decorator to make a function with lru_cache that can be written to disk"""
def __init__(self, func):
self.fname = "{}.{}.dc".format(func.__module__, func.__name__)
@ -26,8 +26,10 @@ class disk_cache:
def __call__(self, *args, **kwargs):
key = make_key(*args, **kwargs)
try:
return self.cache[key]
res = self.cache[key]
return res
except KeyError:
self.fresh = True
res = self.func(*args, **kwargs)
self.cache[key] = res
return res

13
main.py

@ -1,7 +1,7 @@
#!/usr/bin/python3
from dateutil.parser import parse as date_parse
from dateutil import tz
from disk_cache import disk_cache
from disk_cache import disk_cache, make_key
import argparse
import ctabus
import datetime
@ -67,6 +67,7 @@ def pprint_delta(delta):
def gen_list(objs, data, *displays, key=None, sort=0, num_pic=True):
from print2d import create_table, render_table
# sort based on column number
k = displays[sort]
display_data = {obj[k]: obj[data] for obj in objs}
srt_keys = sorted(display_data.keys(), key=key)
@ -152,7 +153,11 @@ def main(args):
data = ctabus.get_directions(route)['directions']
# direction
if not args.direction:
direction = gen_list(data, 'dir', 'dir')
for direction_obj in data:
friendly_name = ctabus.get_name_from_direction(
route, direction_obj['dir'])
direction_obj['friendly_name'] = friendly_name
direction = gen_list(data, 'dir', 'dir', 'friendly_name')
else:
s = Search(args.direction)
direction = sorted((obj['dir'] for obj in data), key=s)[0]
@ -167,6 +172,10 @@ def main(args):
else:
stop_id = args.arg
data = ctabus.get_times(stop_id)
info = data['prd'][0]
key = make_key(info['rt'], info['rtdir'], ctabus.api, None)
if key not in ctabus.get_name_from_direction.cache.keys():
ctabus.get_name_from_direction.cache[key] = info['des']
if args.periodic is not None:
_done = False
while not _done:

16
print2d.py

@ -1,11 +1,10 @@
from terminaltables.terminal_io import terminal_size
from terminaltables import AsciiTable
from textwrap import fill
from pydoc import pipepager, tempfilepager, plainpager, plain
import datetime
import os
import sys
import pydoc
from pydoc import pager, pipepager, tempfilepager, plainpager, plain
from textwrap import fill
from terminaltables import AsciiTable
from terminaltables.terminal_io import terminal_size
def getpager():
@ -32,9 +31,6 @@ def getpager():
return lambda text: pipepager(text, 'less -X')
pydoc.getpager = getpager
def str_coerce(s, **kwargs):
if isinstance(s, datetime.datetime):
return s.strftime(kwargs['datetime_format'])
@ -68,7 +64,7 @@ def render_table(table: AsciiTable, interactive=True):
data[row_num][col_num] = fill(
col_data, table.column_max_width(col_num))
if interactive:
pager = getpager()
pager(table.table)
else:
print(table.table)
print(table.table
Loading…
Cancel
Save