def pop(self, name): """Get and remove key from database (atomic).""" name = mkey(name) temp = mkey((name, "__poptmp__")) self.rename(name, temp) value = self[temp] del(self[temp]) return value
def pop(self, name): """Get and remove key from database (atomic).""" name = mkey(name) temp = mkey((name, "__poptmp__")) self.rename(name, temp) value = self[temp] del (self[temp]) return value
def rename(self, old_name, new_name): """Rename key to a new name.""" try: self.api.rename(mkey(old_name), mkey(new_name)) except ResponseError, exc: if "no such key" in exc.args: raise KeyError(old_name) raise
def __getitem__(self, name): """``x.__getitem__(name) <==> x[name]``""" name = mkey(name) value = self.api.get(name) if value is None: raise KeyError(name) return self.value_to_python(value)
def __contains__(self, name): """``x.__contains__(name) <==> name in x``""" return self.api.exists(mkey(name))
def __delitem__(self, name): """``x.__delitem__(name) <==> del(x[name])``""" name = mkey(name) if not self.api.delete(name): raise KeyError(name)
def __setitem__(self, name, value): """``x.__setitem(name, value) <==> x[name] = value``""" return self.api.set(mkey(name), self.prepare_value(value))
def test_mkey(self): self.assertEqual(utils.mkey("foo"), "foo") self.assertEqual(utils.mkey("foo:bar"), "foo:bar") self.assertEqual(utils.mkey(("foo", "bar", "baz")), "foo:bar:baz") self.assertEqual(utils.mkey(["foo", "bar", "baz"]), "foo:bar:baz")
def __init__(self, name, client): self.name = mkey(name) self.client = client