Python: wildcard subset import
Question: We have all been told using `from module import *` is a bad idea. However, is
there a way to import a **subset** of the contents of `module` using a
wildcard?

For example:

module.py:

    
    
    MODULE_VAR1 = "hello"
    MODULE_VAR2 = "world"
    MODULE_VAR3 = "The"
    MODULE_VAR4 = "quick"
    MODULE_VAR5 = "brown"
    ...
    MODULE_VAR10 = "lazy"
    MODULE_VAR11 = "dog!"
    MODULE_VAR12 = "Now"
    MODULE_VAR13 = "is"
    ...
    MODULE_VAR98 = "Thats"
    MODULE_VAR99 = "all"
    MODULE_VAR100 = "folks!"
    
    def abs():
        print "Absolutely useful function: %s" % MODULE_VAR1
    

Obviously we don't want to use `from module import *` because we'd be
overriding the `abs` function. But suppose we DID want all of the
`MODULE_VAR*` variables to be accessible locally.

Simply putting `from module import MODULE_VAR*` doesn't work. Is there a way
to accomplish this?

I used 100 variables as an illustration, because doing `from module import
MODULE_VAR1, MODULE_VAR2, MODULE_VAR3, ..., MODULE_VAR100` would obviously be
incredibly unwieldy and wouldn't work if more variables (e.g. `MODULE_VAR101`)
were added.


Answer: You can have a helper function for that - and it can be done without magic:

    
    
    import re
    
    def matching_import(pattern, module, globals):
        for key, value in module.__dict__.items():
            if re.findall(pattern, key):
                globals[key] = value
    

Now, you can do for example:

    
    
    from utils import matching_import
    import constants
    
    matching_import("MODULE_VAR.*", constants, globals())
    

Using `globals()` explicitly in this way avoids frame introspection magic,
which is usually considered harmful.

