我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用pickle.DEFAULT_PROTOCOL。
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
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
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 = 1 # 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, protocol=protocol) # delayed import of numpy, to avoid tight coupling try: import numpy as np except ImportError: np = None self.np = np
def send_pyobj(self, obj, flags=0, protocol=DEFAULT_PROTOCOL): """send a Python object as a message using pickle to serialize Parameters ---------- obj : Python object The Python object to send. flags : int Any valid send flag. 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)
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)
def serialize_block(self): """?? Class serialize Pickle ? ???? serialize ? :return: serialize ?? """ return pickle.dumps(self, pickle.DEFAULT_PROTOCOL)
def send_pyobj(self, obj, flags=0, protocol=DEFAULT_PROTOCOL): """send a Python object as a message using pickle to serialize Parameters ---------- obj : Python object The Python object to send. flags : int Any valid send flag. 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)
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 else pickle.HIGHEST_PROTOCOL) Pickler.__init__(self, self.stream, protocol=protocol) # Initialise the hash obj self._hash = hashlib.new(hash_name)
def __init__(self): self.stream = io.BytesIO() self.dependents = [] self.branches = [] protocol = (pickle.DEFAULT_PROTOCOL if PY3_OR_LATER else pickle.HIGHEST_PROTOCOL) Pickler.__init__(self, self.stream, protocol=protocol)
def __init__(self, hash_name='sha1'): 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)
def dump(obj, file_handler, protocol=DEFAULT_PROTOCOL, persistent_id=PersistentSharedVariableID): """Pickles an object to a zip file using external persistence. :param obj: The object to pickle. :type obj: object :param file_handler: The file handle to save the object to. :type file_handler: file :param protocol: 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. :type protocol: int, optional :param persistent_id: The callable that persists certain objects in the object hierarchy to separate files inside of the zip file. For example, :class:`PersistentNdarrayID` saves any :class:`numpy.ndarray` to a separate NPY file inside of the zip file. :type persistent_id: callable .. versionadded:: 0.8 .. note:: The final file is simply a zipped file containing at least one file, `pkl`, which contains the pickled object. It can contain any other number of external objects. Note that the zip files are compatible with NumPy's :func:`numpy.load` function. >>> import theano >>> foo_1 = theano.shared(0, name='foo') >>> foo_2 = theano.shared(1, name='foo') >>> with open('model.zip', 'wb') as f: ... dump((foo_1, foo_2, numpy.array(2)), f) >>> numpy.load('model.zip').keys() ['foo', 'foo_2', 'array_0', 'pkl'] >>> numpy.load('model.zip')['foo'] array(0) >>> with open('model.zip', 'rb') as f: ... foo_1, foo_2, array = load(f) >>> array array(2) """ with closing(zipfile.ZipFile(file_handler, 'w', zipfile.ZIP_DEFLATED, allowZip64=True)) as zip_file: def func(f): p = pickle.Pickler(f, protocol=protocol) p.persistent_id = persistent_id(zip_file) p.dump(obj) zipadd(func, zip_file, 'pkl')