From 3a7551e72c9d109a1e6d7e16b310e11b3f6cf9a0 Mon Sep 17 00:00:00 2001 From: syota-itakura Date: Wed, 2 Sep 2015 19:31:08 +0900 Subject: [PATCH] =?UTF-8?q?=E5=B9=B3=E5=9D=87=E5=87=A6=E7=90=86=E6=99=82?= =?UTF-8?q?=E9=96=93=E3=82=92=E8=A8=98=E9=8C=B2=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- progress_bar.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/progress_bar.py b/progress_bar.py index e1f5f81..31ae397 100644 --- a/progress_bar.py +++ b/progress_bar.py @@ -15,6 +15,9 @@ def __init__(self, max_num, unit_num=1): self.current_num = 0 self.started_at = None self.finished_at = None + self.updated_at = None + self.last_lap = 0 + self.average_lap = 0 @classmethod def iteration(cls, iterable, call_back, unit_num=1): @@ -44,7 +47,15 @@ def forward(self): def back(self): self.update(self.current_num - self.unit_num) + def _lap(self): + now = datetime.now() + current_lap = (self.updated_at - now).total_seconds() + self.average_lap = (self.last_lap + current_lap) / 2 + self.last_lap = current_lap + self.updated_at = now + def update(self, num): + self._lap() self.current_num = max(0, min(self.max_num, num)) def start(self):