我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cPickle.Unpickler()。
def __init__(self, request, servers, debug=0, pickleProtocol=0, pickler=pickle.Pickler, unpickler=pickle.Unpickler, pload=None, pid=None, default_time_expire = DEFAULT_TIME_EXPIRE): self.request=request self.default_time_expire = default_time_expire if request: app = request.application else: app = '' Client.__init__(self, servers, debug, pickleProtocol, pickler, unpickler, pload, pid) if not app in self.meta_storage: self.storage = self.meta_storage[app] = { CacheAbstract.cache_stats_name: { 'hit_total': 0, 'misses': 0, }} else: self.storage = self.meta_storage[app]
def load(self, file_path, varnum=0): """ load variables that previously saved using self.save() varnum : number of variables u want to load (0 mean it will load all) Note: if you are loading class instance(s), you must have it defined in advance """ with open(file_path,'rb') as f: pickler = pickle.Unpickler(f) var_count = pickler.load() if varnum: var_count = min([var_count,varnum]) out = {} for i in range(var_count): key = pickler.load() out[key] = pickler.load() return out
def Decoder(self, raw): """Safe Unpickling. Unpickle only safe primitives like tuples, dicts and strings. Specifically does not allow arbitrary instances to be recovered. """ now = time.time() unpickler = cPickle.Unpickler(cStringIO.StringIO(raw)) unpickler.find_global = None try: decoded = unpickler.load() except Exception: raise io_manager.DecodeError("Unable to unpickle cached object") result = self.renderer.decoder.Decode(decoded) self.session.logging.debug("Decoded in %s sec.", time.time() - now) return result
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 load_object(fname,zip=0,nofind=0,verbose=0): """Loads an object from disk. By default, this handles zipped files and searches in the usual places for OCRopus. It also handles some class names that have changed.""" if not nofind: fname = ocropus_find_file(fname) if verbose: print("# loading object", fname) if zip==0 and fname.endswith(".gz"): zip = 1 if zip>0: # with gzip.GzipFile(fname,"rb") as stream: with os.popen("gunzip < '%s'"%fname,"rb") as stream: unpickler = cPickle.Unpickler(stream) unpickler.find_global = unpickle_find_global return unpickler.load() else: with open(fname,"rb") as stream: unpickler = cPickle.Unpickler(stream) unpickler.find_global = unpickle_find_global return unpickler.load() ################################################################ ### Simple record object. ################################################################
def unpickle(self): """Deserialize `data` into an object.""" IOLOG.debug('%r.unpickle()', self) fp = cStringIO.StringIO(self.data) unpickler = cPickle.Unpickler(fp) unpickler.find_global = self._find_global try: return unpickler.load() except (TypeError, ValueError), ex: raise StreamError('invalid message: %s', ex)
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)
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