• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python aspectlib.weave函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中aspectlib.weave函数的典型用法代码示例。如果您正苦于以下问题:Python weave函数的具体用法?Python weave怎么用?Python weave使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: test_weave_old_style_method_no_warn_patch_module

def test_weave_old_style_method_no_warn_patch_module():
    calls = []
    with aspectlib.weave('warnings.warn', record(calls=calls)):
        with aspectlib.weave('test_aspectlib.LegacyTestClass.foobar', mock('stuff')):
            assert LegacyTestClass().foobar() == 'stuff'

    assert calls == []
开发者ID:ionelmc,项目名称:python-aspectlib,代码行数:7,代码来源:test_aspectlib.py


示例2: enable_aspectlib_maybe

def enable_aspectlib_maybe():                               # pragma: debugging
    """For debugging, we can use aspectlib to trace execution.

    Define COVERAGE_ASPECTLIB to enable and configure aspectlib to trace
    execution::

        $ export COVERAGE_LOG=covaspect.txt
        $ export COVERAGE_ASPECTLIB=coverage.Coverage:coverage.data.CoverageData
        $ coverage run blah.py ...

    This will trace all the public methods on Coverage and CoverageData,
    writing the information to covaspect.txt.

    """
    aspects = os.environ.get("COVERAGE_ASPECTLIB", "")
    if not aspects:
        return

    import aspectlib                            # pylint: disable=import-error
    import aspectlib.debug                      # pylint: disable=import-error

    filename = os.environ.get("COVERAGE_LOG", "/tmp/covlog.txt")
    filters = [add_pid_and_tid, filter_aspectlib_frames]
    aspects_file = DebugOutputFile.the_one(open(filename, "a"), show_process=True, filters=filters)
    aspect_log = aspectlib.debug.log(
        print_to=aspects_file, attributes=['id'], stacktrace=30, use_logging=False
    )
    public_methods = re.compile(r'^(__init__|[a-zA-Z].*)$')
    for aspect in aspects.split(':'):
        aspectlib.weave(aspect, aspect_log, methods=public_methods)
开发者ID:adriankrupa,项目名称:tk-core,代码行数:30,代码来源:debug.py


示例3: test_weave_class_old_style_all_magic

def test_weave_class_old_style_all_magic():
    history = []

    @aspectlib.Aspect
    def aspect(*args):
        history.append(args)
        yield aspectlib.Proceed

    inst = LegacyTestClass()

    with aspectlib.weave(LegacyTestClass, aspect, subclasses=False):
        with aspectlib.weave(LegacyTestSubClass, aspect, subclasses=False):
            with aspectlib.weave(LegacyTestSubSubClass, aspect, subclasses=False):
                inst = LegacyTestClass("stuff")
                assert history == [(inst, "stuff"), (LegacyTestClass, "stuff"), ("stuff",)]
                del history[:]

                inst = LegacyTestSubClass("stuff")
                assert history == [(inst, "stuff"), (LegacyTestSubClass, "stuff"), ("stuff",)]
                del history[:]

                inst = LegacyTestSubSubClass("stuff")
                assert history == [(inst, "stuff"), (LegacyTestSubSubClass, "stuff"), ("stuff",)]
                del history[:]

    inst = LegacyTestClass("stuff")
    inst = LegacyTestSubClass("stuff")
    inst = LegacyTestSubSubClass("stuff")

    assert history == []
开发者ID:nedbat,项目名称:python-aspectlib,代码行数:30,代码来源:test_aspectlib.py


示例4: test_list_of_aspects

def test_list_of_aspects():
    with aspectlib.weave(module_func, [mock('foobar'), record]):
        assert module_func(1, 2, 3) == 'foobar'
        assert module_func.calls == [(None, (1, 2, 3), {})]

    with aspectlib.weave(module_func, [mock('foobar', call=True), record]):
        raises(TypeError, module_func, 1, 2, 3)
        assert module_func.calls == [(None, (1, 2, 3), {})]
开发者ID:ionelmc,项目名称:python-aspectlib,代码行数:8,代码来源:test_aspectlib.py


示例5: test_weave_wrong_module

def test_weave_wrong_module():
    calls = []
    with aspectlib.weave('warnings.warn', record(calls=calls)):
        aspectlib.weave(AliasedGlobal, mock('stuff'), lazy=True)
    assert calls == [
        (None,
         ("Setting test_aspectlib.MissingGlobal to <class 'test_aspectlib.MissingGlobal'>. "
          "There was no previous definition, probably patching the wrong module.",),
         {})
    ]
开发者ID:ionelmc,项目名称:python-aspectlib,代码行数:10,代码来源:test_aspectlib.py


示例6: test_weave_class_meth_no_aliases_unsupported_on_py3

def test_weave_class_meth_no_aliases_unsupported_on_py3():
    with aspectlib.weave(Global.meth, mock("stuff")):
        assert Global().meth() == "stuff"
        assert Global2().meth() == "stuff"

    assert Global().meth() == "base"
    assert Global2().meth() == "base"
