Created
January 2, 2010 16:55
-
-
Save bebraw/267556 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
import imp | |
import inspect | |
import os | |
import tempfile | |
content = 'def func_name(a, b, c): pass' | |
# generate tmp file containing py code to load | |
# http://docs.python.org/library/tempfile.html#tempfile.mktemp | |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.py') | |
temp_file.write(content) | |
temp_file.close() | |
try: | |
module = imp.load_source('', temp_file.name) | |
except Exception, e: | |
print e | |
# inspect module now | |
module_functions = inspect.getmembers(module, inspect.isfunction) # not tested | |
# get the func | |
... | |
# get rid of the files generated | |
os.unlink(temp_file.name) | |
os.unlink(temp_file.name + 'c') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment