From 2443c414af8901e89b1b55e126ab4867c2cf91e4 Mon Sep 17 00:00:00 2001 From: Markus Holtermann Date: Wed, 30 Jan 2013 20:56:55 +0100 Subject: Move hourformat and fullformat to separate module. Add and adjust tests --- bvggrabber/api/__init__.py | 8 +++----- bvggrabber/utils/format.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 6 deletions(-) (limited to 'bvggrabber') 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) -- cgit v1.2.3