bt2: add `StringValue.__contains__()` method
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Fri, 16 Aug 2019 15:43:00 +0000 (11:43 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 16 Aug 2019 20:05:01 +0000 (16:05 -0400)
This is useful to use the `in` Python operator like we do with regular
Python `str` objects:

  if 'my_env_var' in env_string_value:
    ...

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: I8f7f5c02d71bc089e14c3b8f2ffa81045a9e8f15
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1954
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/bindings/python/bt2/bt2/value.py
tests/bindings/python/bt2/test_value.py

index 8c8ef60c3de1a74bca8af47eb72ba203f5428c21..3acabe7998ca5eddd6fb788b20c93216c39ff9a1 100644 (file)
@@ -404,6 +404,9 @@ class StringValue(collections.abc.Sequence, _Value):
     def __len__(self):
         return len(self._value)
 
+    def __contains__(self, item):
+        return self._value_to_str(item) in self._value
+
     def __iadd__(self, value):
         curvalue = self._value
         curvalue += self._value_to_str(value)
index d5051f90d25c48fb762f977755843762fe2725e3..c73e397d9cd1433d20d050a607f48753c34d0cfc 100644 (file)
@@ -1535,6 +1535,15 @@ class StringValueTestCase(_TestCopySimple, unittest.TestCase):
         s1 = bt2.StringValue('allo')
         self.assertGreaterEqual('bateau', s1)
 
+    def test_in_string(self):
+        s1 = bt2.StringValue('beau grand bateau')
+        self.assertIn('bateau', s1)
+
+    def test_in_vstring(self):
+        s1 = bt2.StringValue('beau grand bateau')
+        s2 = bt2.StringValue('bateau')
+        self.assertIn(s2, s1)
+
     def test_bool_op(self):
         self.assertEqual(bool(self._def), bool(self._def_value))
 
This page took 0.026799 seconds and 4 git commands to generate.