Login

Tag "usewithcare"

Snippet List

decorators for creating paramaterized decorators and easy monkeypatching

as with all things related to monkeypatching, the caveat is to use things like these for good, and not for evil. I wrote these decorators because I did not want to rewrite all of [PyAWS](http://github.com/IanLewis/pyaws) -- instead I use these to add some standard/useful methods to the Bag collection that PyAWS uses internally. AN EXAMPLE: class PatchMyMonkey: pass @monkeypatch(PatchMyMonkey) def throwfecesat(self, who="nobody"): print "Throwing Feces At: %s" % who @monkeypatch(PatchMyMonkey) def nicoderm(self, why="no reason"): print "Trying To Quit Because: %s" % why return {'why':str(why)} trampstamp = PatchMyMonkey() trampstamp.throwfecesat(who="another monkey") print trampstamp.nicoderm(why="cigarettes are pricey") A LESS SILLY EXAMPLE: from pyaws import ecs @monkeypatch(ecs.Bag, fname='get') def get(self, param, *args, **kw): return self.__dict__.get(param, *args, **kw) @monkeypatch(ecs.Bag, fname='clearurls') def clearurls(self, *args, **kw): for k, v in self.__dict__.items(): if isinstance(self.__dict__[k], ecs.Bag): self.__dict__[k].clearurls(*args, **kw) if type(v) == type([]): [ii.clearurls() for ii in self.__dict__[k]] if type(v) == type(u''): if self.__dict__[k].count(u'http://') > 0: self.__dict__[k] = "<URL>" (amazon's URLs are extremely long, and can muddle your test/log output, hence that last function.) based on sample code from [here](http://pypi.python.org/pypi/decorator) and [here](http://mail.python.org/pipermail/python-dev/2008-January/076194.html).

  • decorator
  • monkeypatch
  • decoratordecorator
  • functionalprogramming
  • usewithcare
Read More

1 snippet posted so far.