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


Groups > comp.lang.python > #18598

Re: replacing __dict__ with an OrderedDict

From Peter Otten <__peter__@web.de>
Subject Re: replacing __dict__ with an OrderedDict
Date 2012-01-06 12:44 +0100
Organization None
References <hjsit8-j0a.ln1@satorlaser.homedns.org>
Newsgroups comp.lang.python
Message-ID <mailman.4477.1325850270.27778.python-list@python.org> (permalink)

Show all headers | View raw


Ulrich Eckhardt wrote:

> The topic explains pretty much what I'm trying to do under Python
> 2.7[1]. The reason for this is that I want dir(SomeType) to show the
> attributes in the order of their declaration. This in turn should
> hopefully make unittest execute my tests in the order of their
> declaration[2], so that the output becomes more readable and structured,
> just as my test code (hopefully) is.
> 
> I've been toying around with metaclasses, trying to replace __dict__
> directly, or using type() to construct the class, but either I hit
> read-only attributes or the changes seem to be ignored.

Alternatively you can write your own test loader:

$ cat ordered_unittest2.py
import unittest

class Test(unittest.TestCase):
    def test_gamma(self):
        pass
    def test_beta(self):
        pass
    def test_alpha(self):
        pass

class Loader(unittest.TestLoader):
    def getTestCaseNames(self, testCaseClass):
        """Return a sequence of method names found within testCaseClass
        sorted by co_firstlineno.
        """
        def first_lineno(name):
            method = getattr(testCaseClass, name)
            return method.im_func.__code__.co_firstlineno

        function_names = super(Loader, self).getTestCaseNames(testCaseClass)
        function_names.sort(key=first_lineno)
        return function_names


if __name__ == "__main__":
    unittest.main(testLoader=Loader())

$ python2.7 ordered_unittest2.py -v
test_gamma (__main__.Test) ... ok
test_beta (__main__.Test) ... ok
test_alpha (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK
$

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


Thread

replacing __dict__ with an OrderedDict Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-01-06 10:48 +0100
  Re: replacing __dict__ with an OrderedDict Lie Ryan <lie.1296@gmail.com> - 2012-01-06 22:43 +1100
    Re: replacing __dict__ with an OrderedDict Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-01-06 14:36 +0100
      Re: replacing __dict__ with an OrderedDict Lie Ryan <lie.1296@gmail.com> - 2012-01-07 04:01 +1100
        Re: replacing __dict__ with an OrderedDict Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-07 00:45 +0000
      Re: replacing __dict__ with an OrderedDict Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 10:20 -0700
        Re: replacing __dict__ with an OrderedDict Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-07 00:49 +0000
          Re: replacing __dict__ with an OrderedDict Lie Ryan <lie.1296@gmail.com> - 2012-01-07 12:21 +1100
          Re: replacing __dict__ with an OrderedDict Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 18:24 -0700
            Re: replacing __dict__ with an OrderedDict Eelco <hoogendoorn.eelco@gmail.com> - 2012-01-08 14:03 -0800
              Re: replacing __dict__ with an OrderedDict Lie Ryan <lie.1296@gmail.com> - 2012-01-09 23:10 +1100
                Re: replacing __dict__ with an OrderedDict Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-01-09 14:16 +0100
                Re: replacing __dict__ with an OrderedDict Lie Ryan <lie.1296@gmail.com> - 2012-01-10 23:31 +1100
                Re: replacing __dict__ with an OrderedDict Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-01-10 18:21 +0100
        Re: replacing __dict__ with an OrderedDict Roy Smith <roy@panix.com> - 2012-01-09 09:35 -0500
          Re: replacing __dict__ with an OrderedDict Neil Cerutti <neilc@norwich.edu> - 2012-01-09 14:52 +0000
          Re: replacing __dict__ with an OrderedDict Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-01-09 17:59 +0100
            Re: replacing __dict__ with an OrderedDict Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-09 10:30 -0700
            Re: replacing __dict__ with an OrderedDict Roy Smith <roy@panix.com> - 2012-01-09 20:05 -0500
              Re: replacing __dict__ with an OrderedDict Terry Reedy <tjreedy@udel.edu> - 2012-01-09 23:21 -0500
              Re: replacing __dict__ with an OrderedDict Lie Ryan <lie.1296@gmail.com> - 2012-01-10 23:22 +1100
                Re: replacing __dict__ with an OrderedDict Roy Smith <roy@panix.com> - 2012-01-10 09:05 -0500
                Re: replacing __dict__ with an OrderedDict Lie Ryan <lie.1296@gmail.com> - 2012-01-11 02:57 +1100
                Re: replacing __dict__ with an OrderedDict Roy Smith <roy@panix.com> - 2012-01-10 21:47 -0500
                Re: replacing __dict__ with an OrderedDict Tim Wintle <tim.wintle@teamrubber.com> - 2012-01-10 15:45 +0000
            Re: replacing __dict__ with an OrderedDict Lie Ryan <lie.1296@gmail.com> - 2012-01-10 23:46 +1100
      Re: replacing __dict__ with an OrderedDict Lie Ryan <lie.1296@gmail.com> - 2012-01-07 04:42 +1100
  Re: replacing __dict__ with an OrderedDict Peter Otten <__peter__@web.de> - 2012-01-06 12:44 +0100
    Re: replacing __dict__ with an OrderedDict Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-01-06 14:40 +0100
      Re: replacing __dict__ with an OrderedDict Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 09:06 -0700
        Re: replacing __dict__ with an OrderedDict alex23 <wuwei23@gmail.com> - 2012-01-08 20:58 -0800
      Re: replacing __dict__ with an OrderedDict Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 09:07 -0700
      Re: replacing __dict__ with an OrderedDict Arnaud Delobelle <arnodel@gmail.com> - 2012-01-06 21:38 +0000
    Re: replacing __dict__ with an OrderedDict Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-01-18 11:29 +0100
  Re: replacing __dict__ with an OrderedDict Ethan Furman <ethan@stoneleaf.us> - 2012-01-06 06:13 -0800
  Re: replacing __dict__ with an OrderedDict Arnaud Delobelle <arnodel@gmail.com> - 2012-01-06 21:32 +0000

csiph-web