예제 #1
0
class Person(metaclass=decorateAll(timer(label='**'))):    # 데코레이터 인수 사용
    def __init__(self, name, pay):
        self.name = name
        self.pay = pay
    def giveRaise(self, percent):
        self.pay *= (1.0 + percent)
    def lastName(self):
        return self.name.split()[-1]
예제 #2
0

def decorateAll(decorator):
    def DecoDecorate(aClass):
        for attr, attrval in aClass.__dict__.items():
            if type(attrval) is FunctionType:
                setattr(aClass, attr, decorator(attrval))  # Not __dict__
        return aClass

    return DecoDecorate


#@decorateAll(tracer)                          # Use a class decorator


@decorateAll(timer(label='@@'))
class Person:  # Applies func decorator to methods
    def __init__(self, name, pay):  # Person = decorateAll(..)(Person)
        self.name = name  # Person = DecoDecorate(Person)
        self.pay = pay

    def giveRaise(self, percent):
        self.pay *= (1.0 + percent)

    def lastName(self):
        return self.name.split()[-1]


bob = Person('Bob Smith', 50000)
sue = Person('Sue Jones', 100000)
print(bob.name, sue.name)
    return DecoDecorate

#@decorateAll(tracer)                          # Use a class decorator
#@decorateAll(tracer(timer(label='@@')))  

#@decorateAll(timer(label='@@')(tracer))      # Times applying the tracer!
#@decorateAll(tracer(timer(label='@@')))      # Traces applying the timer!

#@decorateAll(tracer)                          # Traces onCall wrapper, times methods
#@decorateAll(timer(label='@@'))

#@decorateAll(timer(label='@@'))
#@decorateAll(tracer)                          # Times onCall wrapper, traces methods

   
@decorateAll(timer(label='@@'))
@decorateAll(tracer)                       # Times onCall wrapper, traces methods
class Person:                                 # Applies func decorator to methods
    def __init__(self, name, pay):            # Person = decorateAll(..)(Person)
        self.name = name                      # Person = DecoDecorate(Person)
        self.pay  = pay
    def giveRaise(self, percent):
        self.pay *= (1.0 + percent)
    def lastName(self):
        return self.name.split()[-1]

bob = Person('Bob Smith', 50000)
sue = Person('Sue Jones', 100000)
print(bob.name, sue.name)
sue.giveRaise(.10)
print('%.2f' % sue.pay)
예제 #4
0
from types import FunctionType
from decotools import tracer, timer


def decorateAll(decorator):
    def DecoDecorate(aClass):
        for attr, attrval in aClass.__dict__.items():
            if type(attrval) is FunctionType:
                setattr(aClass, attr, decorator(attrval))  # __dict__가 아님
        return aClass

    return DecoDecorate


@decorateAll(timer(label='@@'))  # 클래스 데코레이터 사용
class Person:  # 함수 데코레이터를 메서드에 적용
    def __init__(self, name, pay):  # Person = decorateAll(...)(Person)
        self.name = name  # Person = DecoDecorate(Person)
        self.pay = pay

    def giveRaise(self, percent):
        self.pay *= (1.0 + percent)

    def lastName(self):
        return self.name.split()[-1]


bob = Person('Bob Smith', 50000)
sue = Person('Sue Jones', 100000)
print(bob.name, sue.name)
sue.giveRaise(.10)
예제 #5
0

# py -3 decoall-deco-any.py 
# call 1 to __init__
# call 2 to __init__
# Bob Smith Sue Jones
# call 1 to giveRaise
# 110000.00
# call 1 to lastName
# call 2 to lastName
# Smith Jones


@decorateAll(tracer)                    # 모든 메서드를 tracer로 데코레이션

@decorateAll(timer())                   # 모든 메서드를 timer와 기본 인수로 데코레이션
@decorateAll(timer(label='@@'))         # 동일하지만 데코레이터 인수를 전달


# 만약 타이머를 사용한다면: 메서드당 총 소요 시간 출력

print('-'*40)
print('%.5f' % Person.__init__.alltime)
print('%.5f' % Person.giveRaise.alltime)
print('%.5f' % Person.lastName.alltime)


# py -3 decoall-deco-any2.py 
# @@__init__: 0.00001, 0.00001
# @@__init__: 0.00001, 0.00001
# Bob Smith Sue Jones