How to decorate class method

OK, I allready have read all questions about class method decorating, but my case not like them.

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

And then I try call method create from inherited class User:

u = User()
u.create(email='i@example.com', password='secrete')

I get exception:

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)

I cant understand how I can decorate class methods of BaseDB, or how I can make exceptions catch and logging less painfull ?

Answers


You have an error in your decorator:

def safe_db(foo):
    def _inner(*args, **kwargs):
        try:
            foo(*args, **kwargs)   # fixed line
            return True
        except Exception as e:
            log.error(e.message)
            print e.message
            return False
    return _inner

Need Your Help

CSS website backgroud

html css

I am looking for some info on CSS style used for many websites these days. Its a style where whole screen is occupied by one background and the websites content is posted on top of it under smaller

How to install XNA in Visual Studio 2015 (Preview)

xna visual-studio-2015

How can I install the templates and the the framework for XNA in Visual Studio 2015?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.