Skip to content

Instantly share code, notes, and snippets.

@dlovell
Created January 7, 2018 20:11
Show Gist options
  • Save dlovell/80963678b1c78a6807dc5ed1a95e35f1 to your computer and use it in GitHub Desktop.
Save dlovell/80963678b1c78a6807dc5ed1a95e35f1 to your computer and use it in GitHub Desktop.
Attach function/module source on import via a decorator
"""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