From f52e2871ac83ae83a01c58f945eb47af7f549637 Mon Sep 17 00:00:00 2001 From: pictuga Date: Sun, 3 Nov 2013 20:18:43 +0100 Subject: [PATCH] Allow get() on new values in Cache() Previously, it was only possible to access already-cached values via Cache.get(), this restriction is now gone. Also, data is now kept raw (not base64). It's also possible from now on to pass the type of var wanted (such as int). --- morss.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/morss.py b/morss.py index 833e261..3a75e38 100644 --- a/morss.py +++ b/morss.py @@ -130,30 +130,33 @@ class Cache: for line in data: if "\t" in line: key, bdata = line.split("\t", 1) - self._cached[key] = bdata + self._cached[key] = b64decode(bdata) def __del__(self): self.save() def __contains__(self, key): - return key in self._cached + return key in self._cache or key in self._cached - def get(self, key): - if key in self._cached: + def get(self, key, kind=str): + if key in self._cache: + return kind(self._cache[key]) + elif key in self._cached: self._cache[key] = self._cached[key] - return b64decode(self._cached[key]) + return kind(self._cached[key]) else: return None def set(self, key, content): - self._cache[key] = b64encode(str(content) or '') + self._cache[key] = str(content) or '' def save(self): if len(self._cache) == 0: return out = [] - for (key, bdata) in self._cache.iteritems(): + for (key, data) in self._cache.iteritems(): + bdata = b64encode(data) out.append(str(key) + "\t" + bdata) txt = "\n".join(out)