Wednesday, 15 August 2012

Python: wildcard subset import -


All of the modules are described using import is a bad idea, though, what Is there a way to import subset content from module using a wildcard?

For example:

MODULE_VAR1 = "Hello" MODULE_VAR2 = "the lazy" MODULE_VAR3 = "" "MODULE_VAR4 =" Quick "MODULE_VAR5 =" brown "... MODULE_VAR10 =" "MODULE_VAR11 =" dog! "MODULE_VAR12 =" now "MODULE_VAR13 =" is "... MODULE_VAR98 =" was "MODULE_VAR99 =" all "MODULE_VAR100 =" people! "Def abs (): print" absolutely useful function:% s " % MODULE_VAR1

of course we need to override the would like not to use because we want abs function module imports. But assume that we all replace the MODULE_VAR * variable If you want to be accessible.

Just import module MODULE_VAR * and is put. Is there any way to help him?

I have used 100 variables as an example, because doing module import MODULE_VAR1, MODULE_VAR2, MODULE_VAR3, ..., MODULE_VAR100 to be clearly unreliable and if more will not work in variables (e.g. MODULE_VAR101 ) were added.

If you have a helper function can be done without it - and it's magic:

  import ref define_import (patterns, modules, global): for the key, value in module. Item (): If re.findall for example:  
  Import import import constants match_portport ("MODULE_VAR. *", Constants, globals ())   

Clearly the frame introspection spell is avoided, which is generally considered harmful.

P>

No comments:

Post a Comment