Created
September 17, 2017 13:19
-
-
Save amitbhoraniya/42d71c70f690fc20bc41f4f1a5f84bcc to your computer and use it in GitHub Desktop.
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
def get_decorators(cls): | |
target = cls | |
decorators = {} | |
def visit_FunctionDef(node): | |
decorators[node.name] = [] | |
for n in node.decorator_list: | |
name = '' | |
if isinstance(n, ast.Call): | |
name = n.func.attr if isinstance(n.func, ast.Attribute) else n.func.id | |
else: | |
name = n.attr if isinstance(n, ast.Attribute) else n.id | |
decorators[node.name].append(name) | |
node_iter = ast.NodeVisitor() | |
node_iter.visit_FunctionDef = visit_FunctionDef | |
node_iter.visit(ast.parse(inspect.getsource(target))) | |
return decorators |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment