bebraw
1/2/2010 - 4:55 PM

gistfile1.py

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')