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


Python _pickle.Pickler方法代碼示例

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


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

示例1: test_cpickle

# 需要導入模塊: import _pickle [as 別名]
# 或者: from _pickle import Pickler [as 別名]
def test_cpickle(_cache={}):
    import io
    try:
        import _pickle
    except ImportError:
        print("cannot import _pickle, skipped!")
        return
    k, l = None, None
    for n in itertools.count():
        try:
            l = _cache[n]
            continue  # Already tried and it works, let's save some time
        except KeyError:
            for i in range(100):
                l = [k, l]
                k = {i: l}
        _pickle.Pickler(io.BytesIO(), protocol=-1).dump(l)
        _cache[n] = l 
開發者ID:holzschu,項目名稱:python3_ios,代碼行數:20,代碼來源:find_recursionlimit.py

示例2: test_pickler

# 需要導入模塊: import _pickle [as 別名]
# 或者: from _pickle import Pickler [as 別名]
def test_pickler(self):
            basesize = support.calcobjsize('5P2n3i2n3iP')
            p = _pickle.Pickler(io.BytesIO())
            self.assertEqual(object.__sizeof__(p), basesize)
            MT_size = struct.calcsize('3nP0n')
            ME_size = struct.calcsize('Pn0P')
            check = self.check_sizeof
            check(p, basesize +
                MT_size + 8 * ME_size +  # Minimal memo table size.
                sys.getsizeof(b'x'*4096))  # Minimal write buffer size.
            for i in range(6):
                p.dump(chr(i))
            check(p, basesize +
                MT_size + 32 * ME_size +  # Size of memo table required to
                                          # save references to 6 objects.
                0)  # Write buffer is cleared after every dump(). 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:18,代碼來源:test_pickle.py

示例3: test_signature_on_builtin_class

# 需要導入模塊: import _pickle [as 別名]
# 或者: from _pickle import Pickler [as 別名]
def test_signature_on_builtin_class(self):
        self.assertEqual(str(inspect.signature(_pickle.Pickler)),
                         '(file, protocol=None, fix_imports=True)')

        class P(_pickle.Pickler): pass
        class EmptyTrait: pass
        class P2(EmptyTrait, P): pass
        self.assertEqual(str(inspect.signature(P)),
                         '(file, protocol=None, fix_imports=True)')
        self.assertEqual(str(inspect.signature(P2)),
                         '(file, protocol=None, fix_imports=True)')

        class P3(P2):
            def __init__(self, spam):
                pass
        self.assertEqual(str(inspect.signature(P3)), '(spam)')

        class MetaP(type):
            def __call__(cls, foo, bar):
                pass
        class P4(P2, metaclass=MetaP):
            pass
        self.assertEqual(str(inspect.signature(P4)), '(foo, bar)') 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:25,代碼來源:test_inspect.py

示例4: _function_reduce

# 需要導入模塊: import _pickle [as 別名]
# 或者: from _pickle import Pickler [as 別名]
def _function_reduce(self, obj):
        """Reducer for function objects.

        If obj is a top-level attribute of a file-backed module, this
        reducer returns NotImplemented, making the CloudPickler fallback to
        traditional _pickle.Pickler routines to save obj. Otherwise, it reduces
        obj using a custom cloudpickle reducer designed specifically to handle
        dynamic functions.

        As opposed to cloudpickle.py, There no special handling for builtin
        pypy functions because cloudpickle_fast is CPython-specific.
        """
        if _is_importable_by_name(obj):
            return NotImplemented
        else:
            return self._dynamic_function_reduce(obj) 
開發者ID:ray-project,項目名稱:ray,代碼行數:18,代碼來源:cloudpickle_fast.py

示例5: test_unbound_builtin_method

# 需要導入模塊: import _pickle [as 別名]
# 或者: from _pickle import Pickler [as 別名]
def test_unbound_builtin_method(self):
        self.assertEqual(self._get_summary_line(_pickle.Pickler.dump),
            "dump(self, obj, /)")

    # these no longer include "self" 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:7,代碼來源:test_pydoc.py

示例6: test_bound_builtin_method

# 需要導入模塊: import _pickle [as 別名]
# 或者: from _pickle import Pickler [as 別名]
def test_bound_builtin_method(self):
        s = StringIO()
        p = _pickle.Pickler(s)
        self.assertEqual(self._get_summary_line(p.dump),
            "dump(obj, /) method of _pickle.Pickler instance")

    # this should *never* include self! 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:9,代碼來源:test_pydoc.py

示例7: test_getfullargspec_builtin_methods

