def last(self):
(key, value) = self.dict.last()
f = StringIO(value)
return (key, Unpickler(f).load())
python类Unpickler()的实例源码
def loads(self, buf):
f = self.input(buf)
try:
p = cPickle.Unpickler(f)
return p.load()
finally:
self.close(f)
def loads(self, buf):
f = self.input(buf)
try:
p = cPickle.Unpickler(f)
return p.load()
finally:
self.close(f)
def loads(self, *args):
f = self.input(args[0])
try:
p = cPickle.Unpickler(f)
return p.load()
finally:
self.close(f)
def loads(self, *args):
f = self.input(args[0])
try:
p = cPickle.Unpickler(f)
return p.load()
finally:
self.close(f)
def __getitem__(self, key):
try:
value = self.cache[key]
except KeyError:
f = StringIO(self.dict[key])
value = Unpickler(f).load()
if self.writeback:
self.cache[key] = value
return value
def next(self):
(key, value) = self.dict.next()
f = StringIO(value)
return (key, Unpickler(f).load())
def previous(self):
(key, value) = self.dict.previous()
f = StringIO(value)
return (key, Unpickler(f).load())
def first(self):
(key, value) = self.dict.first()
f = StringIO(value)
return (key, Unpickler(f).load())
def last(self):
(key, value) = self.dict.last()
f = StringIO(value)
return (key, Unpickler(f).load())
def base64unpickle(value, unsafe=False):
"""
Decodes value from Base64 to plain format and deserializes (with pickle) its content
>>> base64unpickle('gAJVBmZvb2JhcnEBLg==')
'foobar'
"""
retVal = None
def _(self):
if len(self.stack) > 1:
func = self.stack[-2]
if func not in PICKLE_REDUCE_WHITELIST:
raise Exception, "abusing reduce() is bad, Mkay!"
self.load_reduce()
def loads(str):
f = StringIO.StringIO(str)
if unsafe:
unpickler = picklePy.Unpickler(f)
unpickler.dispatch[picklePy.REDUCE] = _
else:
unpickler = pickle.Unpickler(f)
return unpickler.load()
try:
retVal = loads(base64decode(value))
except TypeError:
retVal = loads(base64decode(bytes(value)))
return retVal
def load(self, filename):
from cPickle import Unpickler
print "reservoirs.py: loading %s" % filename
f = open(filename,'rb')
u = Unpickler(f)
tmp_dict = u.load()
f.close()
self.__dict__.update(tmp_dict)
############################################################
# initialize states randomly
def __getitem__(self, key):
try:
value = self.cache[key]
except KeyError:
f = StringIO(self.dict[key])
value = Unpickler(f).load()
if self.writeback:
self.cache[key] = value
return value
def set_location(self, key):
(key, value) = self.dict.set_location(key)
f = StringIO(value)
return (key, Unpickler(f).load())
def next(self):
(key, value) = self.dict.next()
f = StringIO(value)
return (key, Unpickler(f).load())
def previous(self):
(key, value) = self.dict.previous()
f = StringIO(value)
return (key, Unpickler(f).load())
def first(self):
(key, value) = self.dict.first()
f = StringIO(value)
return (key, Unpickler(f).load())
def __init__(self, servers=None, debug=0,
pickleProtocol=cPickle.HIGHEST_PROTOCOL,
pickler=cPickle.Pickler,
unpickler=cPickle.Unpickler,
pload=None,
pid=None,
make_sync_call=None,
_app_id=None):
"""Create a new Client object.
No parameters are required.
Arguments:
servers: Ignored; only for compatibility.
debug: Ignored; only for compatibility.
pickleProtocol: Pickle protocol to use for pickling the object.
pickler: pickle.Pickler sub-class to use for pickling.
unpickler: pickle.Unpickler sub-class to use for unpickling.
pload: Callable to use for retrieving objects by persistent id.
pid: Callable to use for determine the persistent id for objects, if any.
make_sync_call: Ignored; only for compatibility with an earlier version.
"""
self._pickler_factory = pickler
self._unpickler_factory = unpickler
self._pickle_protocol = pickleProtocol
self._persistent_id = pid
self._persistent_load = pload
self._app_id = _app_id
self._cas_ids = {}
def loads(self, buf):
f = self.input(buf)
try:
p = cPickle.Unpickler(f)
return p.load()
finally:
self.close(f)
def loads(self, *args):
f = self.input(args[0])
try:
p = cPickle.Unpickler(f)
return p.load()
finally:
self.close(f)