def is_module_accelerated(module):
return getattr(pickle.Pickler, '__module__', '<jython>') == 'pickle'
python类Pickler()的实例源码
def _SaveDicts():
if is_readonly:
raise RuntimeError("Trying to write to a readonly gencache ('%s')!" \
% win32com.__gen_path__)
f = open(os.path.join(GetGeneratePath(), "dicts.dat"), "wb")
try:
p = pickle.Pickler(f)
p.dump(pickleVersion)
p.dump(clsidToTypelib)
finally:
f.close()
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 dumps(self, arg, proto=0):
f = self.output()
try:
p = cPickle.Pickler(f, proto)
p.dump(arg)
f.seek(0)
return f.read()
finally:
self.close(f)
def dumps(self, arg, proto=0):
p = cPickle.Pickler(proto)
p.dump(arg)
return p.getvalue()
def dumps(self, arg, proto=0):
f = self.output()
try:
p = cPickle.Pickler(f, proto)
p.fast = 1
p.dump(arg)
f.seek(0)
return f.read()
finally:
self.close(f)
def __setitem__(self, key, value):
if self.writeback:
self.cache[key] = value
f = StringIO()
p = Pickler(f, self._protocol)
p.dump(value)
self.dict[key] = f.getvalue()
def serial(result, fname = "temp.bin"):
if charade.detect(fname)['encoding'] == 'utf-8':
fname = convert(fname)
root_dir = os.path.dirname(__file__)
fname = root_dir + "\\" +fname
f = open(fname,"wb")
p = cPickle.Pickler(f)
p.clear_memo()
p.fast = True
p.dump(result)
f.close()
def dumps(self, arg, proto=0):
f = self.output()
try:
p = cPickle.Pickler(f, proto)
p.dump(arg)
f.seek(0)
return f.read()
finally:
self.close(f)
def dumps(self, arg, proto=0):
p = cPickle.Pickler(proto)
p.dump(arg)
return p.getvalue()
def dumps(self, arg, proto=0):
f = self.output()
try:
p = cPickle.Pickler(f, proto)
p.fast = 1
p.dump(arg)
f.seek(0)
return f.read()
finally:
self.close(f)
def __setitem__(self, key, value):
if self.writeback:
self.cache[key] = value
f = StringIO()
p = Pickler(f, self._protocol)
p.dump(value)
self.dict[key] = f.getvalue()
def __WritePickled(self, obj, filename):
"""Pickles the object and writes it to the given file.
"""
if not filename or filename == '/dev/null' or not obj:
return
descriptor, tmp_filename = tempfile.mkstemp(dir=os.path.dirname(filename))
tmpfile = os.fdopen(descriptor, 'wb')
pickler = pickle.Pickler(tmpfile, protocol=1)
pickler.fast = True
pickler.dump(obj)
tmpfile.close()
self.__file_lock.acquire()
try:
try:
os.rename(tmp_filename, filename)
except OSError:
try:
os.remove(filename)
except:
pass
os.rename(tmp_filename, filename)
finally:
self.__file_lock.release()
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 __setitem__(self, key, value):
if self.writeback:
self.cache[key] = value
f = StringIO()
p = Pickler(f, self._protocol)
p.dump(value)
self.dict[key] = f.getvalue()
def __setitem__(self, key, value):
with self._cache_write_lock:
self._cache[key] = value
f = StringIO()
p = Pickler(f, self._protocol)
p.dump(value)
self._storage.redis.hset(self._hash_key, key, f.getvalue())
def sync(self):
if not self._cache:
return
with self._cache_write_lock, self._storage.redis.pipeline() as pipeline:
for key, entry in self._cache.items():
f = StringIO()
p = Pickler(f, self._protocol)
p.dump(entry)
pipeline.hset(self._hash_key, key, f.getvalue())
pipeline.execute()
self._cache.clear()
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 dumps(self, obj, protocol=None, bin=None):
src = BytesIO()
p = Pickler(src)
p.persistent_id = self._get_ids
p.dump(obj)
return src.getvalue()
def dumps(self, obj, protocol=None, bin=None):
src = BytesIO()
p = Pickler(src)
p.persistent_id = self._get_ids
p.dump(obj)
return src.getvalue()