# 需要導入模塊: import _pickle [as 別名]
# 或者: from _pickle import Pickler [as 別名]
def test_getfullargspec_builtin_methods(self):
        self.assertFullArgSpecEquals(_pickle.Pickler.dump,
                                     args_e=['self', 'obj'], formatted='(self, obj)')

        self.assertFullArgSpecEquals(_pickle.Pickler(io.BytesIO()).dump,
                                     args_e=['self', 'obj'], formatted='(self, obj)')

        self.assertFullArgSpecEquals(
             os.stat,
             args_e=['path'],
             kwonlyargs_e=['dir_fd', 'follow_symlinks'],
             kwonlydefaults_e={'dir_fd': None, 'follow_symlinks': True},
             formatted='(path, *, dir_fd=None, follow_symlinks=True)') 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:15,代碼來源:test_inspect.py

示例8: __init__

# 需要導入模塊: import _pickle [as 別名]
# 或者: from _pickle import Pickler [as 別名]
def __init__(self, file, protocol=None, buffer_callback=None):
        if protocol is None:
            protocol = DEFAULT_PROTOCOL
        Pickler.__init__(self, file, protocol=protocol, buffer_callback=buffer_callback)
        # map functions __globals__ attribute ids, to ensure that functions
        # sharing the same global namespace at pickling time also share their
        # global namespace at unpickling time.
        self.globals_ref = {}

        # Take into account potential custom reducers registered by external
        # modules
        self.dispatch_table = copyreg.dispatch_table.copy()
        self.dispatch_table.update(self.dispatch)
        self.proto = int(protocol) 
開發者ID:ray-project,項目名稱:ray,代碼行數:16,代碼來源:cloudpickle_fast.py

示例9: dump

# 需要導入模塊: import _pickle [as 別名]
# 或者: from _pickle import Pickler [as 別名]
def dump(self, obj):
        try:
            return Pickler.dump(self, obj)
        except RuntimeError as e:
            if "recursion" in e.args[0]:
                msg = (
                    "Could not pickle object as excessively deep recursion "
                    "required."
                )
                raise pickle.PicklingError(msg)
            else:
                raise 
開發者ID:ray-project,項目名稱:ray,代碼行數:14,代碼來源:cloudpickle_fast.py

示例10: reducer_override

# 需要導入模塊: import _pickle [as 別名]
# 或者: from _pickle import Pickler [as 別名]
def reducer_override(self, obj):
        """Type-agnostic reducing callback for function and classes.

        For performance reasons, subclasses of the C _pickle.Pickler class
        cannot register custom reducers for functions and classes in the
        dispatch_table. Reducer for such types must instead implemented in the
        special reducer_override method.

        Note that method will be called for any object except a few
        builtin-types (int, lists, dicts etc.), which differs from reducers in
        the Pickler's dispatch_table, each of them being invoked for objects of
        a specific type only.

        This property comes in handy for classes: although most classes are
        instances of the ``type`` metaclass, some of them can be instances of
        other custom metaclasses (such as enum.EnumMeta for example). In
        particular, the metaclass will likely not be known in advance, and thus
        cannot be special-cased using an entry in the dispatch_table.
        reducer_override, among other things, allows us to register a reducer
        that will be called for any class, independently of its type.


        Notes:

        * reducer_override has the priority over dispatch_table-registered
          reducers.
        * reducer_override can be used to fix other limitations of cloudpickle
          for other types that suffered from type-specific reducers, such as
          Exceptions. See https://github.com/cloudpipe/cloudpickle/issues/248
        """

        # This is a patch for python3.5
        if isinstance(obj, numpy.ndarray):
            if (self.proto < 5 or
                    (not obj.flags.c_contiguous and not obj.flags.f_contiguous) or
                    (issubclass(type(obj), numpy.ndarray) and type(obj) is not numpy.ndarray) or
                    obj.dtype == "O" or obj.itemsize == 0):
                return NotImplemented
            return _numpy_ndarray_reduce(obj)

        t = type(obj)
        try:
            is_anyclass = issubclass(t, type)
        except TypeError:  # t is not a class (old Boost; see SF #502085)
            is_anyclass = False

        if is_anyclass:
            return _class_reduce(obj)
        elif isinstance(obj, types.FunctionType):
            return self._function_reduce(obj)
        else:
            # fallback to save_global, including the Pickler's distpatch_table
            return NotImplemented

    # function reducers are defined as instance methods of CloudPickler
    # objects, as they rely on a CloudPickler attribute (globals_ref) 
開發者ID:ray-project,項目名稱:ray,代碼行數:58,代碼來源:cloudpickle_fast.py


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