-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagHandler.py
More file actions
45 lines (30 loc) · 1.11 KB
/
Copy pathTagHandler.py
File metadata and controls
45 lines (30 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# https://stackoverflow.com/questions/41834530/how-to-make-python-decorators-work-like-a-tag-to-make-function-calls-by-tag
import functools
class TagDecorator(object):
def __init__(self, tagName):
self.tagName = tagName
def __str__(self):
return "<TagDecorator {tagName}>".format(tagName=self.tagName)
def __call__(self, f, *args, **kwargs):
if hasattr(f, "_tags"):
f._tags.append(self.tagName)
else:
f._tags = [self.tagName]
return f
class TagDecoratorClass(object):
def __init__(self, className):
self.className = className
def __call__(self, cls):
cls._tagged = True
taggedFunctions = []
for method in cls.__dict__.values():
if hasattr(method, "_tags"):
taggedFunctions.append(method)
cls.taggedFunctions = taggedFunctions
return cls
@functools.lru_cache(maxsize=None) # memoization
def FunctionTag(tagName):
return TagDecorator(tagName)
@functools.lru_cache(maxsize=None) # memoization
def ClassTag(className):
return TagDecoratorClass(className)