當前位置: 首頁>>代碼示例>>Python>>正文


Python copyreg.pickle方法代碼示例

本文整理匯總了Python中copyreg.pickle方法的典型用法代碼示例。如果您正苦於以下問題:Python copyreg.pickle方法的具體用法?Python copyreg.pickle怎麽用?Python copyreg.pickle使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在copyreg的用法示例。


在下文中一共展示了copyreg.pickle方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: patch_Keypoint_pickiling

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def patch_Keypoint_pickiling():
        # Create the bundling between class and arguements to save for Keypoint class
        # See : https://stackoverflow.com/questions/50337569/pickle-exception-for-cv2-boost-when-using-multiprocessing/50394788#50394788
        def _pickle_keypoint(keypoint):  # : cv2.KeyPoint
            return cv2.KeyPoint, (
                keypoint.pt[0],
                keypoint.pt[1],
                keypoint.size,
                keypoint.angle,
                keypoint.response,
                keypoint.octave,
                keypoint.class_id,
            )

        # C++ : KeyPoint (float x, float y, float _size, float _angle=-1, float _response=0, int _octave=0, int _class_id=-1)
        # Python: cv2.KeyPoint([x, y, _size[, _angle[, _response[, _octave[, _class_id]]]]]) → <KeyPoint object>

        # Apply the bundling to pickle
        copyreg.pickle(cv2.KeyPoint().__class__, _pickle_keypoint)

    # non static, to be sure we patched it before use, only once 
開發者ID:CIRCL,項目名稱:douglas-quaid,代碼行數:23,代碼來源:pickle_import_export.py

示例2: _pickleFunction

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def _pickleFunction(f):
    """
    Reduce, in the sense of L{pickle}'s C{object.__reduce__} special method, a
    function object into its constituent parts.

    @param f: The function to reduce.
    @type f: L{types.FunctionType}

    @return: a 2-tuple of a reference to L{_unpickleFunction} and a tuple of
        its arguments, a 1-tuple of the function's fully qualified name.
    @rtype: 2-tuple of C{callable, native string}
    """
    if f.__name__ == '<lambda>':
        raise _UniversalPicklingError(
            "Cannot pickle lambda function: {}".format(f))
    return (_unpickleFunction,
            tuple([".".join([f.__module__, f.__qualname__])])) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:19,代碼來源:styles.py

示例3: test_nonIdentityHash

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def test_nonIdentityHash(self):
        global ClassWithCustomHash
        class ClassWithCustomHash(styles.Versioned):
            def __init__(self, unique, hash):
                self.unique = unique
                self.hash = hash
            def __hash__(self):
                return self.hash
        
        v1 = ClassWithCustomHash('v1', 0)
        v2 = ClassWithCustomHash('v2', 0)

        pkl = pickle.dumps((v1, v2))
        del v1, v2
        ClassWithCustomHash.persistenceVersion = 1
        ClassWithCustomHash.upgradeToVersion1 = lambda self: setattr(self, 'upgraded', True)
        v1, v2 = pickle.loads(pkl)
        styles.doUpgrade()
        self.assertEqual(v1.unique, 'v1')
        self.assertEqual(v2.unique, 'v2')
        self.assertTrue(v1.upgraded)
        self.assertTrue(v2.upgraded) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:24,代碼來源:test_persisted.py

示例4: test_upgradeDeserializesObjectsRequiringUpgrade

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def test_upgradeDeserializesObjectsRequiringUpgrade(self):
        global ToyClassA, ToyClassB
        class ToyClassA(styles.Versioned):
            pass
        class ToyClassB(styles.Versioned):
            pass
        x = ToyClassA()
        y = ToyClassB()
        pklA, pklB = pickle.dumps(x), pickle.dumps(y)
        del x, y
        ToyClassA.persistenceVersion = 1
        def upgradeToVersion1(self):
            self.y = pickle.loads(pklB)
            styles.doUpgrade()
        ToyClassA.upgradeToVersion1 = upgradeToVersion1
        ToyClassB.persistenceVersion = 1
        ToyClassB.upgradeToVersion1 = lambda self: setattr(self, 'upgraded', True)

        x = pickle.loads(pklA)
        styles.doUpgrade()
        self.assertTrue(x.y.upgraded) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:23,代碼來源:test_persisted.py

示例5: __init__

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def __init__(self, in_func, name):
        """Creates a serializable (picklable) object of a function"""
        self.code = marshal.dumps(in_func.__code__)
        self.name = name
        self.defaults = in_func.__defaults__
        # Pickle references to functions used in the function
        used_globals = {} # name: function
        used_modules = {} # used name: origin module name
        for key, value in in_func.__globals__.items():
            if key in in_func.__code__.co_names:
                if ismodule(value):
                    used_modules[key] = value.__name__
                else:
                    used_globals[key] = value
        self.globals = pickle.dumps(used_globals, pickle.HIGHEST_PROTOCOL)
        self.imports = used_modules 
開發者ID:soravux,項目名稱:scoop,代碼行數:18,代碼來源:encapsulation.py

示例6: writeFile

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def writeFile(self, directory=None):
        """Writes back the file to a temporary path (optionaly specified)"""
        if directory:
            # If a directory was specified
            full_path = os.path.join(directory, self.filename)
            with open(full_path, 'wb') as f:
                f.write(pickle.loads(self.data).read())
            return full_path

        # if no directory was specified, create a temporary file
        this_file = tempfile.NamedTemporaryFile(delete=False)
        this_file.write(pickle.loads(self.data).read())
        this_file.close()

        return this_file.name


# The following block handles callables pickling and unpickling

# TODO: Make a factory to generate unpickling functions 
開發者ID:soravux,項目名稱:scoop,代碼行數:22,代碼來源:encapsulation.py

示例7: _save_subimports

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def _save_subimports(self, code, top_level_dependencies):
        """
        Ensure de-pickler imports any package child-modules that
        are needed by the function
        """
        # check if any known dependency is an imported package
        for x in top_level_dependencies:
            if isinstance(x, types.ModuleType) and hasattr(x, '__package__') and x.__package__:
                # check if the package has any currently loaded sub-imports
                prefix = x.__name__ + '.'
                for name, module in sys.modules.items():
                    # Older versions of pytest will add a "None" module to sys.modules.
                    if name is not None and name.startswith(prefix):
                        # check whether the function can address the sub-module
                        tokens = set(name[len(prefix):].split('.'))
                        if not tokens - set(code.co_names):
                            # ensure unpickler executes this import
                            self.save(module)
                            # then discards the reference to it
                            self.write(pickle.POP) 
開發者ID:FederatedAI,項目名稱:FATE,代碼行數:22,代碼來源:cloudpickle.py

示例8: dumps

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [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 
開發者ID:FederatedAI,項目名稱:FATE,代碼行數:20,代碼來源:cloudpickle.py

示例9: test_badly_quoted_string

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def test_badly_quoted_string(self):
        # Issue #17710
        badpickles = [b"S'\n.",
                      b'S"\n.',
                      b'S\' \n.',
                      b'S" \n.',
                      b'S\'"\n.',
                      b'S"\'\n.',
                      b"S' ' \n.",
                      b'S" " \n.',
                      b"S ''\n.",
                      b'S ""\n.',
                      b'S \n.',
                      b'S\n.',
                      b'S.']
        for p in badpickles:
            self.assertRaises(pickle.UnpicklingError, self.loads, p) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:19,代碼來源:pickletester.py

示例10: pickle_exception

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def pickle_exception(obj):
    # All exceptions, unlike generic Python objects, define __reduce_ex__
    # __reduce_ex__(4) should be no different from __reduce_ex__(3).
    # __reduce_ex__(5) could bring benefits in the unlikely case the exception
    # directly contains buffers, but PickleBuffer objects will cause a crash when
    # running on protocol=4, and there's no clean way to figure out the current
    # protocol from here. Note that any object returned by __reduce_ex__(3) will
    # still be pickled with protocol 5 if pickle.dump() is running with it.
    rv = obj.__reduce_ex__(3)
    if isinstance(rv, str):
        raise TypeError("str __reduce__ output is not supported")
    assert isinstance(rv, tuple) and len(rv) >= 2

    return (unpickle_exception, rv[:2] + (obj.__cause__, obj.__traceback__)) + rv[2:] 
開發者ID:pywren,項目名稱:pywren-ibm-cloud,代碼行數:16,代碼來源:pickling_support.py

示例11: install

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def install(*exc_classes_or_instances):
    copyreg.pickle(TracebackType, pickle_traceback)

    if sys.version_info.major < 3:
        # Dummy decorator?
        if len(exc_classes_or_instances) == 1:
            exc = exc_classes_or_instances[0]
            if isinstance(exc, type) and issubclass(exc, BaseException):
                return exc
        return

    if not exc_classes_or_instances:
        for exception_cls in _get_subclasses(BaseException):
            copyreg.pickle(exception_cls, pickle_exception)
        return

    for exc in exc_classes_or_instances:
        if isinstance(exc, BaseException):
            while exc is not None:
                copyreg.pickle(type(exc), pickle_exception)
                exc = exc.__cause__
        elif isinstance(exc, type) and issubclass(exc, BaseException):
            copyreg.pickle(exc, pickle_exception)
            # Allow using @install as a decorator for Exception classes
            if len(exc_classes_or_instances) == 1:
                return exc
        else:
            raise TypeError(
                "Expected subclasses or instances of BaseException, got %s"
                % (type(exc))
            ) 
開發者ID:pywren,項目名稱:pywren-ibm-cloud,代碼行數:33,代碼來源:pickling_support.py

示例12: _ufunc_reconstruct

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def _ufunc_reconstruct(module, name):
    # The `fromlist` kwarg is required to ensure that `mod` points to the
    # inner-most module rather than the parent package when module name is
    # nested. This makes it possible to pickle non-toplevel ufuncs such as
    # scipy.special.expit for instance.
    mod = __import__(module, fromlist=[name])
    return getattr(mod, name) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:9,代碼來源:__init__.py

示例13: _ufunc_reduce

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:6,代碼來源:__init__.py

示例14: get_object_from_pickle

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def get_object_from_pickle(pickle_obj):
        # Return an object from the pickle version of it

        # The protocol version used is detected automatically, so we do not
        # have to specify it.
        return cPickle.loads(pickle_obj)

    # non static, to be sure we patched it before use, only once 
開發者ID:CIRCL,項目名稱:douglas-quaid,代碼行數:10,代碼來源:pickle_import_export.py

示例15: get_pickle_from_object

# 需要導入模塊: import copyreg [as 別名]
# 或者: from copyreg import pickle [as 別名]
def get_pickle_from_object(obj):
        # Return a pickle version of an object

        # Pickle the 'data' dictionary using the highest protocol available = the faster (>json since v3)
        return cPickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL) 
開發者ID:CIRCL,項目名稱:douglas-quaid,代碼行數:7,代碼來源:pickle_import_export.py


注:本文中的copyreg.pickle方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。