Skip to content

Instantly share code, notes, and snippets.

@mpdehaan
Created November 11, 2018 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpdehaan/f8c85b0af733598f1d3d36ca09128ce5 to your computer and use it in GitHub Desktop.
Save mpdehaan/f8c85b0af733598f1d3d36ca09128ce5 to your computer and use it in GitHub Desktop.
python 3 import magic
# is there a better way to write all the code in magic_imports.py to re-surface a lot of deeply nested
# symbols at the top level?
# ======
# magic_imports.py
# this is just an example but there could be 20 different classes here
# the purpose is to import all the symbols so any file that uses them doesn't have to deal with the full path
from long.stupid.path import Glorp as _Glorp
from long.other.stupid.path import Blarg as _Blarg
# ...
Glorp = _Glorp
Blarg = _Blarg
# ...
# =======
# other.py
from magic_imports import *
Glorp()
@mpdehaan
Copy link
Author

mpdehaan commented Nov 11, 2018

this doesn't work...but is maybe close-ish

import importlib  
__all__ = []
def re_export(package, symbol):
    m1 = importlib.import_module(package)
    c1 = getattr(m1, symbol)
        __all__.append(c1)

re_export('opsmop.core.policy', 'Policy')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment