Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52248

Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?"

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 2013-08-09 08:31 +0200
Organization None
References <215331fa-379f-4251-b722-44555349fbb5@googlegroups.com> <ktvl3d$vr9$1@ger.gmane.org> <mailman.343.1375951856.1251.python-list@python.org> <3b1f6c61-da0b-42bd-acf5-95962f6d33d9@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.387.1376029907.1251.python-list@python.org> (permalink)

Show all headers | View raw


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)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is it possible to make a unittest decorator to rename a method from "x" to "testx?" adam.preble@gmail.com - 2013-08-07 23:32 -0700
  Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Terry Reedy <tjreedy@udel.edu> - 2013-08-08 04:04 -0400
    Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" adam.preble@gmail.com - 2013-08-08 09:20 -0700
      Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Terry Reedy <tjreedy@udel.edu> - 2013-08-08 15:28 -0400
  Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Peter Otten <__peter__@web.de> - 2013-08-08 10:32 +0200
  Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Peter Otten <__peter__@web.de> - 2013-08-08 10:50 +0200
    Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" adam.preble@gmail.com - 2013-08-08 09:17 -0700
      Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Ned Batchelder <ned@nedbatchelder.com> - 2013-08-08 13:14 -0400
    Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" adam.preble@gmail.com - 2013-08-08 22:07 -0700
      Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" Peter Otten <__peter__@web.de> - 2013-08-09 08:31 +0200
        Re: Is it possible to make a unittest decorator to rename a method from "x" to "testx?" adam.preble@gmail.com - 2013-08-11 21:25 -0700

csiph-web