Learning to use decorators with classes
Mr SZ
sk8in_zombi at yahoo.com.au
Tue Jun 30 06:34:15 EDT 2009
Hi,
I'm writing an LDAP plugin for my TG2 application. In this I wrote a small class based decorator with args to set up a connection and call the necessary
functionality but I'm having problems with it. Here's my code:
class getConnection(object):
def __init__(self, settings, credentials):
self.settings = settings
self.credentials = credentials
def __call__(self, f):
def wrapped_f(*args, **kw):
...code snipped...
try:
if tls:
connection.start_tls_s()
if anon:
con.simple_bind_s()
else:
con.simple_bind_s( dn, pw )
except ldap.LDAPError, e:
print e.message['info']
else:
kw['conn'] = connection
try:
f(*args, **kw)
except Exception, e:
print 'Exception in Plugin execution: %s' % e
finally:
connection.unbind()
return wrapped_f
class LdapPlugin(Plugin):
...
def __init__(self, **kw):
Plugin.__init__(self)
@getConnection(self._settings, self.__cred__)
def search(self, **kw):
print 'Searching'
...
Here the base class constructor(Plugin) fetches plugin specific settings from the web framework and stores it in self._settings and user info in self.__cred__ . Now when the method is invoked, this is the error I get:
File '.../plugins/ldap/ldap/__init__.py', line 69 in LdapPlugin
@getConnection(self._settings, self.__cred__)
NameError: name 'self' is not defined
I can simply write a method that gets a connection and deal with it but I wanted to know and learn why decorators is not working in this case.
Regards,
SZ
" life isn't heavy enough,it flies away and floats far above action"
Access Yahoo!7 Mail on your mobile. Anytime. Anywhere.
Show me how: http://au.mobile.yahoo.com/mail
More information about the Python-list
mailing list