Created
February 19, 2018 18:14
-
-
Save Aran-Fey/43fa008c3c52091f0ddf764286492b61 to your computer and use it in GitHub Desktop.
Auto-detects objects defined in the current module and generates a suitable `__all__`-list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Auto-detects objects defined in the current module and generates a | |
| suitable `__all__`-list. | |
| Must be called at the bottom of the file, after everything has been defined. | |
| """ | |
| import sys | |
| import types | |
| import inspect | |
| def auto_all(include_constants=False, include_instances=False): | |
| frame = inspect.currentframe().f_back | |
| globs = frame.f_globals | |
| del frame | |
| __all__ = [] | |
| module = globs['__name__'] | |
| module = sys.modules[module] | |
| for name, obj in globs.items(): | |
| if name.startswith('_'): | |
| continue | |
| if include_constants and name.isupper(): | |
| __all__.append(name) | |
| continue | |
| mod = inspect.getmodule(obj) | |
| if mod != module: | |
| continue | |
| if (not include_instances and | |
| not isinstance(obj, types.FunctionType) and | |
| not isinstance(obj, type)): | |
| continue | |
| __all__.append(name) | |
| globs['__all__'] = __all__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment