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


Python Pickler.save_global方法代碼示例

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


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

示例1: save_function

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_function(self, obj, name=None):
        """ Registered with the dispatch to handle all function types.

        Determines what kind of function obj is (e.g. lambda, defined at
        interactive prompt, etc) and handles the pickling appropriately.
        """
        name = getattr(obj, '__name__', None)
        module_name = _whichmodule(obj, name)
        module = sys.modules.get(module_name, None)
        if module and 'cloudpickle' not in module.__name__:
            self.modules.add(module)

        if _is_global(obj, name=name):
            return Pickler.save_global(self, obj, name=name)
        elif PYPY and isinstance(obj.__code__, builtin_code_type):
            return self.save_pypy_builtin_func(obj)
        else:
            return self.save_function_tuple(obj) 
開發者ID:pywren,項目名稱:pywren-ibm-cloud,代碼行數:20,代碼來源:cloudpickle.py

示例2: save_builtin_function_or_method

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_builtin_function_or_method(self, obj):
            is_bound = getattr(obj, '__self__', None) is not None
            if is_bound:
                # obj is a bound builtin method.
                rv = (getattr, (obj.__self__, obj.__name__))
                return self.save_reduce(obj=obj, *rv)

            is_unbound = hasattr(obj, '__objclass__')
            if is_unbound:
                # obj is an unbound builtin method (accessed from its class)
                rv = (getattr, (obj.__objclass__, obj.__name__))
                return self.save_reduce(obj=obj, *rv)

            # Otherwise, obj is not a method, but a function. Fallback to
            # default pickling by attribute.
            return Pickler.save_global(self, obj) 
開發者ID:pywren,項目名稱:pywren-ibm-cloud,代碼行數:18,代碼來源:cloudpickle.py

示例3: save_global

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_global(self, obj, name=None, pack=struct.pack):
        """
        Save a "global".
        The name of this method is somewhat misleading: all types get
        dispatched here.
        """
        if obj.__module__ == "__main__":
            return self.save_dynamic_class(obj)

        try:
            return Pickler.save_global(self, obj, name=name)
        except Exception:
            if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
                if obj in _BUILTIN_TYPE_NAMES:
                    return self.save_reduce(
                        _builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)

            typ = type(obj)
            if typ is not obj and isinstance(obj, (type, types.ClassType)):
                return self.save_dynamic_class(obj)

            raise 
開發者ID:FederatedAI,項目名稱:FATE,代碼行數:24,代碼來源:cloudpickle.py

示例4: save_global

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_global(self, obj, name=None, pack=struct.pack):
        """
        Save a "global".

        The name of this method is somewhat misleading: all types get
        dispatched here.
        """
        if obj.__module__ == "__main__":
            return self.save_dynamic_class(obj)

        try:
            return Pickler.save_global(self, obj, name=name)
        except Exception:
            if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
                if obj in _BUILTIN_TYPE_NAMES:
                    return self.save_reduce(
                        _builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)

            typ = type(obj)
            if typ is not obj and isinstance(obj, (type, types.ClassType)):
                return self.save_dynamic_class(obj)

            raise 
開發者ID:runawayhorse001,項目名稱:LearningApacheSpark,代碼行數:25,代碼來源:cloudpickle.py

示例5: save_function

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_function(pickler, obj):
    if not _locate_function(obj): #, pickler._session):
        log.info("F1: %s" % obj)
        if getattr(pickler, '_recurse', False):
            # recurse to get all globals referred to by obj
            from .detect import globalvars
            globs = globalvars(obj, recurse=True, builtin=True)
            if obj in globs.values(): #ABORT: use _recurse=False
                globs = obj.__globals__ if PY3 else obj.func_globals
        else:
            globs = obj.__globals__ if PY3 else obj.func_globals
        if PY3:
            pickler.save_reduce(_create_function, (obj.__code__,
                                globs, obj.__name__,
                                obj.__defaults__, obj.__closure__,
                                obj.__dict__), obj=obj)
        else:
            pickler.save_reduce(_create_function, (obj.func_code,
                                globs, obj.func_name,
                                obj.func_defaults, obj.func_closure,
                                obj.__dict__), obj=obj)
    else:
        log.info("F2: %s" % obj)
        StockPickler.save_global(pickler, obj) #NOTE: also takes name=...
    return 
開發者ID:dagbldr,項目名稱:dagbldr,代碼行數:27,代碼來源:dill.py

示例6: save_builtin_method

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_builtin_method(pickler, obj):
    if obj.__self__ is not None:
        if obj.__self__ is __builtin__:
            module = 'builtins' if PY3 else '__builtin__'
            log.info("B1: %s" % obj)
        else:
            module = obj.__self__
            log.info("B3: %s" % obj)
        _recurse = pickler._recurse
        pickler._recurse = False
        pickler.save_reduce(_get_attr, (module, obj.__name__), obj=obj)
        pickler._recurse = _recurse
    else:
        log.info("B2: %s" % obj)
        StockPickler.save_global(pickler, obj)
    return 
開發者ID:dagbldr,項目名稱:dagbldr,代碼行數:18,代碼來源:dill.py

示例7: save_global

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_global(self, obj, name=None, pack=struct.pack):
    """
    Save a "global".
    The name of this method is somewhat misleading: all types get
    dispatched here.
    """
    if obj.__module__ == "__main__":
      return self.save_dynamic_class(obj)

    try:
      return Pickler.save_global(self, obj, name=name)
    except Exception:
      if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
        if obj in _BUILTIN_TYPE_NAMES:
          return self.save_reduce(
              _builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)

      typ = type(obj)
      if typ is not obj and isinstance(obj, (type, types.ClassType)):
        return self.save_dynamic_class(obj)

      raise 
開發者ID:WeBankFinTech,項目名稱:eggroll,代碼行數:24,代碼來源:cloudpickle.py

示例8: save_global

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_global(self, obj, name=None, pack=struct.pack):
        """
        Save a "global".

        The name of this method is somewhat misleading: all types get
        dispatched here.
        """
        try:
            module_name = _whichmodule(obj, obj.__name__)
        except Exception:
            module_name = _whichmodule(obj, None)
        module = sys.modules.get(module_name, None)
        if module and 'cloudpickle' not in module.__name__:
            self.modules.add(module)

        if obj is type(None):
            return self.save_reduce(type, (None,), obj=obj)
        elif obj is type(Ellipsis):
            return self.save_reduce(type, (Ellipsis,), obj=obj)
        elif obj is type(NotImplemented):
            return self.save_reduce(type, (NotImplemented,), obj=obj)
        elif obj in _BUILTIN_TYPE_NAMES:
            return self.save_reduce(
                _builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)
        elif name is not None:
            Pickler.save_global(self, obj, name=name)
        elif not _is_global(obj, name=name):
            self.save_dynamic_class(obj)
        else:
            Pickler.save_global(self, obj, name=name) 
開發者ID:pywren,項目名稱:pywren-ibm-cloud,代碼行數:32,代碼來源:cloudpickle.py

示例9: save_builtin_function

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_builtin_function(self, obj):
        if obj.__module__ == "__builtin__":
            return self.save_global(obj)
        return self.save_function(obj) 
開發者ID:FederatedAI,項目名稱:FATE,代碼行數:6,代碼來源:cloudpickle.py

示例10: save_global

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_global(self, obj, name=None, **kwargs):
        module = getattr(obj, '__module__', None)
        if module == '__main__':
            warnings.warn(
                MAIN_MODULE_WARNING.format(kwargs.get('name', obj.__name__))
            )
        _Pickler.save_global(self, obj, name=name, **kwargs) 
開發者ID:rizar,項目名稱:attention-lvcsr,代碼行數:9,代碼來源:serialization.py

示例11: save_global

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_global(self, obj, name=None, pack=struct.pack):
        """
        Save a "global".

        The name of this method is somewhat misleading: all types get
        dispatched here.
        """
        if obj is type(None):
            return self.save_reduce(type, (None,), obj=obj)
        elif obj is type(Ellipsis):
            return self.save_reduce(type, (Ellipsis,), obj=obj)
        elif obj is type(NotImplemented):
            return self.save_reduce(type, (NotImplemented,), obj=obj)

        if obj.__module__ == "__main__":
            return self.save_dynamic_class(obj)

        try:
            return Pickler.save_global(self, obj, name=name)
        except Exception:
            if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
                if obj in _BUILTIN_TYPE_NAMES:
                    return self.save_reduce(
                        _builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)

            typ = type(obj)
            if typ is not obj and isinstance(obj, (type, types.ClassType)):
                return self.save_dynamic_class(obj)

            raise 
開發者ID:bentoml,項目名稱:BentoML,代碼行數:32,代碼來源:cloudpickle.py

示例12: save_global

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_global(self, obj, name=None, pack=struct.pack):
        """
        Save a "global".

        The name of this method is somewhat misleading: all types get
        dispatched here.
        """
        if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
            if obj in _BUILTIN_TYPE_NAMES:
                return self.save_reduce(_builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)

        if name is None:
            name = obj.__name__

        modname = getattr(obj, "__module__", None)
        if modname is None:
            try:
                # whichmodule() could fail, see
                # https://bitbucket.org/gutworth/six/issues/63/importing-six-breaks-pickling
                modname = pickle.whichmodule(obj, name)
            except Exception:
                modname = '__main__'

        if modname == '__main__':
            themodule = None
        else:
            __import__(modname)
            themodule = sys.modules[modname]
            self.modules.add(themodule)

        if hasattr(themodule, name) and getattr(themodule, name) is obj:
            return Pickler.save_global(self, obj, name)

        typ = type(obj)
        if typ is not obj and isinstance(obj, (type, types.ClassType)):
            self.save_dynamic_class(obj)
        else:
            raise pickle.PicklingError("Can't pickle %r" % obj) 
開發者ID:pywren,項目名稱:pywren,代碼行數:40,代碼來源:cloudpickle.py

示例13: save_classobj

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_classobj(pickler, obj): #FIXME: enable pickler._byref
    if obj.__module__ == '__main__': #XXX: use _main_module.__name__ everywhere?
        log.info("C1: %s" % obj)
        pickler.save_reduce(ClassType, (obj.__name__, obj.__bases__,
                                        obj.__dict__), obj=obj)
                                       #XXX: or obj.__dict__.copy()), obj=obj) ?
    else:
        log.info("C2: %s" % obj)
        StockPickler.save_global(pickler, obj)
    return 
開發者ID:dagbldr,項目名稱:dagbldr,代碼行數:12,代碼來源:dill.py

示例14: save_numpy_ufunc

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_numpy_ufunc(pickler, obj):
        log.info("Nu: %s" % obj)
        StockPickler.save_global(pickler, obj)
        return
# NOTE: the above 'save' performs like:
#   import copy_reg
#   def udump(f): return f.__name__
#   def uload(name): return getattr(numpy, name)
#   copy_reg.pickle(NumpyUfuncType, udump, uload) 
開發者ID:dagbldr,項目名稱:dagbldr,代碼行數:11,代碼來源:dill.py

示例15: save_type

# 需要導入模塊: from pickle import Pickler [as 別名]
# 或者: from pickle.Pickler import save_global [as 別名]
def save_type(pickler, obj):
    if obj in _typemap:
        log.info("T1: %s" % obj)
        pickler.save_reduce(_load_type, (_typemap[obj],), obj=obj)
    elif obj.__module__ == '__main__':
        try: # use StockPickler for special cases [namedtuple,]
            [getattr(obj, attr) for attr in ('_fields','_asdict',
                                             '_make','_replace')]
            log.info("T6: %s" % obj)
            StockPickler.save_global(pickler, obj)
            return
        except AttributeError: pass
        if issubclass(type(obj), type):
        #   try: # used when pickling the class as code (or the interpreter)
            if is_dill(pickler) and not pickler._byref:
                # thanks to Tom Stepleton pointing out pickler._session unneeded
                log.info("T2: %s" % obj)
                _dict = _dict_from_dictproxy(obj.__dict__)
        #   except: # punt to StockPickler (pickle by class reference)
            else:
                log.info("T5: %s" % obj)
                StockPickler.save_global(pickler, obj)
                return
        else:
            log.info("T3: %s" % obj)
            _dict = obj.__dict__
       #print (_dict)
       #print ("%s\n%s" % (type(obj), obj.__name__))
       #print ("%s\n%s" % (obj.__bases__, obj.__dict__))
        pickler.save_reduce(_create_type, (type(obj), obj.__name__,
                                           obj.__bases__, _dict), obj=obj)
    else:
        log.info("T4: %s" % obj)
       #print (obj.__dict__)
       #print ("%s\n%s" % (type(obj), obj.__name__))
       #print ("%s\n%s" % (obj.__bases__, obj.__dict__))
        StockPickler.save_global(pickler, obj)
    return 
開發者ID:dagbldr,項目名稱:dagbldr,代碼行數:40,代碼來源:dill.py


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