diff options
author | Lars Wirzenius <liw@liw.fi> | 2010-06-12 20:59:40 +1200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2010-06-12 20:59:40 +1200 |
commit | d2cad5f1d4a5193a47fddebd8e7ea611c1c5542b (patch) | |
tree | ac84e42b8dd33c0027d65810b16282b7f53d6f46 | |
parent | 2c70ee267a0172de421cdbc068515e785a3963a5 (diff) | |
download | python-ttystatus-d2cad5f1d4a5193a47fddebd8e7ea611c1c5542b.tar.gz |
Add TerminalStatus.get method.
-rw-r--r-- | ttystatus/status.py | 4 | ||||
-rw-r--r-- | ttystatus/status_tests.py | 10 |
2 files changed, 14 insertions, 0 deletions
diff --git a/ttystatus/status.py b/ttystatus/status.py index 67b4a46..f810525 100644 --- a/ttystatus/status.py +++ b/ttystatus/status.py @@ -48,6 +48,10 @@ class TerminalStatus(object): '''Return value for key, or the empty string.''' return self._values.get(key, '') + def get(self, key, default=None): + '''Like dict.get.''' + return self._values.get(key, default) + def __setitem__(self, key, value): '''Set value for key.''' self._values[key] = value diff --git a/ttystatus/status_tests.py b/ttystatus/status_tests.py index aaef1c2..36bc4bd 100644 --- a/ttystatus/status_tests.py +++ b/ttystatus/status_tests.py @@ -61,6 +61,16 @@ class TerminalStatusTests(unittest.TestCase): self.ts['foo'] = 'bar' self.assertEqual(self.ts['foo'], 'bar') + def test_gets_value(self): + self.ts['foo'] = 'bar' + self.assertEqual(self.ts.get('foo'), 'bar') + + def test_gets_default_value_for_nonexistent_key(self): + self.assertEqual(self.ts.get('foo', 'bar'), 'bar') + + def test_gets_None_for_nonexistent_key_without_default_value(self): + self.assertEqual(self.ts.get('foo'), None) + def test_updates_widgets_when_value_is_set(self): w = ttystatus.String('foo') self.ts.add(w) |