飞码网-免费源码博客分享网站

点击这里给我发消息

Python中sys.module方法的用法及相关示例代码总结|-python教程

飞码网-免费源码博客分享网站 爱上飞码网—https://www.codefrees.com— 飞码网-matlab-python-C++ 爱上飞码网—https://www.codefrees.com— 飞码网-免费源码博客分享网站

本篇文章从各大开源网站收集汇编了Python中sys.module方法的经典代码片段示例。如果你正遇到关于sys.module的相关问题,我相信以下的多个示例代码总有一个能够帮到您。当然如果你仅仅是为了学习sys.module的使用方法,那么这里多样的代码示例同样能帮助您充分的理解其在不同方面的应用!

本文中展示了sys.module方法的相关代码,涉及到使用的多个方面,您可以尽情参考以获得自己想要的答案,当然如果代码对您有帮助或者代码存在部分问题,都请在评论区告诉我们,以便我们整理出更多好的代码。

示例1: inline_runsource

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def inline_runsource(self, source, *cmdlineargs):
        """Run a test module in process using ``pytest.main()``.

        This run writes "source" into a temporary file and runs
        ``pytest.main()`` on it, returning a :py:class:`HookRecorder` instance
        for the result.

        :param source: the source code of the test module

        :param cmdlineargs: any extra command line arguments to use

        :return: :py:class:`HookRecorder` instance of the result

        """
        p = self.makepyfile(source)
        values = list(cmdlineargs) + [p]
        return self.inline_run(*values) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:19,代码来源:pytester.py

示例2: getitem

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getitem(self, source, funcname="test_func"):
        """Return the test item for a test function.

        This writes the source to a python file and runs pytest's collection on
        the resulting module, returning the test item for the requested
        function name.

        :param source: the module source

        :param funcname: the name of the test function for which to return a
            test item

        """
        items = self.getitems(source)
        for item in items:
            if item.name == funcname:
                return item
        assert 0, "{!r} item not found in module:\n{}\nitems: {}".format(
            funcname, source, items
        ) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:22,代码来源:pytester.py

示例3: _setWarningRegistryToNone

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def _setWarningRegistryToNone(modules):
    """
    Disable the per-module cache for every module found in C{modules}, typically
    C{sys.modules}.

    @param modules: Dictionary of modules, typically sys.module dict
    """
    for v in list(modules.values()):
        if v is not None:
            try:
                v.__warningregistry__ = None
            except:
                # Don't specify a particular exception type to handle in case
                # some wacky object raises some wacky exception in response to
                # the setattr attempt.
                pass 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:18,代码来源:_synctest.py

示例4: run_module

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def run_module(mod_name, init_globals=None,
                         run_name=None, alter_sys=False):
    """Execute a module's code without importing it

       Returns the resulting top level namespace dictionary
    """
    loader = get_loader(mod_name)
    if loader is None:
        raise ImportError("No module named " + mod_name)
    code = loader.get_code(mod_name)
    if code is None:
        raise ImportError("No code object available for " + mod_name)
    filename = _get_filename(loader, mod_name)
    if run_name is None:
        run_name = mod_name
    return _run_module_code(code, init_globals, run_name,
                            filename, loader, alter_sys) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:19,代码来源:runpy.py

示例5: getitems

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getitems(self, source):
        """Return all test items collected from the module.

        This writes the source to a python file and runs pytest's collection on
        the resulting module, returning all test items contained within.

        """
        modcol = self.getmodulecol(source)
        return self.genitems([modcol]) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:11,代码来源:pytester.py

示例6: getmodulecol

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getmodulecol(self, source, configargs=(), withinit=False):
        """Return the module collection node for ``source``.

        This writes ``source`` to a file using :py:meth:`makepyfile` and then
        runs the pytest collection on it, returning the collection node for the
        test module.

        :param source: the source code of the module to collect

        :param configargs: any extra arguments to pass to
            :py:meth:`parseconfigure`

        :param withinit: whether to also write an ``__init__.py`` file to the
            same directory to ensure it is a package

        """
        if isinstance(source, Path):
            path = self.tmpdir.join(str(source))
            assert not withinit, "not supported for paths"
        else:
            kw = {self.request.function.__name__: Source(source).strip()}
            path = self.makepyfile(**kw)
        if withinit:
            self.makepyfile(__init__="#")
        self.config = config = self.parseconfigure(path, *configargs)
        return self.getnode(config, path) 
开发者ID:sofia-netsurv,项目名称:python-netsurv,代码行数:28,代码来源:pytester.py

示例7: _collectWarnings

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def _collectWarnings(observeWarning, f, *args, **kwargs):
    """
    Call C{f} with C{args} positional arguments and C{kwargs} keyword arguments
    and collect all warnings which are emitted as a result in a list.

    @param observeWarning: A callable which will be invoked with a L{_Warning}
        instance each time a warning is emitted.

    @return: The return value of C{f(*args, **kwargs)}.
    """
    def showWarning(message, category, filename, lineno, file=None, line=None):
        assert isinstance(message, Warning)
        observeWarning(_Warning(
                str(message), category, filename, lineno))

    # Disable the per-module cache for every module otherwise if the warning
    # which the caller is expecting us to collect was already emitted it won't
    # be re-emitted by the call to f which happens below.
    _setWarningRegistryToNone(sys.modules)

    origFilters = warnings.filters[:]
    origShow = warnings.showwarning
    warnings.simplefilter('always')
    try:
        warnings.showwarning = showWarning
        result = f(*args, **kwargs)
    finally:
        warnings.filters[:] = origFilters
        warnings.showwarning = origShow
    return result 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:32,代码来源:_synctest.py

示例8: getSkip

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getSkip(self):
        """
        Return the skip reason set on this test, if any is set. Checks on the
        instance first, then the class, then the module, then packages. As
        soon as it finds something with a C{skip} attribute, returns that.
        Returns L{None} if it cannot find anything. See L{TestCase} docstring
        for more details.
        """
        return util.acquireAttribute(self._parents, 'skip', None) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:11,代码来源:_synctest.py

示例9: getTodo

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getTodo(self):
        """
        Return a L{Todo} object if the test is marked todo. Checks on the
        instance first, then the class, then the module, then packages. As
        soon as it finds something with a C{todo} attribute, returns that.
        Returns L{None} if it cannot find anything. See L{TestCase} docstring
        for more details.
        """
        todo = util.acquireAttribute(self._parents, 'todo', None)
        if todo is None:
            return None
        return makeTodo(todo) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:14,代码来源:_synctest.py

示例10: _getSuppress

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def _getSuppress(self):
        """
        Returns any warning suppressions set for this test. Checks on the
        instance first, then the class, then the module, then packages. As
        soon as it finds something with a C{suppress} attribute, returns that.
        Returns any empty list (i.e. suppress no warnings) if it cannot find
        anything. See L{TestCase} docstring for more details.
        """
        return util.acquireAttribute(self._parents, 'suppress', []) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:11,代码来源:_synctest.py

示例11: getmodulecol

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def getmodulecol(self, source, configargs=(), withinit=False):
        """Return the module collection node for ``source``.

        This writes ``source`` to a file using :py:meth:`makepyfile` and then
        runs the pytest collection on it, returning the collection node for the
        test module.

        :param source: the source code of the module to collect

        :param configargs: any extra arguments to pass to
            :py:meth:`parseconfigure`

        :param withinit: whether to also write an ``__init__.py`` file to the
            same directory to ensure it is a package

        """
        if isinstance(source, Path):
            path = self.tmpdir.join(str(source))
            assert not withinit, "not supported for paths"
        else:
            kw = {self._name: Source(source).strip()}
            path = self.makepyfile(**kw)
        if withinit:
            self.makepyfile(__init__="#")
        self.config = config = self.parseconfigure(path, *configargs)
        return self.getnode(config, path) 
开发者ID:pytest-dev,项目名称:pytest,代码行数:28,代码来源:pytester.py

示例12: setUpClass

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def setUpClass(cls_, module=module):
            cls_._save_sys_modules = sys.modules.copy()
            sys.modules[TESTS] = module
            sys.modules['datetime'] = module.datetime_module
            sys.modules['_strptime'] = module._strptime 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:7,代码来源:test_datetime.py

示例13: exec_module

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def exec_module(module):
        if module.__name__ == SUBMOD_NAME:
            raise ImportError('I cannot be loaded!') 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:5,代码来源:test_api.py

示例14: test_name_requires_rparition

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def test_name_requires_rparition(self):
        # Raise TypeError if a non-string is passed in for the module name.
        with self.assertRaises(TypeError):
            self.__import__(42) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:6,代码来源:test_api.py

示例15: test_negative_level

# 需要导入模块: import sys [as 别名]
# 或者: from sys import module [as 别名]
def test_negative_level(self):
        # Raise ValueError when a negative level is specified.
        # PEP 328 did away with sys.module None entries and the ambiguity of
        # absolute/relative imports.
        with self.assertRaises(ValueError):
            self.__import__('os', globals(), level=-1) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:8,代码来源:test_api.py


注:本文中sys.module方法的所有代码片段均来自于网络,收集整理于各大开源平台,代码拥有权归各大开源平台的作者所拥有,在此所提供的代码仅供大家学习参考。若有其它用途,请联系相关作者!

飞码网-免费源码博客分享网站 爱上飞码网—https://www.codefrees.com— 飞码网-matlab-python-C++ 爱上飞码网—https://www.codefrees.com— 飞码网-免费源码博客分享网站
赞 ()

相关推荐

内容页底部广告位3
留言与评论(共有 0 条评论)
   
验证码: