Skip to content

Instantly share code, notes, and snippets.

@GrahamDumpleton
Created September 2, 2013 23:52
Show Gist options
  • Save GrahamDumpleton/6418300 to your computer and use it in GitHub Desktop.
Save GrahamDumpleton/6418300 to your computer and use it in GitHub Desktop.
Example of a module proxy which selectively overrides behaviour of specific functions.
from __future__ import print_function
import wrapt
import time
import sys
class TimeModuleProxy(wrapt.ObjectProxy):
def __init__(self, module=time):
super(TimeModuleProxy, self).__init__(module)
def sleep(self, seconds):
print('I am going to sleep for %ss.' % seconds)
return self._self_wrapped.sleep(seconds)
sys.modules['wrapt_time'] = TimeModuleProxy()
from wrapt_time import sleep, asctime, tzname
print(tzname)
print(asctime())
sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment