Mocker has one uber-nice feature... it will mock out an object in basically all namespaces, so you can do a .proxy( 'mymodule.this.that' ) and have the resulting mock object replace the target "that" wherever it's been imported. It won't, for instance, pick up references in lists, sets and the like, but for basic namespaces (dictionaries) it works really nicely. The cool thing is, the code to do the replacing is *tiny*:
def global_replace(remove, install): """Replace object 'remove' with object 'install' on all dictionaries.""" for referrer in gc.get_referrers(remove): if (type(referrer) is dict and referrer.get("__mocker_replace__", True)): for key, value in list(referrer.iteritems()): if value is remove: referrer[key] = install
That's just so neat as an approach I felt people should see it.
Pingbacks are closed.