AOPython 1.0.2

AOPython is an AOP (Aspect Oriented Programming) module for Python. AOPython provides a base ‘Aspect’ that can ‘advise’ or wrap function and/or method calls. It also contains a weave function that can weave (apply advice to a subset of- or all functions and methods in) a module, class, or instance. Advice may modify or replace parameters and/or the return value of the advised function or method each time it is called. It can do many other things such as handle exceptions raised by an advised function, collect information on the method call (logging), or obtain and release resources before and after a method is invoked.

Download AOPython 1.0.2.

Example 1: How to wrap a function

from aopython import Aspect
def negate(x):
    return -x
aspect = Aspect()
negate = aspect.wrap(negate)
negate(2) # advised function call

Example 2: How to weave a class

from aopython import Aspect
class MyObj(object):
    def double(self, x):
        return x * 2
    def tripple(self, x):
        return x * 3
aspect = Aspect()
myobj = MyObj()
myobj.double(5) # advised method call
MyObj.tripple(myobj, 5) # advised method call

In this release the weave functionality has been enhanced to allow more flexible weaving of callables in modules, classes, or instances. An unweave function has also been added to allow advice to be removed en-mass from an object that was previously weaved. Test coverage was also greatly improved, especially for the weave/unweave functions.

4 Responses to “AOPython 1.0.2”

- Sylvain Hellegouarch

Hi there,

Just a quick note to say great job!

I’ll have a better look soon but really nice job.

– Sylvain

- Shai


It seems like you have some interesting ideas, but could you explain, for the rest of us, how this improves upon Python2.4’s decorators?

- daniel

Sylvain, thanks for the comment. I hope you find it useful.

Shai, thanks for your input as well. I think there are a few things that AOPython can do that are not quite as simple with decorators, but I could be wrong because I haven’t used them.

AOPython can-

1. wrap almost any callable (not just functions). Note that some callables, once wrapped, cannot be reassigned to their original name/location (i.e. list.append is not a writeable attribute). This simply means that the wrapped version must be referred to by a different alias.

2. declaratively weave or wrap any class or function–even ones that are imported–with minimal syntax. I believe decorators must be applied by putting the @deco line before the function declaration.

Note, I wrote AOPython on Python 2.3.4. After trying it out on 2.4 tonight I noticed that the last doctest (complex weave example–weave the re module) does not pass. Guess I still have some work to do. To be fair, I could just remove that test as it isn’t a very good test anyway–it’s more of an elaborate example.

Leave a Reply