Created
January 11, 2012 01:29
-
-
Save craigcalef/1592398 to your computer and use it in GitHub Desktop.
Wrapping a module
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
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