开发者ID:nedbat,项目名称:python-aspectlib,代码行数:7,代码来源:test_aspectlib.py


示例7: test_weave_str_target

def test_weave_str_target():
    with aspectlib.weave('test_pkg1.test_pkg2.test_mod.target', mock('foobar')):
        from test_pkg1.test_pkg2.test_mod import target
        assert target() == 'foobar'

    from test_pkg1.test_pkg2.test_mod import target
    assert target() is None
开发者ID:ionelmc,项目名称:python-aspectlib,代码行数:7,代码来源:test_aspectlib.py


示例8: test_weave_str_class_meth_target

def test_weave_str_class_meth_target():
    with aspectlib.weave('test_pkg1.test_pkg2.test_mod.Stuff.meth', mock('foobar')):
        from test_pkg1.test_pkg2.test_mod import Stuff
        assert Stuff().meth() == 'foobar'

    from test_pkg1.test_pkg2.test_mod import Stuff
    assert Stuff().meth() is None
开发者ID:ionelmc,项目名称:python-aspectlib,代码行数:7,代码来源:test_aspectlib.py


示例9: test_weave_multiple

def test_weave_multiple():
    with aspectlib.weave((module_func, module_func2), mock('foobar')):
        assert module_func() == 'foobar'
        assert module_func2() == 'foobar'

    assert module_func() is None
    assert module_func2() is None
开发者ID:ionelmc,项目名称:python-aspectlib,代码行数:7,代码来源:test_aspectlib.py


示例10: test_weave_class_all_magic

def test_weave_class_all_magic():
    history = []

    @aspectlib.Aspect
    def aspect(*args):
        history.append(args)
        yield aspectlib.Proceed

    inst = NormalTestClass()

    with aspectlib.weave(NormalTestClass, aspect, methods=aspectlib.ALL_METHODS):
        inst = NormalTestClass("stuff")
        assert history == [(inst, "stuff"), (inst, "stuff"), (NormalTestClass, "stuff"), ("stuff",)]
        del history[:]

        inst = NormalTestSubClass("stuff")
        assert history == [(inst, "stuff"), (inst, "stuff"), (NormalTestSubClass, "stuff"), ("stuff",)]
        del history[:]

        inst = NormalTestSubSubClass("stuff")
        assert history == [(inst, "stuff"), (inst, "stuff"), (NormalTestSubSubClass, "stuff"), ("stuff",)]
        del history[:]

    inst = NormalTestClass("stuff")
    inst = NormalTestSubClass("stuff")
    inst = NormalTestSubSubClass("stuff")

    assert history == []
开发者ID:nedbat,项目名称:python-aspectlib,代码行数:28,代码来源:test_aspectlib.py


示例11: __enter__

 def __enter__(self):
     self._rollback = weave(
         self._logger,
         record(callback=self._callback, extended=True, iscalled=True),
         methods='_log$'
     )
     return self
开发者ID:nedbat,项目名称:python-aspectlib,代码行数:7,代码来源:test.py


示例12: main

def main(template, target, no_input, checkout, verbose):
    if verbose:
        logging.basicConfig(
            format='%(levelname)s %(filename)s: %(message)s',
            level=logging.DEBUG
        )
    else:
        logging.basicConfig(
            format='%(levelname)s: %(message)s',
            level=logging.INFO
        )

    try:
        src = os.path.join(target, '.cookiecutterrc')
        if os.path.exists(src):
            logger.info("Loading config from %r", src)
            extra_context = get_config(src)
            logger.debug("Loaded %r", extra_context)
            extra_context = extra_context.get('cookiecutter') or extra_context.get('default_context')
            logger.debug("Loaded %r", extra_context)
        else:
            logger.info("No .cookiecutterrc in %r", target)
            extra_context = None

        with weave('cookiecutter.main.generate_files', save_context):
            cookiecutter(
                template, checkout, no_input,
                overwrite_if_exists=True,
                output_dir=os.path.dirname(target),
                extra_context=extra_context,
            )
    except (OutputDirExistsException, InvalidModeException) as e:
        click.echo(e)
        sys.exit(1)
开发者ID:ionelmc,项目名称:python-cookiepatcher,代码行数:34,代码来源:cli.py


示例13: test_simple

def test_simple():
    buf = StringIO()
    with aspectlib.weave(some_meth, aspectlib.debug.log(print_to=buf, module=False, stacktrace=10)):
        some_meth(1, 2, 3, a=4)

    assert re.match(LOG_TEST_SIMPLE, buf.getvalue())
    some_meth(1, 2, 3, a=4)
    assert re.match(LOG_TEST_SIMPLE, buf.getvalue())
开发者ID:nedbat,项目名称:python-aspectlib,代码行数:8,代码来源:test_aspectlib_debug.py


示例14: test_weave_class_meth_no_aliases

def test_weave_class_meth_no_aliases():
    with aspectlib.weave(Global.meth, mock("stuff"), aliases=False, lazy=True):
        assert Global().meth() == "stuff"
        assert Global2 is not Global
        assert Global2().meth() == "base"

    assert Global().meth() == "base"
    assert Global2 is Global
    assert Global2().meth() == "base"
开发者ID:nedbat,项目名称:python-aspectlib,代码行数:9,代码来源:test_aspectlib.py


示例15: test_weave_class_no_aliases

def test_weave_class_no_aliases():
    with aspectlib.weave(Global, mock('stuff'), aliases=False, lazy=True):
        assert Global().meth() == 'stuff'
        assert Global2 is not Global
        assert Global2().meth() == 'base'

    assert Global().meth() == 'base'
    assert Global2 is Global
    assert Global2().meth() == 'base'
开发者ID:ionelmc,项目名称:python-aspectlib,代码行数:9,代码来源:test_aspectlib.py


示例16: test_weave_no_aliases

def test_weave_no_aliases():
    with aspectlib.weave(module_func2, mock('stuff'), aliases=False):
        assert module_func2() == 'stuff'
        assert module_func2 is not module_func3
        assert module_func3() is None

    assert module_func2() is None
    assert module_func3() is None
    assert module_func2 is module_func3
开发者ID:ionelmc,项目名称:python-aspectlib,代码行数:9,代码来源:test_aspectlib.py


示例17: enable_aspectlib_maybe

def enable_aspectlib_maybe():                               # pragma: debugging
    """For debugging, we can use aspectlib to trace execution.

    Define COVERAGE_ASPECTLIB to enable and configure aspectlib to trace
    execution::

        COVERAGE_ASPECTLIB=covaspect.txt:coverage.Coverage:coverage.data.CoverageData program...

    This will trace all the public methods on Coverage and CoverageData,
    writing the information to covaspect.txt.

    """
    aspects = os.environ.get("COVERAGE_ASPECTLIB", "")
    if not aspects:
        return

    import aspectlib                            # pylint: disable=import-error
    import aspectlib.debug                      # pylint: disable=import-error

    class AspectlibOutputFile(object):
        """A file-like object that includes pid and cwd information."""
        def __init__(self, outfile):
            self.outfile = outfile
            self.cwd = None

        def write(self, text):
            """Just like file.write"""
            cwd = os.getcwd()
            if cwd != self.cwd:
                self._write("cwd is now {0!r}\n".format(cwd))
                self.cwd = cwd
            self._write(text)

        def _write(self, text):
            """The raw text-writer, so that we can use it ourselves."""
            self.outfile.write("{0:5d}: {1}".format(os.getpid(), text))

    aspects = aspects.split(':')
    aspects_file = AspectlibOutputFile(open(aspects[0], "a"))
    aspect_log = aspectlib.debug.log(print_to=aspects_file, use_logging=False)
    aspects = aspects[1:]
    public_methods = re.compile(r'^(__init__|[a-zA-Z].*)$')
    for aspect in aspects:
        aspectlib.weave(aspect, aspect_log, methods=public_methods)
开发者ID:nedbat,项目名称:coveragepy,代码行数:44,代码来源:debug.py


示例18: test_invalid_string_target

def test_invalid_string_target():
    raises(SyntaxError, aspectlib.weave, "inva lid", mock(None))
    raises(SyntaxError, aspectlib.weave, "os.inva lid", mock(None))
    raises(SyntaxError, aspectlib.weave, "os.2invalid", mock(None))
    raises(SyntaxError, aspectlib.weave, "os.some,junk", mock(None))
    raises(SyntaxError, aspectlib.weave, "os.some?junk", mock(None))
    raises(SyntaxError, aspectlib.weave, "os.some*junk", mock(None))

    with aspectlib.weave("test_aspectlib._internal", mock("stuff")):
        assert _internal() == "stuff"
开发者ID:nedbat,项目名称:python-aspectlib,代码行数:10,代码来源:test_aspectlib.py


示例19: test_no_stack

def test_no_stack():
    buf = StringIO()
    with aspectlib.weave(MyStuff, aspectlib.debug.log(
        print_to=buf,
        stacktrace=None,
        attributes=('foo', 'bar()')
    ), methods='(?!bar)(?!__.*__$)'):
        MyStuff('bar').stuff()
    print(buf.getvalue())
    assert "{test_aspectlib_debug.MyStuff foo='bar' bar='foo'}.stuff()\n{test_aspectlib_debug.MyStuff foo='bar' bar='foo'}.stuff => bar\n" == buf.getvalue()
开发者ID:nedbat,项目名称:python-aspectlib,代码行数:10,代码来源:test_aspectlib_debug.py


示例20: test_socket_all_methods

def test_socket_all_methods():
    buf = StringIO()
    with aspectlib.weave(
        socket.socket,
        aspectlib.debug.log(print_to=buf, stacktrace=False),
        lazy=True,
        methods=aspectlib.ALL_METHODS
    ):
        s = socket.socket()

    assert "}.__init__ => None" in buf.getvalue()
开发者ID:ionelmc,项目名称:python-aspectlib,代码行数:11,代码来源:test_integrations.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python test.mock函数代码示例发布时间:2022-05-24
下一篇:
Python aslist.aslist函数代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap