From 9cb92e835f1b37a5e5a30c0fdbc76ab3bb220f89 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 15 Apr 2012 15:46:33 +0100 Subject: Change __str__ to render We'll want to pass in arguments to render (i.e., width), so using __str__ is not going to be appropriate. --- ttystatus/bytesize.py | 2 +- ttystatus/bytesize_tests.py | 16 ++++++++-------- ttystatus/bytespeed.py | 2 +- ttystatus/bytespeed_tests.py | 10 +++++----- ttystatus/counter.py | 2 +- ttystatus/counter_tests.py | 8 ++++---- ttystatus/elapsed.py | 2 +- ttystatus/elapsed_tests.py | 6 +++--- ttystatus/fmt_tests.py | 8 ++++---- ttystatus/index.py | 2 +- ttystatus/index_tests.py | 6 +++--- ttystatus/integer.py | 2 +- ttystatus/integer_tests.py | 6 +++--- ttystatus/literal.py | 2 +- ttystatus/literal_tests.py | 2 +- ttystatus/pathname.py | 2 +- ttystatus/pathname_tests.py | 6 +++--- ttystatus/percent.py | 2 +- ttystatus/percent_tests.py | 8 ++++---- ttystatus/progressbar.py | 2 +- ttystatus/progressbar_tests.py | 18 +++++++++--------- ttystatus/remtime.py | 2 +- ttystatus/remtime_tests.py | 14 +++++++------- ttystatus/status.py | 2 +- ttystatus/status_tests.py | 6 +++--- ttystatus/string.py | 2 +- ttystatus/string_tests.py | 6 +++--- ttystatus/widget.py | 7 +++++-- 28 files changed, 78 insertions(+), 75 deletions(-) diff --git a/ttystatus/bytesize.py b/ttystatus/bytesize.py index 4c338aa..b5a8059 100644 --- a/ttystatus/bytesize.py +++ b/ttystatus/bytesize.py @@ -28,7 +28,7 @@ class ByteSize(ttystatus.Widget): def update(self, ts): self._bytes = ts[self.name] - def __str__(self): + def render(self): units = ( (1024**4, 2, 'TiB'), (1024**3, 2, 'GiB'), diff --git a/ttystatus/bytesize_tests.py b/ttystatus/bytesize_tests.py index d1dd94d..e6ce761 100644 --- a/ttystatus/bytesize_tests.py +++ b/ttystatus/bytesize_tests.py @@ -25,33 +25,33 @@ class ByteSizeTests(unittest.TestCase): self.w = ttystatus.ByteSize('foo') def test_formats_zero_bytes_without_update(self): - self.assertEqual(str(self.w), '0 B') + self.assertEqual(self.w.render(), '0 B') def test_formats_zero_bytes_correctly(self): self.w.update({ 'foo': 0 }) - self.assertEqual(str(self.w), '0 B') + self.assertEqual(self.w.render(), '0 B') def test_formats_one_bytes_correctly(self): self.w.update({ 'foo': 1 }) - self.assertEqual(str(self.w), '1 B') + self.assertEqual(self.w.render(), '1 B') def test_formats_1023_bytes_correctly(self): self.w.update({ 'foo': 1023 }) - self.assertEqual(str(self.w), '1023 B') + self.assertEqual(self.w.render(), '1023 B') def test_formats_1024_bytes_correctly(self): self.w.update({ 'foo': 1024 }) - self.assertEqual(str(self.w), '1.0 KiB') + self.assertEqual(self.w.render(), '1.0 KiB') def test_formats_1_MiB_bytes_correctly(self): self.w.update({ 'foo': 1024**2 }) - self.assertEqual(str(self.w), '1.00 MiB') + self.assertEqual(self.w.render(), '1.00 MiB') def test_formats_1_GiB_bytes_correctly(self): self.w.update({ 'foo': 1024**3 }) - self.assertEqual(str(self.w), '1.00 GiB') + self.assertEqual(self.w.render(), '1.00 GiB') def test_formats_1_TiB_bytes_correctly(self): self.w.update({ 'foo': 1024**4 }) - self.assertEqual(str(self.w), '1.00 TiB') + self.assertEqual(self.w.render(), '1.00 TiB') diff --git a/ttystatus/bytespeed.py b/ttystatus/bytespeed.py index cc62300..d7e8b78 100644 --- a/ttystatus/bytespeed.py +++ b/ttystatus/bytespeed.py @@ -33,7 +33,7 @@ class ByteSpeed(ttystatus.Widget): return time.time() - def __str__(self): + def render(self): units = ( (1024**4, 2, 'TiB/s'), (1024**3, 2, 'GiB/s'), diff --git a/ttystatus/bytespeed_tests.py b/ttystatus/bytespeed_tests.py index 369c2f6..b56afc3 100644 --- a/ttystatus/bytespeed_tests.py +++ b/ttystatus/bytespeed_tests.py @@ -25,30 +25,30 @@ class ByteSpeedTests(unittest.TestCase): self.w = ttystatus.ByteSpeed('foo') def test_formats_zero_speed_without_update(self): - self.assertEqual(str(self.w), '0 B/s') + self.assertEqual(self.w.render(), '0 B/s') def test_formats_zero_bytes_correctly(self): self.w.update({ 'foo': 0 }) - self.assertEqual(str(self.w), '0 B/s') + self.assertEqual(self.w.render(), '0 B/s') def test_formats_one_byte_per_second_correctly(self): self.w.now = lambda: 1 self.w.update({ 'foo': 0 }) self.w.now = lambda: 2 self.w.update({ 'foo': 1 }) - self.assertEqual(str(self.w), '1 B/s') + self.assertEqual(self.w.render(), '1 B/s') def test_formats_ten_bytes_per_second_correctly(self): self.w.now = lambda: 1 self.w.update({ 'foo': 0 }) self.w.now = lambda: 11 self.w.update({ 'foo': 100 }) - self.assertEqual(str(self.w), '10 B/s') + self.assertEqual(self.w.render(), '10 B/s') def test_formats_ten_tibs_per_second_correctly(self): self.w.now = lambda: 1 self.w.update({ 'foo': 0 }) self.w.now = lambda: 2 self.w.update({ 'foo': 10 * 1024**4 }) - self.assertEqual(str(self.w), '10.00 TiB/s') + self.assertEqual(self.w.render(), '10.00 TiB/s') diff --git a/ttystatus/counter.py b/ttystatus/counter.py index 5b92791..ae61fa0 100644 --- a/ttystatus/counter.py +++ b/ttystatus/counter.py @@ -26,7 +26,7 @@ class Counter(ttystatus.Widget): self.prev = None self.count = 0 - def __str__(self): + def render(self): return str(self.count) def update(self, master): diff --git a/ttystatus/counter_tests.py b/ttystatus/counter_tests.py index f7dc3e1..638de6a 100644 --- a/ttystatus/counter_tests.py +++ b/ttystatus/counter_tests.py @@ -25,19 +25,19 @@ class CounterTests(unittest.TestCase): self.w = ttystatus.Counter('foo') def test_counts_zero_initially(self): - self.assertEqual(str(self.w), '0') + self.assertEqual(self.w.render(), '0') def test_counts_one_change(self): self.w.update({ 'foo': 'a' }) - self.assertEqual(str(self.w), '1') + self.assertEqual(self.w.render(), '1') def test_counts_two_changes(self): self.w.update({ 'foo': 'a' }) self.w.update({ 'foo': 'b' }) - self.assertEqual(str(self.w), '2') + self.assertEqual(self.w.render(), '2') def test_does_not_count_if_value_does_not_change(self): self.w.update({ 'foo': 'a' }) self.w.update({ 'foo': 'a' }) - self.assertEqual(str(self.w), '1') + self.assertEqual(self.w.render(), '1') diff --git a/ttystatus/elapsed.py b/ttystatus/elapsed.py index 242c092..3a85303 100644 --- a/ttystatus/elapsed.py +++ b/ttystatus/elapsed.py @@ -31,7 +31,7 @@ class ElapsedTime(ttystatus.Widget): '''Wrapper around time.time() for unit tests to override.''' return time.time() - def __str__(self): + def render(self): secs = self.secs hours = secs / 3600 secs %= 3600 diff --git a/ttystatus/elapsed_tests.py b/ttystatus/elapsed_tests.py index 9c82acb..3197a77 100644 --- a/ttystatus/elapsed_tests.py +++ b/ttystatus/elapsed_tests.py @@ -25,17 +25,17 @@ class ElapsedtimeTests(unittest.TestCase): self.w = ttystatus.ElapsedTime() def test_shows_zero_initially(self): - self.assertEqual(str(self.w), '00h00m00s') + self.assertEqual(self.w.render(), '00h00m00s') def test_shows_zero_after_first_update(self): self.w.get_time = lambda: 1 self.w.update({}) - self.assertEqual(str(self.w), '00h00m00s') + self.assertEqual(self.w.render(), '00h00m00s') def test_shows_one_one_one_after_second_update(self): self.w.get_time = lambda: 0 self.w.update({}) self.w.get_time = lambda: 60*60 + 60 + 1 self.w.update({}) - self.assertEqual(str(self.w), '01h01m01s') + self.assertEqual(self.w.render(), '01h01m01s') diff --git a/ttystatus/fmt_tests.py b/ttystatus/fmt_tests.py index af39948..422a7fe 100644 --- a/ttystatus/fmt_tests.py +++ b/ttystatus/fmt_tests.py @@ -32,13 +32,13 @@ class FormatTests(unittest.TestCase): x = ttystatus.fmt.parse('hello, world') self.assertEqual(len(x), 1) self.assertEqual(type(x[0]), ttystatus.Literal) - self.assertEqual(str(x[0]), 'hello, world') + self.assertEqual(x[0].render(), 'hello, world') def test_parses_escaped_pecent(self): x = ttystatus.fmt.parse('%%') self.assertEqual(len(x), 1) self.assertEqual(type(x[0]), ttystatus.Literal) - self.assertEqual(str(x[0]), '%') + self.assertEqual(x[0].render(), '%') def test_parses_parameterless_widget(self): x = ttystatus.fmt.parse('%ElapsedTime()') @@ -60,12 +60,12 @@ class FormatTests(unittest.TestCase): self.assertEqual(len(x), 4) self.assertEqual(type(x[0]), ttystatus.Literal) - self.assertEqual(str(x[0]), 'hello, ') + self.assertEqual(x[0].render(), 'hello, ') self.assertEqual(type(x[1]), ttystatus.String) self.assertEqual(type(x[2]), ttystatus.Literal) - self.assertEqual(str(x[2]), ': ') + self.assertEqual(x[2].render(), ': ') self.assertEqual(type(x[3]), ttystatus.ElapsedTime) diff --git a/ttystatus/index.py b/ttystatus/index.py index daa13df..3e73502 100644 --- a/ttystatus/index.py +++ b/ttystatus/index.py @@ -27,7 +27,7 @@ class Index(ttystatus.Widget): self.value = None self.listvalue = [] - def __str__(self): + def render(self): try: index = self.listvalue.index(self.value) + 1 except ValueError: diff --git a/ttystatus/index_tests.py b/ttystatus/index_tests.py index d3d420e..05d2db7 100644 --- a/ttystatus/index_tests.py +++ b/ttystatus/index_tests.py @@ -25,13 +25,13 @@ class IndexTests(unittest.TestCase): self.w = ttystatus.Index('foo', 'foos') def test_is_zero_initially(self): - self.assertEqual(str(self.w), '0/0') + self.assertEqual(self.w.render(), '0/0') def test_gets_index_right(self): self.w.update({ 'foo': 'x', 'foos': ['a', 'x', 'b'] }) - self.assertEqual(str(self.w), '2/3') + self.assertEqual(self.w.render(), '2/3') def test_handles_value_not_in_list(self): self.w.update({ 'foo': 'xxx', 'foos': ['a', 'x', 'b'] }) - self.assertEqual(str(self.w), '0/3') + self.assertEqual(self.w.render(), '0/3') diff --git a/ttystatus/integer.py b/ttystatus/integer.py index 1b27126..7def9ff 100644 --- a/ttystatus/integer.py +++ b/ttystatus/integer.py @@ -25,7 +25,7 @@ class Integer(ttystatus.Widget): self._key = key self.value = None - def __str__(self): + def render(self): try: return str(int(self.value)) except (TypeError, ValueError): diff --git a/ttystatus/integer_tests.py b/ttystatus/integer_tests.py index f5b3f54..9ea8054 100644 --- a/ttystatus/integer_tests.py +++ b/ttystatus/integer_tests.py @@ -25,13 +25,13 @@ class IntegerTests(unittest.TestCase): self.w = ttystatus.Integer('foo') def test_is_error_initially(self): - self.assertEqual(str(self.w), '#') + self.assertEqual(self.w.render(), '#') def test_updates(self): self.w.update({'foo': 123}) - self.assertEqual(str(self.w), '123') + self.assertEqual(self.w.render(), '123') def test_becomes_error_symbol_if_value_is_not_integer(self): self.w.update({'foo': 'bar'}) - self.assertEqual(str(self.w), '#') + self.assertEqual(self.w.render(), '#') diff --git a/ttystatus/literal.py b/ttystatus/literal.py index d0a1dc7..fa09ceb 100644 --- a/ttystatus/literal.py +++ b/ttystatus/literal.py @@ -24,5 +24,5 @@ class Literal(ttystatus.Widget): def __init__(self, string): self.value = string - def __str__(self): + def render(self): return self.value diff --git a/ttystatus/literal_tests.py b/ttystatus/literal_tests.py index 969439d..73c8964 100644 --- a/ttystatus/literal_tests.py +++ b/ttystatus/literal_tests.py @@ -23,4 +23,4 @@ class LiteralTests(unittest.TestCase): def test_sets_value_correctly(self): literal = ttystatus.Literal('foo') - self.assertEqual(str(literal), 'foo') + self.assertEqual(literal.render(), 'foo') diff --git a/ttystatus/pathname.py b/ttystatus/pathname.py index a8b8253..0ecf4a7 100644 --- a/ttystatus/pathname.py +++ b/ttystatus/pathname.py @@ -29,7 +29,7 @@ class Pathname(ttystatus.Widget): self._key = key self.pathname = '' - def __str__(self): + def render(self): return self.pathname def update(self, master): diff --git a/ttystatus/pathname_tests.py b/ttystatus/pathname_tests.py index f9e62ef..7f09de0 100644 --- a/ttystatus/pathname_tests.py +++ b/ttystatus/pathname_tests.py @@ -25,12 +25,12 @@ class PathnameTests(unittest.TestCase): self.w = ttystatus.Pathname('foo') def test_is_empty_initially(self): - self.assertEqual(str(self.w), '') + self.assertEqual(self.w.render(), '') def test_updates(self): self.w.update({'foo': 'bar'}) - self.assertEqual(str(self.w), 'bar') + self.assertEqual(self.w.render(), 'bar') def test_handles_update_to_other_value(self): self.w.update({'other': 1}) - self.assertEqual(str(self.w), '') + self.assertEqual(self.w.render(), '') diff --git a/ttystatus/percent.py b/ttystatus/percent.py index 840c047..117fecc 100644 --- a/ttystatus/percent.py +++ b/ttystatus/percent.py @@ -28,7 +28,7 @@ class PercentDone(ttystatus.Widget): self.done = 0 self.total = 1 - def __str__(self): + def render(self): try: done = float(self.done) total = float(self.total) diff --git a/ttystatus/percent_tests.py b/ttystatus/percent_tests.py index 5f46498..c7d8d64 100644 --- a/ttystatus/percent_tests.py +++ b/ttystatus/percent_tests.py @@ -25,17 +25,17 @@ class PercentDoneTests(unittest.TestCase): self.w = ttystatus.PercentDone('done', 'total', decimals=1) def test_shows_zero_value_initially(self): - self.assertEqual(str(self.w), '0.0 %') + self.assertEqual(self.w.render(), '0.0 %') def test_sets_value(self): self.w.update({ 'done': 50, 'total': 100 }) - self.assertEqual(str(self.w), '50.0 %') + self.assertEqual(self.w.render(), '50.0 %') def test_handles_empty_strings_as_values(self): self.w.update({ 'done': '', 'total': '' }) - self.assertEqual(str(self.w), '0.0 %') + self.assertEqual(self.w.render(), '0.0 %') def test_handles_zero_total(self): self.w.update({ 'done': 0, 'total': 0 }) - self.assertEqual(str(self.w), '0.0 %') + self.assertEqual(self.w.render(), '0.0 %') diff --git a/ttystatus/progressbar.py b/ttystatus/progressbar.py index d8189d3..54ca99f 100644 --- a/ttystatus/progressbar.py +++ b/ttystatus/progressbar.py @@ -28,7 +28,7 @@ class ProgressBar(ttystatus.Widget): self.total = 1 self.width = width - def __str__(self): + def render(self): try: done = float(self.done) total = float(self.total) diff --git a/ttystatus/progressbar_tests.py b/ttystatus/progressbar_tests.py index a229f46..8c7c261 100644 --- a/ttystatus/progressbar_tests.py +++ b/ttystatus/progressbar_tests.py @@ -25,37 +25,37 @@ class ProgressBarTests(unittest.TestCase): self.w = ttystatus.ProgressBar('done', 'total', 10) def test_sets_initial_value_to_empty(self): - self.assertEqual(str(self.w), '-' * 10) + self.assertEqual(self.w.render(), '-' * 10) def test_shows_zero_percent_for_empty_string_total(self): self.w.update({ 'done': 1, 'total': '' }) - self.assertEqual(str(self.w), '-' * 10) + self.assertEqual(self.w.render(), '-' * 10) def test_shows_zero_percent_for_zero_total(self): self.w.update({ 'done': 1, 'total': 0 }) - self.assertEqual(str(self.w), '-' * 10) + self.assertEqual(self.w.render(), '-' * 10) def test_shows_zero_percent_correctly(self): self.w.update({ 'done': 0, 'total': 100 }) - self.assertEqual(str(self.w), '-' * 10) + self.assertEqual(self.w.render(), '-' * 10) def test_shows_one_percent_correctly(self): self.w.update({ 'done': 1, 'total': 100 }) - self.assertEqual(str(self.w), '-' * 10) + self.assertEqual(self.w.render(), '-' * 10) def test_shows_ten_percent_correctly(self): self.w.update({ 'done': 10, 'total': 100 }) - self.assertEqual(str(self.w), '#' + '-' * 9) + self.assertEqual(self.w.render(), '#' + '-' * 9) def test_shows_ninety_percent_correctly(self): self.w.update({ 'done': 90, 'total': 100 }) - self.assertEqual(str(self.w), '#' * 9 + '-') + self.assertEqual(self.w.render(), '#' * 9 + '-') def test_shows_ninety_ine_percent_correctly(self): self.w.update({ 'done': 99, 'total': 100 }) - self.assertEqual(str(self.w), '#' * 10) + self.assertEqual(self.w.render(), '#' * 10) def test_shows_one_hundred_percent_correctly(self): self.w.update({ 'done': 100, 'total': 100 }) - self.assertEqual(str(self.w), '#' * 10) + self.assertEqual(self.w.render(), '#' * 10) diff --git a/ttystatus/remtime.py b/ttystatus/remtime.py index 9d90895..50529e6 100644 --- a/ttystatus/remtime.py +++ b/ttystatus/remtime.py @@ -41,7 +41,7 @@ class RemainingTime(ttystatus.Widget): return time.time() - def __str__(self): + def render(self): if self.started is None: self.started = self.get_time() duration = self.get_time() - self.started diff --git a/ttystatus/remtime_tests.py b/ttystatus/remtime_tests.py index dced388..df6bced 100644 --- a/ttystatus/remtime_tests.py +++ b/ttystatus/remtime_tests.py @@ -26,30 +26,30 @@ class RemainingTimeTests(unittest.TestCase): self.w.get_time = lambda: 0.0 def test_is_dashes_initially(self): - self.assertEqual(str(self.w), '--h--m--s') + self.assertEqual(self.w.render(), '--h--m--s') def test_estimates_and_formats_correctly(self): - self.assertEqual(str(self.w), '--h--m--s') + self.assertEqual(self.w.render(), '--h--m--s') self.w.update({ 'done': 0, 'total': 100 }) self.w.get_time = lambda: 5.0 self.w.update({ 'done': 5, 'total': 100 }) - self.assertEqual(str(self.w), '00h01m35s') + self.assertEqual(self.w.render(), '00h01m35s') self.w.get_time = lambda: 10.0 self.w.update({ 'done': 5, 'total': 100 }) - self.assertEqual(str(self.w), '00h03m10s') + self.assertEqual(self.w.render(), '00h03m10s') self.w.get_time = lambda: 20.0 self.w.update({ 'done': 80, 'total': 100 }) - self.assertEqual(str(self.w), '00h00m05s') + self.assertEqual(self.w.render(), '00h00m05s') def test_handles_zero_speed(self): self.w.update({ 'done': 0, 'total': 100 }) self.w.get_time = lambda: 5.0 self.w.update({ 'done': 0, 'total': 100 }) - self.assertEqual(str(self.w), '--h--m--s') + self.assertEqual(self.w.render(), '--h--m--s') def test_handles_empty_strings_for_done_and_total(self): self.w.update({ 'done': '', 'total': '' }) self.w.get_time = lambda: 5.0 self.w.update({ 'done': '', 'total': '' }) - self.assertEqual(str(self.w), '--h--m--s') + self.assertEqual(self.w.render(), '--h--m--s') diff --git a/ttystatus/status.py b/ttystatus/status.py index e45c774..526029b 100644 --- a/ttystatus/status.py +++ b/ttystatus/status.py @@ -76,7 +76,7 @@ class TerminalStatus(object): def _render(self): '''Format and output all widgets.''' - self._m.write(''.join(str(w) for w in self._widgets)) + self._m.write(''.join(w.render() for w in self._widgets)) def increase(self, key, delta): '''Increase value for a key by a given amount.''' diff --git a/ttystatus/status_tests.py b/ttystatus/status_tests.py index 03a0499..bb581f6 100644 --- a/ttystatus/status_tests.py +++ b/ttystatus/status_tests.py @@ -94,9 +94,9 @@ class TerminalStatusTests(unittest.TestCase): def test_updates_widgets_when_value_is_set(self): w = ttystatus.String('foo') self.ts.add(w) - self.assertEqual(str(w), '') + self.assertEqual(w.render(), '') self.ts['foo'] = 'bar' - self.assertEqual(str(w), 'bar') + self.assertEqual(w.render(), 'bar') def test_increases_value(self): self.ts['foo'] = 10 @@ -128,5 +128,5 @@ class TerminalStatusTests(unittest.TestCase): self.ts.add(w) for i in range(n): self.ts['value'] = i - self.assertEqual(str(w), str(n)) + self.assertEqual(w.render(), str(n)) diff --git a/ttystatus/string.py b/ttystatus/string.py index a23a8e5..90fb48b 100644 --- a/ttystatus/string.py +++ b/ttystatus/string.py @@ -25,7 +25,7 @@ class String(ttystatus.Widget): self._key = key self.value = '' - def __str__(self): + def render(self): return str(self.value) def update(self, master): diff --git a/ttystatus/string_tests.py b/ttystatus/string_tests.py index aad21f5..b377f37 100644 --- a/ttystatus/string_tests.py +++ b/ttystatus/string_tests.py @@ -25,12 +25,12 @@ class StringTests(unittest.TestCase): self.s = ttystatus.String('foo') def test_is_empty_initially(self): - self.assertEqual(str(self.s), '') + self.assertEqual(self.s.render(), '') def test_updates(self): self.s.update({'foo': 'bar'}) - self.assertEqual(str(self.s), 'bar') + self.assertEqual(self.s.render(), 'bar') def test_handles_non_string_value(self): self.s.update({'foo': 123}) - self.assertEqual(str(self.s), '123') + self.assertEqual(self.s.render(), '123') diff --git a/ttystatus/widget.py b/ttystatus/widget.py index 17c2df4..9fccc29 100644 --- a/ttystatus/widget.py +++ b/ttystatus/widget.py @@ -26,12 +26,15 @@ class Widget(object): * the widget `update` method is called by TerminalStatus whenever any of the values change - * the widget `__str__` method is called by TerminalStatus when it is + * the widget `render` method is called by TerminalStatus when it is time to display things - + ''' def __str__(self): + raise NotImplementedError() + + def render(self): '''Format the current value. This will be called only when the value actually needs to be -- cgit v1.2.1