Last active
February 14, 2016 15:24
-
-
Save shtaxxx/0b31b67e3ac2d02a4487 to your computer and use it in GitHub Desktop.
Test using inspect.currentframe()
This file contains 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
from __future__ import absolute_import | |
from __future__ import print_function | |
import os | |
import sys | |
import inspect | |
import ast | |
import types | |
import textwrap | |
def foo(): | |
a = 100 | |
b = 200 | |
return a + b | |
def bar(): | |
return 0 | |
def hoge(): | |
c = 300 | |
return c + foo() + bar() | |
def thread(method): | |
frame = inspect.currentframe() | |
_locals = frame.f_back.f_locals | |
_globals = frame.f_back.f_globals | |
for key, value in _locals.items(): | |
if isinstance(value, types.FunctionType): | |
print("FunctionType", key) | |
for key, value in _globals.items(): | |
if isinstance(value, types.FunctionType): | |
print("FunctionType", key) | |
source = inspect.getsource(method) | |
source = textwrap.dedent(source) | |
print(source) | |
_ast = ast.parse(source) | |
print(_ast) | |
thread(hoge) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment