Created
January 7, 2018 20:11
-
-
Save dlovell/80963678b1c78a6807dc5ed1a95e35f1 to your computer and use it in GitHub Desktop.
Attach function/module source on import via a decorator
This file contains 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
"""Attach function/module source on import via a decorator | |
""" | |
import sys | |
import inspect | |
SOURCE = '_source' | |
def get_source(f): | |
return getattr(f, SOURCE) | |
def set_source(f, source): | |
setattr(f, SOURCE, source) | |
def has_source(f): | |
return hasattr(f, SOURCE) | |
def _attach_source(f): | |
source = inspect.getsource(f) | |
set_source(f, source) | |
def attach_source(f, record_module=True): | |
if record_module: | |
module = sys.modules[f.__module__] | |
if not has_source(module): | |
_attach_source(module) | |
_attach_source(f) | |
return f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment