Repository: janehmueller/python-json-config

Issue:
Check if key exists in config
It would be good to have the possibility to check if a certain configuration parameter is defined within the configuration or not. So basically something like this should be possible:

```python
if 'backend.media.type' in config:
    return True
else:
    return False
```

Comments:

Patch:
diff --git a/python_json_config/config_node.py b/python_json_config/config_node.py
index 0b23352..972900a 100644
--- a/python_json_config/config_node.py
+++ b/python_json_config/config_node.py
@@ -15,10 +15,17 @@ class ConfigNode(object):
 
         self.__node_dict = node_dict
 
-    def __getattr__(self, item):
+    def __getattr__(self, item: str):
         return self.get(item)
 
     def get(self, path: Union[str, List[str]]):
+        """
+        Retrieve a value in the config. If the referenced field does not exist an KeyError is thrown.
+        :param path: The key of the field. Can be either a string with '.' as delimiter of the nesting levels or a list
+                     of keys with each element being one nesting level.
+                     E.g., the string 'key1.key2' and list ['key1', 'key2'] reference the same config element.
+        :return: The value of the referenced field.
+        """
         path = normalize_path(path)
         key = path[0]
         try:
@@ -29,9 +36,17 @@ class ConfigNode(object):
                 return value.get(path[1:])
         except KeyError as exception:
             print_path = '.'.join(self.__path) + ('.' if len(self.__path) > 0 else '')
-            raise KeyError(f'No value exists for key "{print_path}{key}"') from exception
+            raise AttributeError(f'No value exists for key "{print_path}{key}"') from exception
 
-    def update(self, path: Union[str, List[str]], value):
+    def update(self, path: Union[str, List[str]], value) -> None:
+        """
+        Update field in the config.
+        :param path: The name of the field. Can be either a string with '.' as delimiter of the nesting levels or a list
+                     of keys with each element being one nesting level.
+                     E.g., the string 'key1.key2' and list ['key1', 'key2'] reference the same config element.
+        :param value: The value that should replace the old value. If this value is a dictionary it is transformed into
+                      a ConfigNode.
+        """
         path = normalize_path(path)
         key = path[0]
         if len(path) == 1:
@@ -42,6 +57,21 @@ class ConfigNode(object):
         else:
             self.get(key).update(path[1:], value)
 
+    def __contains__(self, item: Union[str, List[str]]) -> bool:
+        """
+        Test if a field exists in the config and is not None (result in the case of a non-existing optional field).
+        If the field does not exist, an AttributeError is thrown, and therefore False is returned.
+        :param item: The field whose existence is tested. Can be either a string with '.' as delimiter of the nesting
+                     levels or a list of keys with each element being one nesting level.
+                     E.g., the string 'key1.key2' and list ['key1', 'key2'] reference the same config element.
+        :return: True if the field exists in the Config and False otherwise.
+        """
+        try:
+            result = self.get(item)
+            return result is not None
+        except AttributeError:
+            return False
+
     def __str__(self):
         return f'ConfigNode(path={self.__path}, values={self.__node_dict})'
 


Test patch:
diff --git a/tests/config_node_test.py b/tests/config_node_test.py
index 86521fc..0dc62b6 100644
--- a/tests/config_node_test.py
+++ b/tests/config_node_test.py
@@ -33,10 +33,10 @@ class ConfigNodeTest(TestCase):
     def test_get(self):
         node = ConfigNode(self.config_dict)
         self.assertEqual(node.key1, 1)
-        with self.assertRaises(KeyError): node.nokey
+        with self.assertRaises(AttributeError): node.nokey
 
         self.assertEqual(node.get('key1'), 1)
-        with self.assertRaises(KeyError): node.get('nokey')
+        with self.assertRaises(AttributeError): node.get('nokey')
 
         self.assertEqual(node.get('key2.key3'), 3)
 
@@ -50,7 +50,7 @@ class ConfigNodeTest(TestCase):
         self.assertIsInstance(node.key1, ConfigNode)
         self.assertEqual(node.key1.newkey, 1)
 
-        with self.assertRaises(KeyError): node.key3
+        with self.assertRaises(AttributeError): node.key3
         node.update('key3', 'asd')
         self.assertEqual(node.key3, 'asd')
 
@@ -69,3 +69,11 @@ class ConfigNodeTest(TestCase):
         self.assertEqual(pickle_conf.key1, 1)
         self.assertEqual(pickle_conf.key2.key3, 3)
         self.assertEqual(pickle_conf.key2.key4.key5, 5)
+
+    def test_contains(self):
+        config = ConfigNode(self.config_dict)
+        self.assertTrue('key1' in config)
+        self.assertFalse('key1.key2' in config)
+        self.assertTrue('key2.key3' in config)
+        self.assertTrue('key2.key4.key5' in config)
+        self.assertFalse('key2.key4.key6' in config)