From d5dae075d38074cd8ff949fdae535ea7eda6befd Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Tue, 2 Apr 2019 20:09:37 -0500 Subject: [PATCH] Extensive testing of disk_cache and new terminal tables --- ctabus.py | 10 +++++++++- disk_cache.py | 6 ++++-- main.py | 13 +++++++++++-- print2d.py | 16 ++++++---------- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/ctabus.py b/ctabus.py index 8491553..6e968cd 100644 --- a/ctabus.py +++ b/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'] diff --git a/disk_cache.py b/disk_cache.py index a5e84d4..b5a9b3c 100644 --- a/disk_cache.py +++ b/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 diff --git a/main.py b/main.py index d867493..e73b9af 100755 --- a/main.py +++ b/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: diff --git a/print2d.py b/print2d.py index ce702dd..67cf1ce 100644 --- a/print2d.py +++ b/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