2017-05-05 11 views
1

나는 많은 자손 클래스에서 많은 메소드의 데코레이터 역할을하는 @classmethod을 가진 기본 클래스를가집니다. 나는이 코드를 프로파일 및 트리보기를 통해 볼 때파이썬 cProfile - 프로파일 시각화를 모호하게하는 함수

class BaseClass(): 
    @classmethod 
    def some_decorator(cls, method): 
     @wraps(method) 
     def inner_method(self, *args, **kwargs): 
      # do stuff 
      return method(self, *args, **kwargs) 
     return inner_method 


class ChildClass(BaseClass): 
    @BaseClass.some_decorator 
    def some_child_method(self): 
     # do other stuff 
     return 

, 나는 다른 장소의 수백 some_decorator에 대한 호출의 수천을 참조하십시오.

그리고 나서 some_decorator이 방금 나온 수백 개의 장소로 전화를 겁니다.

코드를 변경하거나 다른 방식으로 프로파일을 적용하는 방법을 찾아 내지 못했습니다. (gprof2dot atm 사용 : How can you get the call tree with python profilers?)

생각 하시겠습니까?

+0

이 장식은 일반적으로 각 장식 기능에 대한 폐쇄를하게 때문에 당신이 업데이트 할 수 없습니다 (또는 사본을) 코드 파일 및 라인 정보를 데코 레이팅 된 함수 위에있는 라인을 참조하도록 변경 하시겠습니까? –

답변

0

나는 완전히 확신 할 수는 없지만, snakeviz 또는 run snake run 시각화 도우미가 작업을 수행하고 올바른 트리를 표시한다고 생각합니다. 너 그거 해봤 니?

0

문서/서명을 보존하기 위해 데코레이터를 만드는 방법이 있습니다. wrapt 라이브러리는 그 목적을 위해 많은 기능을 제공합니다.

https://wrapt.readthedocs.io/en/latest/decorators.html#decorating-class-methods

그것은 이런 식으로 뭔가를 찾고 끝낼 것 :

class BaseClass(): 
    @wrapt.decorator 
    @classmethod 
    def some_decorator(cls, method, instance, *args, *kwargs): 
     # do stuff 
     return method(instance, *args, **kwargs)