Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'warnings': 0.05; 'be:': 0.09; 'module,': 0.09; 'parameter': 0.09; 'subject:create': 0.09; 'used.': 0.09; 'python': 0.11; 'print': 0.15; '**kwargs)': 0.16; '**kwargs):': 0.16; 'chris,': 0.16; 'decorator': 0.16; 'executed': 0.16; 'func': 0.16; 'helps!': 0.16; 'received:80.91': 0.16; 'received:80.91.229': 0.16; 'received:gmane.org': 0.16; 'received:list': 0.16; 'solution,': 0.16; 'subject: \n ': 0.16; 'though)': 0.16; 'versions': 0.18; 'def': 0.20; 'wrote:': 0.21; 'function': 0.22; 'header:In-Reply-To:1': 0.22; 'header:User- Agent:1': 0.23; 'idea': 0.26; '(the': 0.26; 'pass': 0.28; "i've": 0.28; 'bit': 0.28; 'pm,': 0.28; 'that.': 0.28; '27,': 0.29; 'enhanced': 0.29; 'skip:@ 10': 0.29; 'though.': 0.29; 'code': 0.29; 'class': 0.29; 'question': 0.30; 'raise': 0.30; 'chris': 0.32; 'option': 0.32; 'work,': 0.32; 'suggestion': 0.32; 'could': 0.34; 'thanks': 0.34; 'subject:new': 0.35; 'see,': 0.35; 'actually': 0.35; 'version': 0.36; "i'm": 0.36; 'done': 0.36; 'sure': 0.36; 'header:X-Complaints-To:1': 0.36; 'but': 0.36; 'next': 0.38; 'comment': 0.38; 'received:org': 0.38; 'skip:b 20': 0.39; 'to:addr:python-list': 0.39; 'think': 0.40; 'to:addr:python.org': 0.40; 'mar': 0.61; 'single': 0.61; 'name': 0.61; 'call': 0.62; 'lists,': 0.63; 'more': 0.63; 'fill': 0.65; 'hope': 0.65; 'incorporate': 0.66; '2012': 0.69; 'idea.': 0.72; 'facilities': 0.76; '11:50': 0.84; 'gelonida': 0.84; 'overlooked': 0.84; 'subject:warning': 0.91; 'have.': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Gelonida N Subject: Re: best way to create warning for obsolete functions and call new one Date: Wed, 28 Mar 2012 09:51:52 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: unicorn.dungeon.de User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.28) Gecko/20120313 Lightning/1.0b2 "" In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1332921126 news.xs4all.nl 6955 [2001:888:2000:d::a6]:56873 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:22265 Hi Chris, On 03/26/2012 11:50 PM, Chris Angelico wrote: > On Tue, Mar 27, 2012 at 7:26 AM, Gelonida N wrote: >> One option I though of would be: >> >> def obsolete_func(func): >> def call_old(*args, **kwargs): >> print "func is old psl use new one" >> return func(*args, **kwargs) >> return call_old >> >> and >> >> def get_time(a='high'): >> return a + 'noon' > > That's a reasonable idea. Incorporate Dan's suggestion of using > DeprecationWarning. > Will do that. > You may want to try decorator syntax: > > def was(oldname): > def _(func): > globals()[oldname]=func > return func > return _ > > @was("get_thyme") > def get_time(a='high'): > return a + 'noon' > > That won't raise DeprecationWarning, though. It's a very simple > assignment. The was() internal function could be enhanced to do a bit > more work, but I'm not sure what version of Python you're using and > what introspection facilities you have. But if you're happy with the > old versions coming up with (*args,**kwargs) instead of their > parameter lists, it's not difficult: I'm using python 2.6 and sometimes still 2.5 (the latter is not important for this question though) Good idea about the decorators. I overlooked to see, that if done properly a decorator will not add call time overhead for calling the function with it's new name > > def was(oldname): > def _(func): > def bounce(*args,**kwargs): > # raise DeprecationWarning > return func(*args,**kwargs) > globals()[oldname]=bounce > return func > return _ > > I've never actually used the Python warnings module, but any line of > code you fill in at the comment will be executed any time the old name > is used. In any case, it's still used with the same convenient > decorator. You could even unify multiple functions under a single new > name: > > @was("foo") > @was("bar") > def quux(spam,ham): > return ham.eat() > Now the next step will do write a decorator working for class methods. I think I have a solution, but it would require to pass the class as additional parameter to each decorator. it would be setattr(cls, oldname, finc) > Hope that helps! It does. Thanks again.