Any AOP support library for Python?
Asked Answered
T

6

31

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

So my question are:

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


Edit

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

Edit 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.

Theosophy answered 13/11, 2008 at 13:51 Comment(1)
links no longer workEncapsulate
R
7

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.

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

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)

Retinoscopy answered 7/3, 2011 at 13:48 Comment(2)
The link in this answer seems broken.Bartholomeo
Fixed the link (the fixed link will appear after the edit has been peer reviewed)Retinoscopy
K
25

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

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.

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
Kymry answered 13/11, 2008 at 17:39 Comment(9)
Isn't this memoizing with a different name?Snare
Yes it is. I think memoization is a fancy name for a simple concept that can make it harder to understand. I have similar feelings about the term 'AOP'.Kymry
@Kymry You have a similar feeling about 'AOP' because your example only includes one facet of AOP, i.e., quantification. It is missing obliviousness (see citeseerx.ist.psu.edu/viewdoc/…).Dacy
@Raffi nope. I appreciate your distinction but I believe I mention obliviousness in more accessible terms (applying based on rules), and acknowledge that you'll need a framework in that case. My feelings about the overworked terminology in AOP actually evolved from being pretty familiar with it.Kymry
I'm not convinced decoration and AOP are the same thing. IMHO, with AOP, you should be able to add new behaviours to existing code without modyfing the existing code.Willodeanwilloughby
@Willodeanwilloughby In Python you can just patch existing code without modifying it directly. You don't need fancy terms and techniques...Mazurek
@schlamar, AOP isn't about patching existing code without modifying it directly, it's about encapsulating pieces of code that "cross-cut" (i.e. affect) other encapsulation units (e.g. classes) into their own units (generally, "aspects" depending on which tool you use). Decorators in Python, or even monkey-patching, can be a way to achieve this, but this is far from sufficient.Bridle
@Bruno: False - there are many ways of achieving this in Python, and certainly they are sufficient. Since the language allows you to retrieve code objects (not talking about bytecode, but classes,modules and functions) and modify then at run time, most usually prepending and post-pending behaviors, but not limited to that, it is a matter of taste to come with whichever approach one likes in a library/framework.Ebony
@Ebony I was saying using decorators and monkey patching doesn't constitute AOP on their own. Sure, the low-level mechanisms are available out of the box in Python, but Python (without external libraries or processor) doesn't provide you the higher level constructs like you would get in AspectJ for example. This answer certainly isn't an example of AOP, it's an example of code instrumentation. It's missing the "obliviousness" principle (read the paper mentioned in an earlier comment).Bridle
R
7

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.

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

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)

Retinoscopy answered 7/3, 2011 at 13:48 Comment(2)
The link in this answer seems broken.Bartholomeo
Fixed the link (the fixed link will appear after the edit has been peer reviewed)Retinoscopy
H
6

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.

Heshvan answered 13/11, 2008 at 14:11 Comment(0)
B
3

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

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

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

Bounden answered 21/5, 2010 at 19:8 Comment(3)
This is not an answer but a self ad. -1Linotype
It's nice that you compared the libraries - but where is it, the "Aspyct" and why do you recommend it?Ferroconcrete
If you are going to write an answer that advertises a to-be-published paper, you should link it once it is published. Your answer is 13 years old and it does not contain enough information to find the paper, even if I include your profile.Weighbridge
G
3

What about the BSD-licensed python-aspectlib?

Implementation status

Weaving functions, methods, instances and classes is completed.

Glanville answered 15/12, 2015 at 10:59 Comment(2)
This is the only answer which links to an aspect lib which still seems to work with the current Python. Have my upvote.Linotype
Thanks @Nils - and even so it's a bit outdated, but I could not find anything more recent/better maintainedGlanville
S
2

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

Snare answered 13/11, 2008 at 14:5 Comment(3)
No, it's not. Decorating is not AOP (see my comment above).Dacy
@RaffiKhatchadourian: "Above"? The answers can be sorted into a variety of orders. (Active, Oldest, Votes). "Above" doesn't mean anything at all. What are you talking about?Snare
From above: You have a similar feeling about 'AOP' because your example only includes one facet of AOP, i.e., quantification. It is missing obliviousness (see citeseerx.ist.psu.edu/viewdoc/…).Dacy

© 2022 - 2024 — McMap. All rights reserved.