Skip to content

Instantly share code, notes, and snippets.

@Aran-Fey
Created February 19, 2018 18:14
Show Gist options
  • Select an option

  • Save Aran-Fey/43fa008c3c52091f0ddf764286492b61 to your computer and use it in GitHub Desktop.

Select an option

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.
"""
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