任何适用于 Python 的 AOP 支持库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/286958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:49:39  来源:igfitidea点击:

Any AOP support library for Python?

pythonaop

提问by edomaur

I am trying to use some AOP in my Python programming, but I do not have any experience of the various libraries that exist.

我试图在我的 Python 编程中使用一些 AOP,但我对现有的各种库没有任何经验。

So my question are:

所以我的问题是:

What AOP support exists for Python? And what are the advantages of the differents libraries between them?

Python 有哪些 AOP 支持?它们之间的差异库有什么优点?



Edit

编辑

I've found some, but I don't know how they compare:

我找到了一些,但我不知道它们如何比较:

Edit 2

编辑 2

In which context will I use these?

我将在什么情况下使用这些?

I have two applications, written in Python, which have typically methods which compute taxes and other money things. I'd like to be able to write a "skeleton" of a functionality, and customize it at runtime, for example changing the way local taxes are applied (by country, or state, or city, etc.) without having to overload the full stack.

我有两个用 Python 编写的应用程序,它们通常具有计算税收和其他金钱事物的方法。我希望能够编写功能的“骨架”,并在运行时对其进行自定义,例如更改地方税收的应用方式(按国家/地区、州或城市等),而不必超载全栈。

采纳答案by Tim Diels

Edit: I no longer maintain pytilities and it has been unmaintained for years. You may want to consider one of the other answers instead or this list on Wikipedia.

编辑:我不再维护 pytilities 并且多年来一直没有维护。您可能需要考虑其他答案之一或Wikipedia 上的列表

Another AOP library for python would be pytilities(Documentation; svn repo). It is currently the most powerful (as far as I know).

python 的另一个 AOP 库是pytilities( Documentation; svn repo)。它是目前最强大的(据我所知)。

Its features are:

它的特点是:

  • make reusable Aspect classes
  • apply multiple aspects to an instance or class
  • unapply aspects to an instance/class
  • add new attributes to an instance by using an aspect
  • apply advice to all attributes of an instance/class
  • ...
  • 制作可重用的方面类
  • 将多个方面应用于实例或类
  • 不将方面应用于实例/类
  • 使用方面向实例添加新属性
  • 将建议应用于实例/类的所有属性
  • ...

It also has other goodies such as some special descriptors (see the documentation)

它还具有其他优点,例如一些特殊描述符(请参阅文档)

回答by orip

See S.Lott's link about Python decorators for some great examples, and see the defining PEP for decorators.

请参阅 S.Lott 的关于 Python 装饰器的链接以获取一些很好的示例,并参阅定义装饰器的 PEP

Python had AOP since the beginning, it just didn't have an impressive name. In Python 2.4 the decorator syntax was added, which makes applying decorators very nice syntactically.

Python 从一开始就有 AOP,只是它没有一个令人印象深刻的名字。在 Python 2.4 中添加了装饰器语法,这使得在语法上应用装饰器非常好。

Maybe if you want to apply decorators based on rules you would need a library, but if you're willing to mark the relevant functions/methods when you declare them you probably don't.

也许如果你想根据规则应用装饰器,你需要一个库,但如果你愿意在声明相关函数/方法时标记它们,你可能不会。

Here's an example for a simple caching decorator (I wrote it for this question):

这是一个简单的缓存装饰器的示例(我为这个问题写的):

import pickle, functools
def cache(f):
  _cache = {}
  def wrapper(*args, **kwargs):
    key = pickle.dumps((args, kwargs))
    if key not in _cache:
      _cache[key] = f(*args, **kwargs) # call the wrapped function, save in cache
    return _cache[key] # read value from cache
  functools.update_wrapper(wrapper, f) # update wrapper's metadata
  return wrapper

import time
@cache
def foo(n):
  time.sleep(2)
  return n*2

foo(10) # first call with parameter 10, sleeps
foo(10) # returns immediately

回答by Eli Courtwright

In Python, aspect-oriented programming typically consists of dynamically modifying classes and instances at runtime, which is commonly referred to as monkeypatching. In an answer to another AOP question, I summarized some of these use cases for AOP in Python.

在 Python 中,面向方面的编程通常包括在运行时动态修改类和实例,这通常称为monkeypatching。在回答另一个 AOP 问题时,我总结了Python 中 AOP 的一些用例

回答by Arx

Using annotations is not really AOP, because the weaving process is somewhat hard-coded.

使用注解并不是真正的 AOP,因为编织过程有点硬编码。

There are several AOP frameworks in Python (I counted and compared 8 of them, of which Aspyctwas the clear winner).

Python中有几个AOP框架(我统计并比较了其中的8个,其中Aspyct明显的赢家)。

I'm going to publish a paper with my findings on one of the next conferences, including a real-life industry use case.

我将在接下来的一个会议上发表一篇论文,其中包含我的发现,包括一个真实的行业用例。

回答by astrojuanlu

What about the BSD-licensed python-aspectlib?

BSD 许可的python-aspectlib怎么

Implementation status

Weaving functions, methods, instances and classes is completed.

实施状况

编织函数、方法、实例和类就完成了。

回答by S.Lott

I'd start with the Python Decorator Library. Much of that is AOP kind of stuff.

我将从Python Decorator Library 开始。其中大部分是 AOP 类型的东西。