Source code for pythonwrench.time
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
from typing import Callable, Optional
[docs]
class Ticker:
def __init__(
self,
*,
get_time_fn: Callable[[], float] = time.perf_counter,
prev_tick: Optional[float] = None,
) -> None:
"""Utility class to show time elapsed since last tick."""
if prev_tick is None:
prev_tick = get_time_fn()
super().__init__()
self._get_time_fn = get_time_fn
self._prev_tick = prev_tick
[docs]
def tick(self) -> float:
"""Set tick time and returns duration since last tick."""
now = self._get_time_fn()
duration = now - self._prev_tick
self._prev_tick = now
return duration
[docs]
def set_prev_tick(self, prev_tick: Optional[float] = None) -> None:
"""Set tick time."""
if prev_tick is None:
prev_tick = self._get_time_fn()
self._prev_tick = prev_tick