From 874b91d5878b929b53a227a3a409d8c507eb093c Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 30 Mar 2015 06:56:27 +0200 Subject: [PATCH] Added convenience get() wrappers. --- didex/values.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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) -- 2.11.0