本文整理匯總了Python中pickle.DEFAULT_PROTOCOL屬性的典型用法代碼示例。如果您正苦於以下問題:Python pickle.DEFAULT_PROTOCOL屬性的具體用法?Python pickle.DEFAULT_PROTOCOL怎麽用?Python pickle.DEFAULT_PROTOCOL使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類pickle
的用法示例。
在下文中一共展示了pickle.DEFAULT_PROTOCOL屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: dumps
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def dumps(obj, protocol=None):
"""Serialize obj as a string of bytes allocated in memory
protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to
pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed
between processes running the same Python version.
Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure
compatibility with older versions of Python.
"""
file = StringIO()
try:
cp = CloudPickler(file, protocol=protocol)
cp.dump(obj)
return file.getvalue()
finally:
file.close()
# including pickles unloading functions in this namespace
示例2: __init__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def __init__(self, fp, protocol=None):
self.file_handle = fp
self.buffered = isinstance(self.file_handle, BinaryZlibFile)
# By default we want a pickle protocol that only changes with
# the major python version and not the minor one
if protocol is None:
protocol = (pickle.DEFAULT_PROTOCOL if PY3_OR_LATER
else pickle.HIGHEST_PROTOCOL)
Pickler.__init__(self, self.file_handle, protocol=protocol)
# delayed import of numpy, to avoid tight coupling
try:
import numpy as np
except ImportError:
np = None
self.np = np
示例3: dumps
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def dumps(obj, protocol=None):
"""Serialize obj as a string of bytes allocated in memory
protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to
pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed
between processes running the same Python version.
Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure
compatibility with older versions of Python.
"""
file = StringIO()
try:
cp = CloudPickler(file, protocol=protocol)
cp.dump(obj)
return file.getvalue()
finally:
file.close()
# including pickles unloading functions in this namespace
示例4: dumps
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def dumps(obj, protocol=None):
"""Serialize obj as a string of bytes allocated in memory
protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to
pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed
between processes running the same Python version.
Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure
compatibility with older versions of Python.
"""
file = BytesIO()
try:
cp = CloudPickler(file, protocol=protocol)
cp.dump(obj)
return file.getvalue()
finally:
file.close()
# including pickles unloading functions in this namespace
示例5: dumps
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def dumps(obj, protocol=None, buffer_callback=None):
"""Serialize obj as a string of bytes allocated in memory
protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to
pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed
between processes running the same Python version.
Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure
compatibility with older versions of Python.
"""
with io.BytesIO() as file:
cp = CloudPickler(file, protocol=protocol, buffer_callback=buffer_callback)
cp.dump(obj)
return file.getvalue()
# COLLECTION OF OBJECTS __getnewargs__-LIKE METHODS
# -------------------------------------------------
示例6: __init__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def __init__(self, save):
self.save = save
try:
self.db_experiences = shelve.Shelf(
LMDBDict("data/commit_experiences.lmdb", readonly=not save),
protocol=pickle.DEFAULT_PROTOCOL,
writeback=save,
)
except lmdb.Error as e:
if not save and "No such file or directory" in str(e):
self.db_experiences = {}
else:
raise
if not save:
self.mem_experiences = {}
示例7: dumps
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def dumps(obj, protocol=None):
"""Serialize obj as a string of bytes allocated in memory
protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to
pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed
between processes running the same Python version.
Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure
compatibility with older versions of Python.
"""
file = StringIO()
try:
cp = CloudPickler(file, protocol=protocol)
cp.dump(obj)
return file.getvalue()
finally:
file.close()
# including pickles unloading functions in this namespace
示例8: __init__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def __init__(self, filename, compress=0, cache_size=10, protocol=None):
self._filename = filename
self._filenames = [filename, ]
self.cache_size = cache_size
self.compress = compress
if not self.compress:
self.file = open(filename, 'wb')
else:
self.file = BytesIO()
# Count the number of npy files that we have created:
self._npy_counter = 0
# By default we want a pickle protocol that only changes with
# the major python version and not the minor one
if protocol is None:
protocol = (pickle.DEFAULT_PROTOCOL if PY3
else pickle.HIGHEST_PROTOCOL)
Pickler.__init__(self, self.file,
protocol=protocol)
# delayed import of numpy, to avoid tight coupling
try:
import numpy as np
except ImportError:
np = None
self.np = np
示例9: __init__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def __init__(self, file, protocol=None):
if protocol is None:
protocol = DEFAULT_PROTOCOL
Pickler.__init__(self, file, protocol=protocol)
# set of modules to unpickle
self.modules = set()
# map ids to dictionary. used to ensure that functions can share global env
self.globals_ref = {}
示例10: dump
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def dump(obj, file, protocol=None):
"""Serialize obj as bytes streamed into file
protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to
pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed
between processes running the same Python version.
Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure
compatibility with older versions of Python.
"""
CloudPickler(file, protocol=protocol).dump(obj)
示例11: send_pyobj
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def send_pyobj(self, obj, flags=0, protocol=DEFAULT_PROTOCOL, **kwargs):
"""Send a Python object as a message using pickle to serialize.
Parameters
----------
obj : Python object
The Python object to send.
flags : int
Any valid flags for :func:`Socket.send`.
protocol : int
The pickle protocol number to use. The default is pickle.DEFAULT_PROTOCOL
where defined, and pickle.HIGHEST_PROTOCOL elsewhere.
"""
msg = pickle.dumps(obj, protocol)
return self.send(msg, flags=flags, **kwargs)
示例12: __init__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def __init__(self, hash_name='md5'):
self.stream = io.BytesIO()
# By default we want a pickle protocol that only changes with
# the major python version and not the minor one
protocol = (pickle.DEFAULT_PROTOCOL if PY3_OR_LATER
else pickle.HIGHEST_PROTOCOL)
Pickler.__init__(self, self.stream, protocol=protocol)
# Initialise the hash obj
self._hash = hashlib.new(hash_name)
示例13: dump
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def dump(obj, file, protocol=None):
"""Serialize obj as bytes streamed into file
protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to
pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed
between processes running the same Python version.
Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure
compatibility with older versions of Python.
"""
CloudPickler(file, protocol=protocol).dump(obj)
示例14: dump_and_add_to_dump
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def dump_and_add_to_dump(object_, file_, parameters=None, to_add=None,
use_cpickle=False, protocol=DEFAULT_PROTOCOL,
**kwargs):
r"""Calls both `dump` and `add_to_dump` to serialze several objects.
This function is used to serialize several at the same time, using
persistent ID. Its main advantage is that it can be used with
`secure_dump`.
Parameters
----------
object_ : object
The object to pickle. If None, only the parameters passed to the
`parameters` argument will be saved.
file_ : file
The destination for saving.
parameters : list, optional
Shared variables whose internal numpy arrays should be saved
separately in the `_parameters` field of the tar file.
to_add : dict of objects
A {'name': object} dictionnary of additional objects to save in
the tar archive. Its keys will be used as name in the tar file.
use_cpickle : bool
Use cPickle instead of pickle. Setting it to true will disable the
warning message if you try to pickle objects from the main module,
so be sure that there is no warning before turning this flag
on. Default: False.
protocol : int, optional
The pickling protocol to use. Unlike Python's built-in pickle, the
default is set to `2` instead of 0 for Python 2. The Python 3
default (level 3) is maintained.
\*\*kwargs
Keyword arguments to be passed to `pickle.Pickler`.
"""
dump(object_, file_, parameters=parameters, use_cpickle=use_cpickle,
protocol=protocol, **kwargs)
if to_add is not None:
for name, obj in six.iteritems(to_add):
add_to_dump(obj, file_, name, parameters=parameters,
use_cpickle=use_cpickle, protocol=protocol, **kwargs)
示例15: __init__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import DEFAULT_PROTOCOL [as 別名]
def __init__(self, file, protocol=None):
if protocol is None:
protocol = DEFAULT_PROTOCOL
Pickler.__init__(self, file, protocol=protocol)
# map ids to dictionary. used to ensure that functions can share global env
self.globals_ref = {}