uggedal (owner)

Revisions

gist: 225423 Download_button fork
public
Public Clone URL: git://gist.github.com/225423.git
Embed All Files: show embed
unicode_tyrant.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
##
## 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))