- djangosnippets.org shows many snippets of Python/Django code along with doctests.
- Have a look at partitioning lists example.
- This is what I think a good usage of doctests.
- I'm not sure about using them *instead* of unittests.
- Michael is also not sure
- BTW, I find many of those Python code samples really pretty.
- I couldn't find any with the string 'self' inside.
- Maybe that's why I like them ;-)
def partition(thelist, n):
"""
Break a list into ``n`` pieces. The last list may be larger than the rest if
the list doesn't break cleanly. That is::
>>> l = range(10)
>>> partition(l, 2)
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
>>> partition(l, 3)
[[0, 1, 2], [3, 4, 5], [6, 7, 8, 9]]
>>> partition(l, 4)
[[0, 1], [2, 3], [4, 5], [6, 7, 8, 9]]
>>> partition(l, 5)
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
"""
try:
n = int(n)
thelist = list(thelist)
except (ValueError, TypeError):
return [thelist]
p = len(thelist) / n
return [thelist[p*i:p*(i+1)] for i in range(n - 1)] + [thelist[p*(i+1):]]
No comments:
Post a Comment