Created
February 17, 2012 13:53
-
-
Save binarymatt/1853553 to your computer and use it in GitHub Desktop.
load arbitrary functions - note, on the fs test1 and test2 should be in the lib directory
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
import os, os.path | |
import lib | |
import types | |
def init(): | |
#walk lib and import modules | |
module_set = set([os.path.splitext(module)[0] for module in os.listdir('lib') if not module.startswith('__init__')]) | |
module_set = [m for m in module_set] | |
mods = __import__('lib',globals(), locals(), module_set, -1) | |
return mods, module_set | |
def grab_func(f_name, mods, module_set): | |
for m in module_set: | |
mod = getattr(mods, m) | |
print [mod.__dict__.get(a) for a in dir(mod) if isinstance(mod.__dict__.get(a),types.FunctionType)] | |
def load_and_call(f_name): | |
mods, mod_set = init() | |
grab_func(f_name, mods, mod_set) | |
if __name__ == "__main__": | |
load_and_call('test1_f2') |
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
def test1_f1(*args, **kwargs): | |
print 'test_f1' | |
def test1_f2(*args,**kwargs): | |
print 'test_f2' |
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
def test2_f1(*args, **kwargs): | |
print 'test_f1' | |
def test2_f2(*args,**kwargs): | |
print 'test_f2' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment