diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-12-04 17:44:28 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-12-04 18:26:36 +0200 |
commit | 58e7a2839e1ef37e71e7e96bbd9d826129dd08df (patch) | |
tree | fb43b8772b481ea79a4d0d3d62a1dd788e9a8a1e | |
parent | 9fc37d945d81ae06d245b23132fc3081d5e81247 (diff) | |
download | ttystatus-58e7a2839e1ef37e71e7e96bbd9d826129dd08df.tar.gz |
Add: Speed() widget
-rw-r--r-- | ttystatus/__init__.py | 1 | ||||
-rw-r--r-- | ttystatus/speed.py | 61 | ||||
-rw-r--r-- | ttystatus/speed_tests.py | 31 |
3 files changed, 93 insertions, 0 deletions
diff --git a/ttystatus/__init__.py b/ttystatus/__init__.py index 4c5317e..1ed6260 100644 --- a/ttystatus/__init__.py +++ b/ttystatus/__init__.py @@ -30,6 +30,7 @@ from .percent import PercentDone from .progressbar import ProgressBar from .remtime import RemainingTime from .elapsed import ElapsedTime +from .speed import Speed from .bytespeed import ByteSpeed from .fmt import parse diff --git a/ttystatus/speed.py b/ttystatus/speed.py new file mode 100644 index 0000000..2a1221a --- /dev/null +++ b/ttystatus/speed.py @@ -0,0 +1,61 @@ +# Copyright 2010, 2012 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +import time + +import ttystatus + + +class Speed(ttystatus.Widget): + + '''Display speed of changes.''' + + static_width = False + + def __init__(self, name, duration=None): + self.name = name + self._duration = None if duration is None else float(duration) + self._data_points = [] + self._changes = 0 + + def now(self): # pragma: no cover + '''Wrapper around time.time for unit tests to overrride.''' + return time.time() + + def render(self, width): + if len(self._data_points) < 2: + speed = 0.0 + else: # pragma: no cover + oldest, started = self._data_points[0] + latest, _ = self._data_points[-1] + delta = latest - oldest + now = self.now() + duration = now - started + speed = float(delta) / float(duration) + + return '%.2f/s' % float(speed) + + def update(self, master): # pragma: no cover + self._changes += 1 + now = self.now() + self._data_points.append((self._changes, now)) + if self._duration is None: + if len(self._data_points) > 2: + del self._data_points[1:-1] + else: + cutoff = now - self._duration + while self._data_points[0][1] < cutoff: + del self._data_points[0] diff --git a/ttystatus/speed_tests.py b/ttystatus/speed_tests.py new file mode 100644 index 0000000..12ae2ee --- /dev/null +++ b/ttystatus/speed_tests.py @@ -0,0 +1,31 @@ +# Copyright 2010 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +import unittest + +import ttystatus + + +class SpeedTests(unittest.TestCase): + + def setUp(self): + self.w = ttystatus.Speed('foo') + + def test_is_not_static_width(self): + self.assertFalse(self.w.static_width) + + def test_formats_zero_speed_without_update(self): + self.assertEqual(self.w.render(0), '0.00/s') |