Skip to content

Instantly share code, notes, and snippets.

@binarymatt
Created February 17, 2012 13:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binarymatt/1853553 to your computer and use it in GitHub Desktop.
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
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')
def test1_f1(*args, **kwargs):
print 'test_f1'
def test1_f2(*args,**kwargs):
print 'test_f2'
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