好的,我已经阅读了有关类方法装饰的所有问题,但我的情况不喜欢它们。
def safe_db(foo):
def _inner(*args, **kwargs):
try:
foo(args, kwargs)
return True
except Exception as e:
log.error(e.message)
print e.message
return False
return _inner
class BaseDB(object):
def __init__(self):
self.connection = Connection()
self.db = self.connection.goobi
self.table = None
@safe_db
def create(self, **data):
self.table.insert(data)
def update(self, where, **data):
try:
self.table.update(where, {'$set': data})
return True
except Exception as e:
log.error(e.message)
print e.message
return False
然后我尝试从继承的类用户调用方法创建:
u = User()
u.create(email='i@example.com', password='secrete')
我得到例外:
2012-09-08 18:17:18,230 ERROR [hairs.model.user][worker 2] create() takes exactly 1 argument (2 given)
create() takes exactly 1 argument (2 given)
我无法理解如何装饰 BaseDB 的类方法,或者如何使异常捕获和记录不那么痛苦?