wrapping a method function call?
Goldfish
gregturn at mindspring.com
Tue Nov 4 13:15:12 EST 2008
Spring Python provides an AOP solution (http://
springpython.webfactional.com/reference/html/aop.html). You can define
regexp patterns of what you want to intercept.
Imagine having this service:
class SampleService:
def method(self, data):
return "You sent me '%s'" % data
def do_something(self):
return "Okay, I'm doing something"
You can write a simple interceptor that wraps the results:
from springpython.aop import *
class WrappingInterceptor(MethodInterceptor):
"""Interceptor that is called before the real method, and has
access afterwards to the results
def invoke(self, invocation):
print "BEFORE..."
results = "<Wrapped>" + invocation.proceed() + "</Wrapped>"
print "AFTER...."
return results
Simply creating an instance of your base class acts as you would
expect:
service = SampleService()
print service.method("something")
>>> "You sent me 'something'"
Change one line, and your interceptor is plugged in:
service = ProxyFactoryComponent(target = SampleService(), interceptors
= [WrappingInterceptor()])
print service.method("something")
>>> "<Wrapped>You sent me 'something'</Wrapped>"
Visit the website at http://springpython.webfactional.com, and read
about AOP, along with the other features provided by this library.
More information about the Python-list
mailing list