Python pickle.whichmodule() Examples

The following are code examples for showing how to use pickle.whichmodule(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pywren-ibm-cloud   Author: pywren   File: cloudpickle.py    Apache License 2.0 6 votes vote down vote up
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 
Example 2
Project: ironpython2   Author: IronLanguages   File: win32serviceutil.py    Apache License 2.0 6 votes vote down vote up
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__ 
Example 3
Project: open-recipe   Author: dspray95   File: win32serviceutil.py    The Unlicense 6 votes vote down vote up
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.
            filelist = win32api.FindFiles(fname)
            # win32api.FindFiles will not detect files in a zip or exe. If list is empty,
            # skip the test and hope the file really exists. 
            if len(filelist) != 0:
                # Get the long name
                fname = os.path.join(path, filelist[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__ 
Example 4
Project: ray   Author: ray-project   File: cloudpickle.py    Apache License 2.0 6 votes vote down vote up
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 
Example 5
Project: arkScript   Author: akbchris   File: win32serviceutil.py    MIT License 6 votes vote down vote up
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.
            filelist = win32api.FindFiles(fname)
            # win32api.FindFiles will not detect files in a zip or exe. If list is empty,
            # skip the test and hope the file really exists. 
            if len(filelist) != 0:
                # Get the long name
                fname = os.path.join(path, filelist[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__ 
Example 6
Project: LaserTOF   Author: kyleuckert   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 7
Project: FX-RER-Value-Extraction   Author: tsKenneth   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 8
Project: recruit   Author: Frank-qlu   File: __init__.py    Apache License 2.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 9
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 10
Project: FUTU_Stop_Loss   Author: BigtoC   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 11
Project: MARRtino-2.0   Author: DaniAffCH   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 12
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 13
Project: open-recipe   Author: dspray95   File: reflect.py    The Unlicense 5 votes vote down vote up
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 
Example 14
Project: vnpy_crypto   Author: birforce   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 15
Project: ble5-nrf52-mac   Author: tomasero   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 16
Project: deepjets   Author: deepjets   File: tasksystem.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def save_global(self, obj, name=None, pack=pickle.struct.pack):
        assert obj
        assert id(obj) not in self.memo
        if name is None:
            name = obj.__name__

        module = getattr(obj, "__module__", None)
        if module is None or module == "__main__":
            module = pickle.whichmodule(obj, name)
        if module is None or module == "__main__":
            raise pickle.PicklingError(
                    "Can't pickle %r: module not found: %s" % (obj, module))

        try:
            __import__(module)
            mod = sys.modules[module]
            klass = getattr(mod, name)
        except (ImportError, KeyError, AttributeError):
            raise pickle.PicklingError(
                    "Can't pickle %r: it's not found as %s.%s" % (obj, module, name))
        else:
            if klass is not obj:
                raise pickle.PicklingError(
                        "Can't pickle %r: it's not the same object as %s.%s" % (obj, module, name))

        assert "\n" not in module
        assert "\n" not in name
        self.write(pickle.GLOBAL + module + '\n' + name + '\n')
        self.memoize(obj)

    # Some types in the types modules are not correctly referenced,
    # such as types.FunctionType. This is fixed here. 
Example 17
Project: Computable   Author: ktraunmueller   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 18
Project: poker   Author: surgebiswas   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 19
Project: Safejumper-for-Desktop   Author: proxysh   File: reflect.py    GNU General Public License v2.0 5 votes vote down vote up
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 
Example 20
Project: P3_image_processing   Author: latedude2   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 21
Project: GraphicDesignPatternByPython   Author: Relph1119   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 22
Project: 3dprinteros-client   Author: panasevychol   File: __init__.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 23
Project: 3dprinteros-client   Author: panasevychol   File: __init__.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 24
Project: predictive-maintenance-using-machine-learning   Author: awslabs   File: __init__.py    Apache License 2.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 25
Project: fund   Author: Frank-qlu   File: __init__.py    Apache License 2.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 26
Project: pySINDy   Author: luckystarufo   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 27
Project: weibo_twitter_earnings_analysis   Author: lynings   File: reflect.py    Apache License 2.0 5 votes vote down vote up
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 
Example 28
Project: Programming-for-Non-Technical-Roles-   Author: PacktPublishing   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 29
Project: learn_python3_spider   Author: wistbean   File: reflect.py    MIT License 5 votes vote down vote up
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 
Example 30
Project: linear_neuron   Author: uglyboxer   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 31
Project: facethin   Author: ParkerGod   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 32
Project: islam-buddy   Author: hamir   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 33
Project: mxnet-lambda   Author: awslabs   File: __init__.py    Apache License 2.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 34
Project: Deribit_funding_rate_indicator   Author: Dimasik007   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 35
Project: psychrometric-chart-makeover   Author: buds-lab   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 36
Project: wine-ml-on-aws-lambda   Author: pierreant   File: __init__.py    Apache License 2.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 37
Project: linux-cross-gcc   Author: nmercier   File: __init__.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 38
Project: SignLanguage_ML   Author: mareep-raljodid   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 39
Project: ImageFusion   Author: pfchai   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 40
Project: sarah   Author: ChonchoFronto   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 41
Project: honours_project   Author: JFriel   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 42
Project: honours_project   Author: JFriel   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 43
Project: Blackjack-Tracker   Author: martinabeleda   File: __init__.py    MIT License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 44
Project: server   Author: lowitty   File: reflect.py    MIT License 5 votes vote down vote up
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 
Example 45
Project: server   Author: lowitty   File: reflect.py    MIT License 5 votes vote down vote up
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 
Example 46
Project: Splunking-Crime   Author: nccgroup   File: __init__.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 47
Project: PYPIC   Author: max614   File: __init__.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 48
Project: PYPIC   Author: max614   File: __init__.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 49
Project: PYPIC   Author: max614   File: __init__.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _ufunc_reduce(func):
    from pickle import whichmodule
    name = func.__name__
    return _ufunc_reconstruct, (whichmodule(func, name), name) 
Example 50
Project: python-for-android   Author: kuri65536   File: reflect.py    Apache License 2.0 5 votes vote down vote up
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