Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > replacing __dict__ with an OrderedDict

Reply
Thread Tools

replacing __dict__ with an OrderedDict

 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      01-06-2012
Hi!

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.

As additional thought (but I'm not there yet), I guess that if I want
dir(some_object) to be ordered similarly, I will also have to perform
some steps on instance creation, not only on class creation, right?

Thanks for any suggestions!

Uli


[1] I'm stuck with 2.x for now, from what I found it would be easier
using 3.x.

[2] No, don't tell me that the tests should run in any order. It's not
the case that my tests depend on each other, but if one basic test for
the presence of an API fails, the elaborate tests using that API will
obviously fail, too, and I just want to take the first test that fails
and analyse that instead of guessing the point to start debugging from
the N failed tests.
 
Reply With Quote
 
 
 
 
Lie Ryan
Guest
Posts: n/a
 
      01-06-2012
On 01/06/2012 08:48 PM, Ulrich Eckhardt wrote:
> Hi!
>
> 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.


IMO that's a futile effort, first, because as you already know, the test
should not rely on the order. If you want the result to be printed in a
certain order, that's a display issue, not testing order issue. I guess
you would have better luck doing what you want by customizing the
TestResult or TestRunner.

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-06-2012
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
$


 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      01-06-2012
Am 06.01.2012 12:43, schrieb Lie Ryan:
> On 01/06/2012 08:48 PM, Ulrich Eckhardt wrote:
>> Hi!
>>
>> 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.

>
> IMO that's a futile effort, first, because as you already know, the test
> should not rely on the order. If you want the result to be printed in a
> certain order, that's a display issue, not testing order issue.


I'm not sure if you just -ahem- enjoy Usenet discussions, but please
read the footnote that explains that I don't want to discuss this part
and why you are actually wrong with your partial understanding of the
issue. ;^)


> I guess you would have better luck doing what you want by customizing
> the TestResult or TestRunner.


True, perhaps, but doing it this way would be more fun and easier
reusable in other cases where the default order is not desirable. I can
also go and name the test functions test_000 to test_009 to get results
quickly, if that was the only goal.

Cheers!

Uli
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      01-06-2012
Am 06.01.2012 12:44, schrieb Peter Otten:
> Alternatively you can write your own test loader:

[...CODE...]

Well, actually you just did that for me and it works!


Nonetheless, I'm still wondering if I could somehow replace the dict
with an OrderedDict.

Thank you!

Uli
 
Reply With Quote
 
Ethan Furman
Guest
Posts: n/a
 
      01-06-2012
Ulrich Eckhardt wrote:
> Hi!
>
> 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 believe unittest executes tests in alphabetical order, but it does not
display them in the same order when it prints error/fail results.

~Ethan~
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      01-06-2012
On Fri, Jan 6, 2012 at 6:40 AM, Ulrich Eckhardt
<(E-Mail Removed)> wrote:
> Nonetheless, I'm still wondering if I could somehow replace the dict with an
> OrderedDict.


In Python 3, yes. This is pretty much the entire use case for the new
__prepare__ method of metaclasses. See the "OrderedClass" example at:
http://docs.python.org/py3k/referenc...l-method-names

Cheers,
Ian
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      01-06-2012
On Fri, Jan 6, 2012 at 9:06 AM, Ian Kelly <(E-Mail Removed)> wrote:
> On Fri, Jan 6, 2012 at 6:40 AM, Ulrich Eckhardt
> <(E-Mail Removed)> wrote:
>> Nonetheless, I'm still wondering if I could somehow replace the dict with an
>> OrderedDict.

>
> In Python 3, yes. *This is pretty much the entire use case for the new
> __prepare__ method of metaclasses. *See the "OrderedClass" example at:
> http://docs.python.org/py3k/referenc...l-method-names


That link should be:
http://docs.python.org/py3k/referenc...class-creation
 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      01-06-2012
On 01/07/2012 12:36 AM, Ulrich Eckhardt wrote:
> Am 06.01.2012 12:43, schrieb Lie Ryan:
>> On 01/06/2012 08:48 PM, Ulrich Eckhardt wrote:
>>> Hi!
>>>
>>> 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.

>>
>> IMO that's a futile effort, first, because as you already know, the test
>> should not rely on the order. If you want the result to be printed in a
>> certain order, that's a display issue, not testing order issue.

>
> I'm not sure if you just -ahem- enjoy Usenet discussions, but please
> read the footnote that explains that I don't want to discuss this part
> and why you are actually wrong with your partial understanding of the
> issue. ;^)


I fully understand your issue, and I stand by my opinion. I believe
you're misunderstanding the nature of your problem, your issue is not
that you want a customized test order execution, but you want a
customized view of the test result.

That unittest executes its tests in alphabetical order is implementation
detail for a very good reason, and good unittest practice dictates that
execution order should never be defined (some even argued that the
execution order should be randomized). If the test runner turns out to
execute tests concurrently, that should not cause problems for a
well-designed test. Displaying the test results in a more convenient
order for viewing is what you really wanted in 99.99% of the cases.

> > I guess you would have better luck doing what you want by customizing
> > the TestResult or TestRunner.

>
> True, perhaps, but doing it this way would be more fun and easier
> reusable in other cases where the default order is not desirable. I can
> also go and name the test functions test_000 to test_009 to get results
> quickly, if that was the only goal.


Fun and easier, perhaps. Except that it solves the wrong problem.

 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      01-06-2012
On Fri, Jan 6, 2012 at 10:01 AM, Lie Ryan <(E-Mail Removed)> wrote:
> That unittest executes its tests in alphabetical order is implementation
> detail for a very good reason, and good unittest practice dictates that
> execution order should never be defined (some even argued that the execution
> order should be randomized). If the test runner turns out to execute tests
> concurrently, that should not cause problems for a well-designed test.
> Displaying the test results in a more convenient order for viewing is what
> you really wanted in 99.99% of the cases.


Randomizing the order is not a bad idea, but you also need to be able
to run the tests in a consistent order, from a specific random seed.
In the real world, test conflicts and dependencies do happen, and if
we observe a failure, make a change, rerun the tests and observe
success, we need to be able to be sure that we actually fixed the bug,
and that it didn't pass only because it was run in a different order.

Concurrent testing is a bad idea for this reason -- it's not
repeatable (testing concurrency, OTOH, is a perfectly fine thing to be
thinking about).

Cheers,
Ian
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using an OrderedDict for __dict__ in Python 3 using __prepare__ Steven D'Aprano Python 0 01-09-2012 02:21 AM
Python 3: Plist as OrderedDict Gnarlodious Python 6 02-09-2010 04:07 PM
FYI: ConfigParser, ordered options, PEP 372 and OrderedDict + bigthank you Jonathan Fine Python 0 11-17-2009 04:59 PM
When is a __dict__ not a __dict__? Derek Fountain Python 1 04-21-2004 10:31 AM
__slots__ replacing __dict__ function anabell@sh163.net Python 1 11-06-2003 09:16 AM



Advertisments