[Python-ideas] time.wallclock() or other similar stuff

Jacob Holm jh at improva.dk
Tue Nov 2 20:42:57 CET 2010


On 2010-11-02 16:17, James Noble wrote:
> 
> On Nov 2, 2010, at 11:02 AM, spir wrote:
> 
>> Imo, "timer" is exactly the proper term -- the issue due to proximity with "time" is real, but far less important.
> 
> How about "stopwatch"

With that name, I would expect a "proper" stopwatch object with "get",
"start", "stop" and "reset" methods.  Not just an alias for time.time or
time.clock.

I wouldn't mind seeing such an object in the stdlib, but it is easy
enough to define in terms of the function that is actually proposed.
For example:


from time import timer # the new function

class stopwatch(object):

    def __init__(self, t=0.0):
        self._value = t
        self._started = None

    def get(self):
        s = self._started
        if s is not None:
            return self._value + timer() - s
        return self._value

    def reset(self, t=0.0):
        self._value = t
        if self._started is not None:
            self._started = timer()

    def is_running(self):
        return self._started is not None

    def start(self):
        if self._started is None:
            self._started = timer()

    def stop(self):
        s = self._started
        if s is not None:
            self._value += timer() - s
            self._started = None

    value = property(get, reset, reset)

    running = property(is_running, None, stop)

    @running.setter
    def running(self, value):
        if value:
            if self._started is None:
                self._started = timer()
        else:
            s = self._started
            if s is not None:
                self._value += timer() - s
                self._started = None


Oh, and +1 for the proposed function - I don't really care what it's called.

- Jacob



More information about the Python-ideas mailing list