summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-06-06 14:14:08 +1200
committerLars Wirzenius <liw@liw.fi>2010-06-06 14:14:08 +1200
commitd1e7b83f5b29c05124a4fc630247fdc97e0957f7 (patch)
treeffc982e190482856e5b63e54a055ac659bd08248
parent22b38e86eca00f96b1396240639ce85f27c92f76 (diff)
downloadpython-ttystatus-d1e7b83f5b29c05124a4fc630247fdc97e0957f7.tar.gz
Implement TerminalStatus.__getitem__
-rw-r--r--ttystatus/status.py16
-rw-r--r--ttystatus/status_tests.py3
2 files changed, 18 insertions, 1 deletions
diff --git a/ttystatus/status.py b/ttystatus/status.py
index b51c5c5..6eeb55e 100644
--- a/ttystatus/status.py
+++ b/ttystatus/status.py
@@ -16,7 +16,16 @@
class TerminalStatus(object):
- '''Show status and progress information on a terminal.'''
+ '''Show status and progress information on a terminal.
+
+ All output is provided via widgets of various kinds. Many widgets
+ format data that TerminalStatus stores. TerminalStatus provides a
+ dict interface for setting and retrieving data items. Unlike a real
+ dict, getting a value for a key that has not been set does not
+ result in a KeyError exception, but in the empty string being
+ returned.
+
+ '''
def __init__(self):
self.clear()
@@ -28,3 +37,8 @@ class TerminalStatus(object):
def clear(self):
'''Remove all widgets.'''
self._widgets = []
+ self._values = dict()
+
+ def __getitem__(self, key):
+ '''Return value for key, or the empty string.'''
+ return self._values.get(key, '')
diff --git a/ttystatus/status_tests.py b/ttystatus/status_tests.py
index 15a95c1..35a1463 100644
--- a/ttystatus/status_tests.py
+++ b/ttystatus/status_tests.py
@@ -36,3 +36,6 @@ class TerminalStatusTests(unittest.TestCase):
self.ts.add(ttystatus.Literal('foo'))
self.ts.clear()
self.assertEqual(self.ts._widgets, [])
+
+ def test_returns_empty_string_for_unknown_value(self):
+ self.assertEqual(self.ts['foo'], '')