[David MacKay] > Hello, I'm a python-list newbie. I've got a question about doctest; perhaps > a bug report.
As things will turn out, it's just a question. That's common for newbies :-) > I really like doctest, but sometimes doctest gives a failure when the output > looks absolutely fine to me -- indeed, even after I have gone to considerable > effort to make my documentation match the output perfectly. > > http://www.aims.ac.za/~mackay/python/compression/huffman/Huffman3.py > > The above file is an example. > > It's self-contained, so you can plop it into emacs and hit C-cC-c to run the > doctests. One of them fails. > The piece of source code concerned is here: > > >>> c = []; \ > c.append(node(0.5,1,'a')); \ > c.append(node(0.25,2,'b')); \ > c.append(node(0.125,3,'c')); \ > c.append(node(0.125,4,'d')); \ > iterate(c) ; reportcode(c) # doctest: > +NORMALIZE_WHITESPACE, +ELLIPSIS I'd probably write that more like so: >>> c = [node(0.5,1,'a'), node(0.25,2,'b'), node(0.125,3,'c'), node(0.125,4,'d')] >>> iterate(c) >>> reportcode(c) # doctest [etc] When Python doesn't "look clean", it's not Python -- and backslash continuation & semicolons often look like dirt to the experienced Python's eye. > #Symbol Count Codeword > a (0.5) 1 > b (0.25) 01 > c (0.12) 000 > d (0.12) 001 > """ > > And the output is: > > Failed example: > c = []; c.append(node(0.5,1,'a')); > c.append(node(0.25,2,'b')); c.append(node(0.125,3,'c')); > c.append(node(0.125,4,'d')); iterate(c) ; reportcode(c) > # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS > Expected: > #Symbol Count Codeword > a (0.5) 1 > b (0.25) 01 > c (0.12) 000 > d (0.12) 001 > Got: > <__main__.internalnode instance at 0xb7aee76c> Well, there you go, right? There was certainly no <__main__.internalnode instance at 0xb7aee76c> line in the expected output. I don't know whether you _want_ to see that line or not, but the doctest said you don't. That's why the test fails. If you do want to see it, add, e.g., <__main__.internalnode instance at 0x...> as the first line of the expected output. > #Symbol Count Codeword > a (0.5) 1 > b (0.25) 01 > c (0.12) 000 > d (0.12) 001 You have another problem here, alas: whether a "%2.2g" format rounds 0.125 to "0.12" or "0.13" varies across platforms. For example, if you had run this on Windows, you would have seen: c (0.13) 000 d (0.14) 001 for the last two lines. > I have tried numerous tweaks, and am at a loss. I am wondering whether there > is some big in doctest involving the "#" character in the output. > Or maybe I made some silly mistake. I think we're ready to vote on that now ;-) > Any advice appreciated! > > Many thanks again to the authors of doctest, it gives a great feeling > to write code in the way that doctest encourages. :-) You're welcome! -- http://mail.python.org/mailman/listinfo/python-list