diff configmix/config.py @ 439:bd27da55483a

Optimized __contains__() implementation for jailed and unjailed configurations
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 10 Dec 2021 02:15:00 +0100
parents 37424d1f8dcf
children f297c23f78f0
line wrap: on
line diff
--- a/configmix/config.py	Fri Dec 10 01:44:12 2021 +0100
+++ b/configmix/config.py	Fri Dec 10 02:15:00 2021 +0100
@@ -247,6 +247,18 @@
         else:
             return self.getvarl_s(key)
 
+    def __contains__(self, key):
+        if isinstance(key, (tuple, list)):
+            # No namespace and quoting support here
+            try:
+                self._lookupvar(*key)
+            except KeyError:
+                return False
+            else:
+                return True
+        else:
+            return super(Configuration, self).__contains__(key)
+
     def getvarl(self, *path, **kwds):
         """Get a variable where the hierarchy is given in `path` as sequence
         and the namespace is given in the `namespace` keyword argument.
@@ -827,6 +839,12 @@
         else:
             return self.getvarl_s(key)
 
+    def __contains__(self, key):
+        if isinstance(key, (tuple, list)):
+            return (self._path + key) in self._base
+        else:
+            return (self._path + (key, )) in self._base
+
     def getvarl(self, *path, **kwds):
         return self._base.getvarl(*(self._path + path), **kwds)