Douwe van der Meij: AOP in Python API design
Douwe van der Meij talks about Aspect Oriented Programming in Python API design at PyGrunn.
See the PyGrunn website for more info about this one-day Python conference in Groningen, The Netherlands.
Aspect oriented programming is more often used in Java than Python. I have used Java before, so I tried using this concept in Python now.
The use case was BioBench, which is one of the sponsors of this conference. It is an application for biogas installations. A farmer enters measurements and gets info from this.
One aspect you need for your API is security. Also statistics/logging, serialization. Probably more. The danger is that those aspects are scattered all over your code base and are tangled, interweaved with other aspects.
Aspect Oriented Programming means: separating your concerns. Put each aspect in a separate module. How can you implement this in pure python? Decorators!
What is an aspect:
- pointcuts, a signature of your code, a regular expression (though I don't use it in my Python code)
- join points: point in your code where the pointcut expression is true, so where the advices should be run
- advices: code to run before or after the join point, or around it.
The security code is an 'around' advice. If a condition is true, we execute the main code, otherwise we skip it and raise an exception.
So for example you end up with:
@secure @serialize @statistics @dispatch def api_call(...): ...
It is good to apply this for aspects that are orthogonal to your main code, like (mostly) security.
Audience: in Zope you can use zcml for aspect oriented programming.