diff options
author | Markus Holtermann <info@markusholtermann.eu> | 2013-01-26 01:30:49 +0100 |
---|---|---|
committer | Markus Holtermann <info@markusholtermann.eu> | 2013-01-26 01:30:49 +0100 |
commit | 8b5266b2999e4a18372c41c718044a44e089c525 (patch) | |
tree | 94598f9e4ef84fc7c15dd95965e564500bd3eb0b /bvggrabber | |
parent | 97ec685d169289147cc4d46d89a376f283f119c7 (diff) | |
download | bvg-grabber-8b5266b2999e4a18372c41c718044a44e089c525.tar.gz bvg-grabber-8b5266b2999e4a18372c41c718044a44e089c525.tar.bz2 bvg-grabber-8b5266b2999e4a18372c41c718044a44e089c525.zip |
Finally fix the remaining seconds behaviour of ``bvggrabber.api.Departure`` and add appropriate test cases
Diffstat (limited to 'bvggrabber')
-rw-r--r-- | bvggrabber/api/__init__.py | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/bvggrabber/api/__init__.py b/bvggrabber/api/__init__.py index 81c4980..c6ad86a 100644 --- a/bvggrabber/api/__init__.py +++ b/bvggrabber/api/__init__.py @@ -11,6 +11,15 @@ fullformat = lambda dt: dt.strftime('%Y-%m-%d %H:%M:%S') hourformat = lambda dt: dt.strftime('%H:%M') +def compute_remaining(start, end): + if not isinstance(start, datetime.datetime): + raise ValueError("start needs to be a datetime.datetime") + if not isinstance(end, datetime.datetime): + raise ValueError("start needs to be a datetime.datetime") + seconds = (end - start).total_seconds() + return datetime.timedelta(minutes=floor(seconds / 60)).total_seconds() + + class QueryApi(object): def __init__(self): @@ -41,24 +50,19 @@ class Departure(object): elif isinstance(when, datetime.datetime): self.when = when else: - ValueError("when must be a valid datetime, timestamp or string!") + raise TypeError("when must be a valid datetime, timestamp or " + "string!") def __str__(self): return "Start: %s, End: %s, when: %s, now: %s, line: %s" % ( - self.start, self.end, hourformat(self.when), hourformat(self.now), - self.line) + self.start.decode('iso-8859-1'), self.end, hourformat(self.when), + hourformat(self.now), self.line) @property def remaining(self): - td = self.when - self.now - seconds = (td / 60).total_seconds() - if td < datetime.timedelta(seconds=0): - return datetime.timedelta(minutes=floor(seconds)) - elif td > datetime.timedelta(seconds=0): - return datetime.timedelta(minutes=ceil(seconds)) - else: - return datetime.timedelta(seconds=0) + return compute_remaining(self.now, self.when) + @property def to_json(self): return json.dumps({'start': self.start.decode('iso-8859-1'), 'end': self.end, @@ -67,4 +71,4 @@ class Departure(object): 'now_hour': hourformat(self.now), 'when_full': fullformat(self.when), 'when_hour': hourformat(self.when), - 'remaining': round(self.remaining.total_seconds())}) + 'remaining': round(self.remaining)}) |