From 24dddc8a684cb55c733412d6762ccf0c2befae35 Mon Sep 17 00:00:00 2001 From: Florian Schweikert Date: Sun, 23 Oct 2011 22:46:22 +0200 Subject: [PATCH] caching for lines and stations (saved as json) --- gotovienna/cache.py | 14 ++++++-------- gotovienna/realtime.py | 16 ++++++++-------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/gotovienna/cache.py b/gotovienna/cache.py index edb6677..eae95b1 100644 --- a/gotovienna/cache.py +++ b/gotovienna/cache.py @@ -29,10 +29,10 @@ class Lines(dict): def __init__(self): self.load() - def update(self, *args, **kwargs): - a = dict.update(self, *args, **kwargs) + def __setitem__(self, *args, **kwargs): + s = dict.__setitem__(self, *args, **kwargs) self.save() - return a + return s def save(self): with open(defaults.cache_lines, 'w') as fp: @@ -45,7 +45,6 @@ class Lines(dict): lines = Lines() - class Stations(dict): stations = None @@ -63,10 +62,9 @@ class Stations(dict): # FIXME maybe cause problems in the future, race conditions Stations.stations[line] = self - def update(self, *args, **kwargs): - u = dict.update(self, *args, **kwargs) - if args[0]: - Stations.save() + def __setitem__(self, *args, **kwargs): + u = dict.__setitem__(self, *args, **kwargs) + Stations.save() return u @classmethod diff --git a/gotovienna/realtime.py b/gotovienna/realtime.py index d0f276a..73f927c 100644 --- a/gotovienna/realtime.py +++ b/gotovienna/realtime.py @@ -6,6 +6,8 @@ from datetime import time import re import collections from errors import LineNotFoundError, StationNotFoundError +import cache +from cache import Stations from gotovienna import defaults @@ -40,19 +42,18 @@ class Departure: class ITipParser: def __init__(self): - self._stations = {} - self._lines = {} + self._lines = cache.lines def get_stations(self, name): """ Get station by direction {'Directionname': [('Station name', 'url')]} """ - if not self._stations.has_key(name): - st = {} + if not name in self.lines: + return {} - if not self.lines.has_key(name): - return None + st = Stations(name) + if not st: bs = BeautifulSoup(urlopen(self.lines[name])) tables = bs.findAll('table', {'class': 'text_10pix'}) for i in range(2): @@ -66,9 +67,8 @@ class ITipParser: sta.append((tr.text.strip(' '), None)) st[dir] = sta - self._stations[name] = st - return self._stations[name] + return st @property def lines(self): -- 1.7.9.5