Skip to content

Instantly share code, notes, and snippets.

@craigcalef
Created January 11, 2012 01:29
Show Gist options
  • Save craigcalef/1592398 to your computer and use it in GitHub Desktop.
Save craigcalef/1592398 to your computer and use it in GitHub Desktop.
Wrapping a module
import sys, warnings
def WrapMod(mod, deprecated):
"""Return a wrapped object that warns about deprecated accesses"""
deprecated = set(deprecated)
class Wrapper(object):
def __getattr__(self, attr):
if attr in deprecated:
warnings.warn("Property %s is deprecated" % attr)
return getattr(mod, attr)
def __setattr__(self, attr, value):
if attr in deprecated:
warnings.warn("Property %s is deprecated" % attr)
return setattr(mod, attr, value)
return Wrapper()
oldVal = 6*9
newVal = 42
sys.modules[__name__] = WrapMod(sys.modules[__name__],
deprecated = ['oldVal'])
""" Lifted from http://stackoverflow.com/a/922693 """
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment