Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ae2388f01e
Fetching contributors…

Cannot retrieve contributors at this time

file 58 lines (47 sloc) 1.487 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
class Event(object):
    """An event that can have an arbitrary number of listeners that get called
when the event fires."""
    def __init__(self):
        self.listeners = set()

    def register_listener(self, callable):
        self.listeners.add(callable)
        return callable

    def fire(self, *args, **kwargs):
        for listener in self.listeners:
            listener(*args, **kwargs)

    __iadd__ = register_listener
    __call__ = fire


def pretty_time_span(delta):
    seconds = int(delta.total_seconds())
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)

    if hours == 1:
        return "1 hour"
    elif hours > 1:
        return "%d hours" % hours
    elif minutes == 1:
        return "1 minute"
    elif minutes > 1:
        return "%d minutes" % minutes
    elif seconds == 1:
        return "1 second"
    else:
        return "%d seconds" % seconds


def pretty_and_accurate_time_span(delta):
    seconds = int(delta.total_seconds())
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)

    parts = []
    if hours == 1:
        parts.append("1 hour")
    elif hours > 1:
        parts.append("%d hours" % hours)

    if minutes == 1:
        parts.append("1 minute")
    elif minutes > 1:
        parts.append("%d minutes" % minutes)

    if seconds == 1:
        parts.append("1 second")
    elif seconds > 1:
        parts.append("%d seconds" % seconds)
    return ", ".join(parts)
Something went wrong with that request. Please try again.