summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2020-09-10 07:17:06 +0000
committerDaniel Silverstone <dsilvers+gitlab@digital-scurf.org>2020-09-10 07:17:06 +0000
commit5cf964d1a635c9c4a6aa0124ee4e23656dc9bf5e (patch)
tree00af0a77d3ccbabcce72709754d5aedcbfb50757
parent04d23939695d903d57bc72d7c3921b087d8f47d9 (diff)
parent017de1442c02403cf30f2a9bc5c6c5cc28331eee (diff)
downloadsubplot-5cf964d1a635c9c4a6aa0124ee4e23656dc9bf5e.tar.gz
Merge branch 'contains' into 'main'
feat(python template): add "foo in ctx" support Closes #71 See merge request larswirzenius/subplot!62
-rw-r--r--templates/python/context.py3
-rw-r--r--templates/python/context_tests.py9
2 files changed, 12 insertions, 0 deletions
diff --git a/templates/python/context.py b/templates/python/context.py
index 8d9894f..9bb8056 100644
--- a/templates/python/context.py
+++ b/templates/python/context.py
@@ -18,3 +18,6 @@ class Context:
def __setitem__(self, key, value):
logging.info("Context: {}={!r}".format(key, value))
self._vars[key] = value
+
+ def __contains__(self, key):
+ return key in self._vars
diff --git a/templates/python/context_tests.py b/templates/python/context_tests.py
index fc99af5..9db6bd0 100644
--- a/templates/python/context_tests.py
+++ b/templates/python/context_tests.py
@@ -13,6 +13,15 @@ class ContextTests(unittest.TestCase):
ctx["foo"] = "bar"
self.assertEqual(ctx["foo"], "bar")
+ def test_does_not_contain_item(self):
+ ctx = Context()
+ self.assertFalse("foo" in ctx)
+
+ def test_contains_item(self):
+ ctx = Context()
+ ctx["foo"] = "bar"
+ self.assertTrue("foo" in ctx)
+
def test_get_returns_default_if_item_does_not_exist(self):
ctx = Context()
self.assertEqual(ctx.get("foo"), None)