diff options
author | Markus Holtermann <info@markusholtermann.eu> | 2013-01-30 20:56:55 +0100 |
---|---|---|
committer | Markus Holtermann <info@markusholtermann.eu> | 2013-01-30 20:56:55 +0100 |
commit | 2443c414af8901e89b1b55e126ab4867c2cf91e4 (patch) | |
tree | 54e734c0b42358c5c066cfe872d7c73d99ad77e3 /bvggrabber | |
parent | 41af0a769e8896b6df5baccfff31d5ccea3f7da8 (diff) | |
download | bvg-grabber-2443c414af8901e89b1b55e126ab4867c2cf91e4.tar.gz bvg-grabber-2443c414af8901e89b1b55e126ab4867c2cf91e4.tar.bz2 bvg-grabber-2443c414af8901e89b1b55e126ab4867c2cf91e4.zip |
Move hourformat and fullformat to separate module. Add and adjust tests
Diffstat (limited to 'bvggrabber')
-rw-r--r-- | bvggrabber/api/__init__.py | 8 | ||||
-rw-r--r-- | bvggrabber/utils/format.py | 35 |
2 files changed, 37 insertions, 6 deletions
diff --git a/bvggrabber/api/__init__.py b/bvggrabber/api/__init__.py index 8568d00..ba68450 100644 --- a/bvggrabber/api/__init__.py +++ b/bvggrabber/api/__init__.py @@ -4,13 +4,11 @@ import json import re from functools import total_ordering -from math import ceil, floor +from math import floor from dateutil.parser import parse - -fullformat = lambda dt: dt.strftime('%Y-%m-%d %H:%M:%S') -hourformat = lambda dt: dt.strftime('%H:%M') +from bvggrabber.utils.format import hourformat, fullformat def compute_remaining(start, end): @@ -90,7 +88,7 @@ class Departure(object): would require some kind of geo location in order to define a *total order*. """ - return (remaining < other.remaining) + return (self.remaining < other.remaining) def __str__(self): return "Start: %s, End: %s, when: %s, now: %s, line: %s" % ( diff --git a/bvggrabber/utils/format.py b/bvggrabber/utils/format.py index 98bbaee..603124b 100644 --- a/bvggrabber/utils/format.py +++ b/bvggrabber/utils/format.py @@ -1,7 +1,40 @@ #-*- coding: utf-8 -*- -def int2bin(i, length): +def fullformat(dt): + """Formats a datetime object as YYYY-MM-DD HH:MM:SS + + :param datetime dt: The datetime.datetime object to format + :return: A formattet string + :rtype: str + + """ + return dt.strftime('%Y-%m-%d %H:%M:%S') + + +def hourformat(dt): + """Formats a datetime object as HH:MM + + :param datetime dt: The datetime.datetime object to format + :return: A formattet string + :rtype: str + + """ + return dt.strftime('%H:%M') + + +def int2bin(i, length=8): + """Returns the bit representation of the given integer with a minimum + length of ``length``. E.g. ``int2bin(109, 7) == '1101101'`` and + ``int2bin(109, 8) == '01101101'. + + :param int i: The integer to format + :param int length: The minimum length of the output string. The string is + zero-padded on the left. + :return: The bit representation of the given int + :rtype: string + + """ if not isinstance(length, int): raise ValueError("expected int for length") return ('{:0>' + str(length) + 'b}').format(i) |