From: Fredrik Tolf Date: Mon, 30 Mar 2015 04:56:27 +0000 (+0200) Subject: Added convenience get() wrappers. X-Git-Url: http://www.dolda2000.com/gitweb/?a=commitdiff_plain;ds=sidebyside;h=874b91d5878b929b53a227a3a409d8c507eb093c;p=didex.git Added convenience get() wrappers. --- diff --git a/didex/values.py b/didex/values.py index c2b6d1f..ecb78dd 100644 --- a/didex/values.py +++ b/didex/values.py @@ -40,6 +40,27 @@ class base(storedesc): 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)