Last active
April 15, 2022 09:29
-
-
Save toaster-code/74b47142c10bd59c3b116c04bcce3f8a to your computer and use it in GitHub Desktop.
Create a decorator to a counter of class calls and object creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Report(): | |
| """Decorator for counter.""" | |
| COUNTER = dict() | |
| @classmethod | |
| def counter(cls,func): | |
| def wrapper_counter(*args,**kwargs): | |
| # COUNTER['split_array']+=1 | |
| # print(dir(func)) | |
| print(func.__qualname__) | |
| name = func.__qualname__ | |
| x = cls.COUNTER.get(name, None) | |
| if x is None: | |
| cls.COUNTER[name] = 1 | |
| #class | |
| # elif func.__class__.__name__ is not '__main__': | |
| # COUNTER[func.__class__.__name__] += 1 | |
| # print(COUNTER) | |
| else: | |
| cls.COUNTER[name] += 1 | |
| print(cls.COUNTER) | |
| return func(*args,**kwargs) | |
| return wrapper_counter | |
| @classmethod | |
| def summary(cls): | |
| print("Summary:") | |
| print(cls.COUNTER) | |
| if __name__ == "__main__": | |
| @Report.counter | |
| def new_sum(a,b): | |
| return a+b | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment