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


Python aspectlib.weave函数代码示例

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


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


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