Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any AOP support library for Python?

Tags:

python

aop

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:

  • Aspyct
  • Lightweight AOP for Python

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.

like image 987
edomaur Avatar asked Nov 13 '08 13:11

edomaur


People also ask

What is AOP in Python?

Aspect oriented programming (AOP) is a horizontal programming paradigm, where some type of behavior is applied to several classes that don't share the same vertical, object-oriented inheritance.

Is Python aspect oriented?

Python does not need something like a "language extension" for being able to work in an Aspect Oriented way. That is simply due to the dynamic mechanisms in Python itself. A Google search will yield a couple projects - but despite looking merely like libraries, it is all that is needed in Python.


4 Answers

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
like image 140
orip Avatar answered Oct 19 '22 15:10

orip


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)

like image 36
Tim Diels Avatar answered Oct 19 '22 14:10

Tim Diels


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.

like image 6
Eli Courtwright Avatar answered Oct 19 '22 15:10

Eli Courtwright


What about the BSD-licensed python-aspectlib?

Implementation status

Weaving functions, methods, instances and classes is completed.

like image 3
astrojuanlu Avatar answered Oct 19 '22 14:10

astrojuanlu