Skip to content

Instantly share code, notes, and snippets.

@amitbhoraniya
Created September 17, 2017 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitbhoraniya/42d71c70f690fc20bc41f4f1a5f84bcc to your computer and use it in GitHub Desktop.
Save amitbhoraniya/42d71c70f690fc20bc41f4f1a5f84bcc to your computer and use it in GitHub Desktop.
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