Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'modify': 0.04; 'none:': 0.05; 'main()': 0.07; 'wrapper': 0.07; "%s'": 0.09; 'app,': 0.09; 'imported': 0.09; 'mess': 0.09; 'name)': 0.09; 'name):': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.14; 'skip:p 40': 0.15; 'above?': 0.16; 'annotations.': 0.16; 'attrname': 0.16; 'maintainable': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sugar': 0.16; 'syntactic': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'helper': 0.18; 'skip:v 30': 0.20; 'decorator': 0.22; 'latter': 0.22; 'file.': 0.24; 'import': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'this.': 0.28; 'app.': 0.29; 'ret': 0.29; 'subject:some': 0.29; 'function': 0.30; 'classes': 0.30; 'lines': 0.30; 'print': 0.31; "i'd": 0.31; 'third': 0.33; 'to:addr:python-list': 0.35; 'along': 0.35; 'clear': 0.35; 'functions.': 0.35; 'skip:> 10': 0.35; 'skip:d 30': 0.35; 'tasks': 0.35; 'something': 0.35; 'problem.': 0.35; 'skip:o 20': 0.35; 'but': 0.36; 'possible': 0.36; 'subject:: ': 0.37; 'instead': 0.38; 'received:org': 0.38; 'brief': 0.38; 'skip:s 40': 0.38; 'stuff': 0.38; 'skip:p 20': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'subject:with': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'more': 0.62; 'skip:n 10': 0.63; 'email addr:gmail.com': 0.64 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: how to replace some methods with instrumented ones Date: Fri, 26 Jun 2015 19:37:14 +0200 Organization: None References: <1bd45a06-6141-429d-89bc-79b5aa7de0ff@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd81b0.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435340255 news.xs4all.nl 2946 [2001:888:2000:d::a6]:38162 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93200 georgeryoung@gmail.com wrote: > [python 2.7, linux] > I have a python app. I cannot modify the file. But I can import it and > mess with it. I need to perform brief tasks before and after some of the > member functions. > I'd like to do this in as clear and maintainable way as possible (no third > party imports). Here's what I have now(which works): > > from newvbr import * #get all the stuff from the newvbr app. > > def perf_time(tag): # I do a little more than this, but you get the idea. > print >>sys.stderr, datetime.datetime.now().isoformat() + tag > > def perf(tag): > def decorator(fn): > def wrapper(*args): > perf_time('start %s' % tag) > ret = fn(*args) > perf_time('end %s' % tag) > return ret > return wrapper > return decorator > > > I use this on various members of various classes in the newvbr app: > > old_database_snapshot = VerticaSession.database_snapshot > @perf('db_snap') > def new_database_snapshot(self, name): > old_database_snapshot(self, name) > VerticaSession.database_snapshot = new_database_snapshot > > old_object_snapshot = VerticaSession.object_snapshot > @perf('obj_snap') > def new_object_snapshot(self, name): > old_object_snapshot(self, name) > VerticaSession.object_snapshot = new_object_snapshot > > main() #do the usual stuff of the imported app, but with annotations. > > > This is still pretty laborious. How can I make something roughly like: > > super_tagger(VerticaSession.database_snapshot) > > that does all the above? I need to tag a bunch of function members like > this. I don't insist on using decorators, it just looked like that kind > of problem. Remember that @foo def bar(...): ... is just syntactic sugar for def bar(...): ... bar = foo(bar) In your case the latter form is more convenient, so instead of > old_object_snapshot = VerticaSession.object_snapshot > @perf('obj_snap') > def new_object_snapshot(self, name): > old_object_snapshot(self, name) > VerticaSession.object_snapshot = new_object_snapshot use VerticalSession.object_snapshot = perf("obj_snap")(VerticalSession.object_snapshot) If you like you can write a helper function along these lines def wrap_attr(obj, attrname, name=None): if name is None: name = attrname setattr(obj, attrname perf(name)(getattr(obj, attrname))) wrap_attr(VerticalSession, "object_snapshot")