Supply substitute implementation of itertools.(i)zip_longest.

This commit is contained in:
Georg Brandl 2010-08-21 21:54:50 +02:00
parent 043579e468
commit 2887d24710
2 changed files with 27 additions and 8 deletions

View File

@ -12,7 +12,7 @@
import sys
import codecs
import encodings
import re
try:
from types import ClassType
@ -21,10 +21,11 @@ except ImportError:
# Python 3
class_types = (type,)
try:
from itertools import product
except ImportError: # python < 2.6
# this code has been taken from the python documentation
except ImportError: # Python < 2.6
# this code has been taken from the Python itertools documentation
def product(*args, **kwargs):
pools = map(tuple, args) * kwargs.get('repeat', 1)
result = [[]]
@ -34,6 +35,28 @@ except ImportError: # python < 2.6
yield tuple(prod)
try:
from itertools import izip_longest as zip_longest
except ImportError: # Python < 2.6 or >= 3.0
try:
from itertools import zip_longest
except ImportError:
from itertools import izip, repeat, chain
# this code has been taken from the Python itertools documentation
def izip_longest(*args, **kwds):
# izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
fillvalue = kwds.get('fillvalue')
def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
yield counter() # yields the fillvalue, or raises IndexError
fillers = repeat(fillvalue)
iters = [chain(it, sentinel(), fillers) for it in args]
try:
for tup in izip(*iters):
yield tup
except IndexError:
pass
# the ubiquitous "bytes" helper function
if sys.version_info >= (3, 0):
def b(s):

View File

@ -12,12 +12,8 @@
from uuid import uuid4
from operator import itemgetter
from collections import defaultdict
try:
from itertools import izip_longest as zip_longest
except ImportError:
from itertools import zip_longest
from sphinx.util.pycompat import product
from sphinx.util.pycompat import product, zip_longest
# anything below that ratio is considered equal/changed