Added convenience get() wrappers.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 30 Mar 2015 04:56:27 +0000 (06:56 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 30 Mar 2015 04:56:27 +0000 (06:56 +0200)
didex/values.py

index c2b6d1f..ecb78dd 100644 (file)
@@ -40,6 +40,27 @@ class base(storedesc):
     def get(self, **kwargs):
         return cursor(self.index().get(**kwargs), self.store)
 
     def get(self, **kwargs):
         return cursor(self.index().get(**kwargs), self.store)
 
+    def get1(self, *, check=False, default=KeyError, **kwargs):
+        with self.get(**kwargs) as cursor:
+            try:
+                k, v = next(cursor)
+            except StopIteration:
+                if default is not KeyError:
+                    return default
+                raise KeyError("no matches in " + self.name, kwargs)
+            if check:
+                try:
+                    next(cursor)
+                except StopIteration:
+                    pass
+                else:
+                    raise ValueError("unexpected multiple matchies in " + self.name, kwargs)
+            return v
+
+    def list(self, **kwargs):
+        with self.get(**kwargs) as cursor:
+            return [v for k, v in cursor]
+
 class descbase(base):
     def __init__(self, store, indextype, name, datatype, default):
         super().__init__(store, indextype, name, datatype)
 class descbase(base):
     def __init__(self, store, indextype, name, datatype, default):
         super().__init__(store, indextype, name, datatype)