Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'odd': 0.07; 'prefix': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'python': 0.11; 'def': 0.12; 'attr': 0.16; 'bases,': 0.16; 'buggy': 0.16; 'false):': 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; 'subject:make': 0.16; 'subject:possible': 0.16; 'thursday,': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; "i'm": 0.30; 'code': 0.31; 'assert': 0.31; 'fixing': 0.31; 'class': 0.32; 'actual': 0.34; 'skip:_ 10': 0.34; 'subject:from': 0.34; 'to:addr :python-list': 0.38; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'august': 0.61; 'email addr:gmail.com': 0.63; 'name': 0.63; 'myself': 0.63; 'skip:n 10': 0.64; 'here': 0.66; 'glad': 0.83; '2.7.': 0.84; 'otten': 0.84; 'worth,': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Date: Fri, 09 Aug 2013 08:31:43 +0200 Organization: None References: <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> <3b1f6c61-da0b-42bd-acf5-95962f6d33d9@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084bd43.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376029907 news.xs4all.nl 15984 [2001:888:2000:d::a6]:46412 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52248 adam.preble@gmail.com wrote: > On Thursday, August 8, 2013 3:50:47 AM UTC-5, Peter Otten wrote: >> Peter Otten wrote: >> Oops, that's an odd class name. Fixing the name clash in Types.__new__() >> is >> >> left as an exercise... > > Interesting, I got __main__.T, even though I pretty much just tried your > code wholesale. I see I have to fix it myself then... > For what it's worth, I'm using Python 2.7. I'm glad to > see that code since I learned a lot of tricks from it. [My buggy code] > class Type(type): > def __new__(class_, name, bases, classdict): Here 'name' is the class name > newclassdict = {} > for name, attr in classdict.items(): > if getattr(attr, "test", False): > assert not name.startswith(PREFIX) > name = PREFIX + name > assert name not in newclassdict > newclassdict[name] = attr Here 'name' is the the last key of classdict which is passed to type.__new__ instead of the actual class name. > return type.__new__(class_, name, bases, newclassdict) [Fixed version] class Type(type): def __new__(class_, classname, bases, classdict): newclassdict = {} for name, attr in classdict.items(): if getattr(attr, "test", False): assert not name.startswith(PREFIX) name = PREFIX + name assert name not in newclassdict newclassdict[name] = attr return type.__new__(class_, classname, bases, newclassdict)