auto import all sub modules in a folder then invoke same name functions - python runtime inspect related -


i have open folder on computer,

openfunctions\     ____template.py     function1.py     function2.py     ......     functionx.py 

this folder experimental purpose extend ability of whole app can doing. let’s think quick , dry trying, no security consideration in case.

my purpose is, if drop functionx.py following ____template.py, app can know new functions available , invoke functions defined in new joined file in someway – plugin system, should bit different.

so wrote ____inspect.py may let application have ability know has been inputed.

here

____inspect.py  def get_this_file_defined_functions(name_filter = "__"):     import inspect, sys      f = inspect.getmembers(sys.modules[__name__], inspect.isfunction)      return [x x in f if not x[0].startswith(name_filter)]   def get_this_module_sub_modules(name_filter = "__"):     import os.path, pkgutil     pkgpath = os.path.dirname(__file__)     m = [name _, name, _ in pkgutil.iter_modules([pkgpath])]      return [x x in m if not x[0].startswith(name_filter)]  def import_sub_modules_under_me(auto_exec_function = "auto_exec"):     m = get_this_module_sub_modules()     in m:  # need try except later         exec "global %s; import %s" % (i, i)           #this auto invoke __init__.py if sub modules folder included     in m:         try:             eval(i).eval(auto_exec_function)()         except attributeerror:             print "module %s has no function %s", % (i, auto_exec_function)          else:             print "error on execute %s in module %s", % (auto_exec_function, i)  def execute_all_homonymy_functions(exec_function = "exec"):     m = get_this_module_sub_modules()     in m:         #i need here test if module has been imported         eval(i).eval(exec_function)() 

here

____template.py  def __you_can_not_see_me(): pass  # because filtered str.startswith() def auto_exec(): pass             # auto executed def you_can_get_me(): pass def you_can_get_me1(): pass def you_can_get_me2(): pass 

based on above idea want extend structure below

main.py ____inspect.py openfunctions\     __init__.py     ____template.py     function1.py     function2.py     ......     functionx.py     module_aa         \__init__.py           aa.py           aa1.py 

here main.py while __init__.py may looks like

import ____inspect ____inspect.import_sub_modules_under_me() ____inspect.execute_all_homonymy_functions("what_ever_i_want") 

questions:

  1. above __init__ code not working, because sys.modules[__name__] ____inspect when invoking not openfunctions or module_aa want, there way avoid pass sys.modules[__name__] import_sub_modules_under_me() on main.py or __init__.py?

  2. i suppose execute_all_homonymy_functions() execute same name function in folder no matter exists in sub module or in single file, want invoke , latest version in case module new added or source has been changed runtime. want use code import aa, reload(aa) may thought wrong on below link, suggestions? issue marked i need here test if module has been imported in __inspect.py

    [http://stackoverflow.com/questions/5027352/how-to-test-if-one-python-module-has-been-imported]

  3. i want know return type of 1 function in file before invoking it, suggested attach decorate on each function. plan is:

\n

____decorate.py  def attrs(**kwds):      def decorate(f):          k in kwds:              setattr(f, k, kwds[k])          return f      return decorate  functionx.py import ../____decorate.py  @attrs(argument_types=(int, int,),returns=int) def __you_can_not_see_me(): pass  @attrs(argument_types=(int, int,),returns=int) def auto_exec(): pass             # auto executed  @attrs(argument_types=(int, int,),returns=int) def you_can_get_me(): pass  @attrs(argument_types=(int, int,),returns=int) def you_can_get_me1(): pass  @attrs(argument_types=(int, int,),returns=int) def you_can_get_me2(): pass 

is works ok inspect case? or there’s better solution?

the last one: below code

exec "global %s; import %s" % (i, i)  eval(i).eval(auto_exec_function)() 

looks ugly, alternative above 2 lines?

thanks help.

rgs, kc

to address last question first: dynamically import modules in recent versions of python, use importlib.import_module(), that's for. if you're on older version of python, should use __import__ instead (but check idiosyncracies of direct approach in docs - there's reason import_module() function added replacement).

to address first question: there no officially portable way retrieve information calling function's global environment, correct answer pass __name__ in argument wherever needed.

reloading in python little dangerous, since not works correctly under reloading (even many standard library modules fail if reload either them, or module reference).

i suggest spending time exploring answers existing question plugin architectures in python: building minimal plugin architecture in python


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -