当前位置: 首页>>代码示例>>Python>>正文


Python pickle.whichmodule方法代码示例

本文整理汇总了Python中pickle.whichmodule方法的典型用法代码示例。如果您正苦于以下问题:Python pickle.whichmodule方法的具体用法?Python pickle.whichmodule怎么用?Python pickle.whichmodule使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pickle的用法示例。


在下文中一共展示了pickle.whichmodule方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _whichmodule

# 需要导入模块: import pickle [as 别名]
# 或者: from pickle import whichmodule [as 别名]
def _whichmodule(obj, name):
    """Find the module an object belongs to.

    This function differs from ``pickle.whichmodule`` in two ways:
    - it does not mangle the cases where obj's module is __main__ and obj was
      not found in any module.
    - Errors arising during module introspection are ignored, as those errors
      are considered unwanted side effects.
    """
    module_name = getattr(obj, '__module__', None)
    if module_name is not None:
        return module_name
    # Protect the iteration by using a list copy of sys.modules against dynamic
    # modules that trigger imports of other modules upon calls to getattr.
    for module_name, module in list(sys.modules.items()):
        if module_name == '__main__' or module is None:
            continue
        try:
            if _getattribute(module, name)[0] is obj:
                return module_name
        except Exception:
            pass
    return None 
开发者ID:pywren,项目名称:pywren-ibm-cloud,代码行数:25,代码来源:cloudpickle.py

示例2: GetServiceClassString

# 需要导入模块: import pickle [as 别名]
# 或者: from pickle import whichmodule [as 别名]
def GetServiceClassString(cls, argv = None):
    if argv is None:
        argv = sys.argv
    import pickle
    modName = pickle.whichmodule(cls, cls.__name__)
    if modName == '__main__':
        try:
            fname = win32api.GetFullPathName(argv[0])
            path = os.path.split(fname)[0]
            # Eaaaahhhh - sometimes this will be a short filename, which causes
            # problems with 1.5.1 and the silly filename case rule.
            # Get the long name
            fname = os.path.join(path, win32api.FindFiles(fname)[0][8])
        except win32api.error:
            raise error("Could not resolve the path name '%s' to a full path" % (argv[0]))
        modName = os.path.splitext(fname)[0]
    return modName + "." + cls.__name__ 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:19,代码来源:win32serviceutil.py

示例3: _ufunc_reduce

# 需要导入模块: import pickle [as 别名]
# 或者: from pickle import whichmodule [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

示例4: fullFuncName

# 需要导入模块: import pickle [as 别名]
# 或者: from pickle import whichmodule [as 别名]
def fullFuncName(func):
    qualName = (str(pickle.whichmodule(func, func.__name__)) + '.' + func.__name__)
    if namedObject(qualName) is not func:
        raise Exception("Couldn't find %s as %s." % (func, qualName))
    return qualName 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:7,代码来源:reflect.py

示例5: _whichmodule

# 需要导入模块: import pickle [as 别名]
# 或者: from pickle import whichmodule [as 别名]
def _whichmodule(obj, name):
    """Find the module an object belongs to.

    This function differs from ``pickle.whichmodule`` in two ways:
    - it does not mangle the cases where obj's module is __main__ and obj was
      not found in any module.
    - Errors arising during module introspection are ignored, as those errors
      are considered unwanted side effects.
    """
    if sys.version_info[:2] < (3, 7) and isinstance(obj, typing.TypeVar):  # pragma: no branch  # noqa
        # Workaround bug in old Python versions: prior to Python 3.7,
        # T.__module__ would always be set to "typing" even when the TypeVar T
        # would be defined in a different module.
        #
        # For such older Python versions, we ignore the __module__ attribute of
        # TypeVar instances and instead exhaustively lookup those instances in
        # all currently imported modules.
        module_name = None
    else:
        module_name = getattr(obj, '__module__', None)

    if module_name is not None:
        return module_name
    # Protect the iteration by using a copy of sys.modules against dynamic
    # modules that trigger imports of other modules upon calls to getattr or
    # other threads importing at the same time.
    for module_name, module in sys.modules.copy().items():
        # Some modules such as coverage can inject non-module objects inside
        # sys.modules
        if (
                module_name == '__main__' or
                module is None or
                not isinstance(module, types.ModuleType)
        ):
            continue
        try:
            if _getattribute(module, name)[0] is obj:
                return module_name
        except Exception:
            pass
    return None 
开发者ID:ray-project,项目名称:ray,代码行数:43,代码来源:cloudpickle.py

示例6: save_global

# 需要导入模块: import pickle [as 别名]
# 或者: from pickle import whichmodule [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


注:本文中的pickle.whichmodule方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。