jeudi 29 novembre 2018

Profiling all class methods

I'm reading the Practical Python Design Patterns and I'm trying to learn the decorator concept. I've stuck in the last example, where I cannot get the logic of the writing a profiler that applies to all methods of a class.

Here is at the example from the book. I didn't rewrite it here because of the copywrite restriction but I hope Google Book's link is sufficient.

The problem is, when I implement the code and apply it on my DoMathStuff class, I get TypeError: 'NoneType' object is not callable. To me, the try/except/else part is unclear and I think there is a typo somewhere but I can tell where.

@profile_all_class_methods
class DoMathStuff(object):
    """docstring for DoMathStuff"""
    def __init__(self, n):
        self.n = n

    def fib(self):
        fPrev, f = 1, 1
        for num in xrange(2, self.n):
            fPrev, f = f, f + fPrev

        return f

    @profiling_decorator
    def fact(self):
        fct = 1
        for num in xrange(1, self.n):
            fct *= num

        return fct


if __name__ == '__main__':
    m = DoMathStuff(10)
    print("Fib = {}, Fact = {}".format(m.fib(), m.fact()))

Aucun commentaire:

Enregistrer un commentaire