Created
November 3, 2009 21:03
-
-
Save uggedal/225423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
## Helper functions: | |
## | |
def force_unicode(s, encoding='utf-8', errors='strict'): | |
if not isinstance(s, unicode): | |
s = unicode(s, encoding, errors) | |
return s | |
def force_bytestring(s, encoding='utf-8', errors='strict', string_only=False): | |
if not string_only and not isinstance(s, basestring): | |
try: | |
return str(s) | |
except UnicodeEncodeError: | |
return unicode(s).encode(encoding, errors) | |
elif isinstance(s, unicode): | |
return s.encode(encoding, errors) | |
else: | |
return s | |
## | |
## Wrappers that handle ecoding to utf8 on write and encoding to unicode on read | |
## | |
from pytyrant import Tyrant | |
class UnicodeTyrant(Tyrant): | |
def put(self, key, value): | |
key = force_bytestring(key) | |
value = force_bytestring(value) | |
super(UnicodeTyrant, self).put(key, value) | |
def putkeep(self, key, value): | |
key = force_bytestring(key) | |
value = force_bytestring(value) | |
super(UnicodeTyrant, self).putkeep(key, value) | |
def out(self, key): | |
key = force_bytestring(key) | |
super(UnicodeTyrant, self).out(key) | |
def get(self, key): | |
key = force_bytestring(key) | |
return force_unicode(super(UnicodeTyrant, self).get(key)) | |
def ext(self, func, opts, key, value): | |
key = force_bytestring(key) | |
value = force_bytestring(value) | |
return force_unicode( | |
super(UnicodeTyrant, self).ext(func, opts, key, value) | |
) | |
def mget(self, klst): | |
klst = map(force_bytestring, klst) | |
return [(force_unicode(k), force_unicode(v)) | |
for k, v in self._mget(klst)] | |
def fwmkeys(self, prefix, maxkeys=4294967295): | |
prefix = force_bytestring(prefix) | |
return map(force_unicode, | |
super(UnicodeTyrant, self).fwmkeys(prefix, maxkeys)) | |
def mout(self, klst): | |
klst = map(force_bytestring, klst) | |
return not len(self.misc("outlist", 0, klst)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment