are elements of a list in sequence in list b

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Feb 9 10:20:24 EST 2008


On Sat, 09 Feb 2008 05:44:27 -0800, bearophileHUGS wrote:

> def issubseq(sub, items):
>     """issubseq(sub, items): return true if the sequence 'sub' is a
> contiguous subsequence of the 'items' sequence.
[snip]

A stylistic note for you...

I believe that it is considered an abuse of doctest to write a function 
with 28 lines of code and 19 tests (about two tests per three LOC). Your 
second function (adapted from mine) with 18 tests and only 14 LOC is even 
more abusive.

(At least, *I* consider it an abuse of doctest.)

As I understand it, doctests are not supposed to be used to cover trivial 
and uninteresting cases, and many of your tests were trivial and 
uninteresting. Doc tests are meant to be first and foremost 
*documentation*. For extensive, cover-every-case testing, you should be 
using unit testing.

For example, given that your docstring stated that both arguments to 
issubseq() are sequences, I question the need to have a doctest showing 
issubseq() fail when one of the arguments is not a sequence. In my 
opinion, that *doc* test is redundant (it shows nothing the reader hasn't 
already seen), and should be a unit test.


And another thing... your timing code does this:

from time import clock

Linux, Unix and Macintosh users of your code may hate you for this. As 
the timeit module explains:

    ... on Windows, clock() has microsecond granularity but time()'s
    granularity is 1/60th of a second; on Unix, clock() has 1/100th 
    of a second granularity and time() is much more precise.

If you're timing fast code, it's probably a good idea to follow Tim 
Peters' recommendation and use time() on Unix and clock() on Windows. An 
easy way to do that is by importing timeit.default_timer.



-- 
Steven



More information about the Python-list mailing list