Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attributes': 0.05; '"""': 0.07; 'read-only': 0.07; 'unittest': 0.07; 'python': 0.08; '__name__': 0.09; 'readable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'output': 0.10; 'def': 0.13; 'skip:f 30': 0.13; 'class,': 0.15; '"""return': 0.16; '"__main__":': 0.16; 'eckhardt': 0.16; 'name)': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; 'trying': 0.21; 'from:addr:web.de': 0.23; 'skip:m 30': 0.24; 'explains': 0.24; 'hopefully': 0.24; 'tests': 0.25; 'code': 0.25; "i'm": 0.26; 'import': 0.27; 'pass': 0.29; 'ignored.': 0.29; 'class': 0.29; 'topic': 0.30; 'seem': 0.30; 'construct': 0.30; 'ran': 0.30; 'skip:- 70': 0.31; "i've": 0.31; 'changes': 0.32; 'pretty': 0.32; 'header:X-Complaints-To:1': 0.33; 'to:addr:python-list': 0.34; 'test': 0.35; '...': 0.36; 'subject:with': 0.36; 'sequence': 0.37; 'but': 0.37; 'using': 0.38; 'replace': 0.38; 'received:org': 0.38; 'skip:o 20': 0.38; 'either': 0.39; 'should': 0.39; 'hit': 0.40; 'to:addr:python.org': 0.40; 'within': 0.60; 'more': 0.61; 'your': 0.61; 'order': 0.62; 'show': 0.67 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: replacing __dict__ with an OrderedDict Date: Fri, 06 Jan 2012 12:44:21 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a0cb.dip.t-dialin.net 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1325850270 news.xs4all.nl 6932 [2001:888:2000:d::a6]:49897 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18598 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 $