~pythonregexp2.7/python/issue2636-18

« back to all changes in this revision

Viewing changes to Lib/test/test_itertools.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-09-21 13:47:31 UTC
  • mfrom: (39021.1.404 Regexp-2.7)
  • Revision ID: darklord@timehorse.com-20080921134731-rudomuzeh1b2tz1y
Merged in changes from the latest python source snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1185
1185
[22]
1186
1186
[25, 26, 27, 28]
1187
1187
 
1188
 
>>> def take(n, seq):
1189
 
...     return list(islice(seq, n))
1190
 
 
1191
 
>>> def enumerate(iterable):
1192
 
...     return izip(count(), iterable)
1193
 
 
1194
 
>>> def tabulate(function):
 
1188
>>> def take(n, iterable):
 
1189
...     "Return first n items of the iterable as a list"
 
1190
...     return list(islice(iterable, n))
 
1191
 
 
1192
>>> def enumerate(iterable, start=0):
 
1193
...     return izip(count(start), iterable)
 
1194
 
 
1195
>>> def tabulate(function, start=0):
1195
1196
...     "Return function(0), function(1), ..."
1196
 
...     return imap(function, count())
1197
 
 
1198
 
>>> def iteritems(mapping):
1199
 
...     return izip(mapping.iterkeys(), mapping.itervalues())
 
1197
...     return imap(function, count(start))
1200
1198
 
1201
1199
>>> def nth(iterable, n):
1202
 
...     "Returns the nth item"
 
1200
...     "Returns the nth item or empty list"
1203
1201
...     return list(islice(iterable, n, n+1))
1204
1202
 
1205
 
>>> def all(seq, pred=None):
1206
 
...     "Returns True if pred(x) is true for every element in the iterable"
1207
 
...     for elem in ifilterfalse(pred, seq):
1208
 
...         return False
1209
 
...     return True
1210
 
 
1211
 
>>> def any(seq, pred=None):
1212
 
...     "Returns True if pred(x) is true for at least one element in the iterable"
1213
 
...     for elem in ifilter(pred, seq):
1214
 
...         return True
1215
 
...     return False
1216
 
 
1217
 
>>> def no(seq, pred=None):
1218
 
...     "Returns True if pred(x) is false for every element in the iterable"
1219
 
...     for elem in ifilter(pred, seq):
1220
 
...         return False
1221
 
...     return True
1222
 
 
1223
 
>>> def quantify(seq, pred=None):
1224
 
...     "Count how many times the predicate is true in the sequence"
1225
 
...     return sum(imap(pred, seq))
1226
 
 
1227
 
>>> def padnone(seq):
 
1203
>>> def quantify(iterable, pred=bool):
 
1204
...     "Count how many times the predicate is true"
 
1205
...     return sum(imap(pred, iterable))
 
1206
 
 
1207
>>> def padnone(iterable):
1228
1208
...     "Returns the sequence elements and then returns None indefinitely"
1229
 
...     return chain(seq, repeat(None))
 
1209
...     return chain(iterable, repeat(None))
1230
1210
 
1231
 
>>> def ncycles(seq, n):
1232
 
...     "Returns the sequence elements n times"
1233
 
...     return chain(*repeat(seq, n))
 
1211
>>> def ncycles(iterable, n):
 
1212
...     "Returns the seqeuence elements n times"
 
1213
...     return chain(*repeat(iterable, n))
1234
1214
 
1235
1215
>>> def dotproduct(vec1, vec2):
1236
1216
...     return sum(imap(operator.mul, vec1, vec2))
1254
1234
...     return izip(a, b)
1255
1235
 
1256
1236
>>> def grouper(n, iterable, fillvalue=None):
1257
 
...     "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
 
1237
...     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
1258
1238
...     args = [iter(iterable)] * n
1259
 
...     kwds = dict(fillvalue=fillvalue)
1260
 
...     return izip_longest(*args, **kwds)
 
1239
...     return izip_longest(fillvalue=fillvalue, *args)
1261
1240
 
1262
1241
>>> def roundrobin(*iterables):
1263
 
...     "roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'"
 
1242
...     "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
1264
1243
...     # Recipe credited to George Sakkis
1265
1244
...     pending = len(iterables)
1266
1245
...     nexts = cycle(iter(it).next for it in iterables)
1280
1259
...         yield set(x for m, x in pairs if m&n)
1281
1260
 
1282
1261
>>> def compress(data, selectors):
1283
 
...     "compress('abcdef', [1,0,1,0,1,1]) --> a c e f"
1284
 
...     for d, s in izip(data, selectors):
1285
 
...         if s:
1286
 
...             yield d
 
1262
...     "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
 
1263
...     return (d for d, s in izip(data, selectors) if s)
 
1264
 
 
1265
>>> def combinations_with_replacement(iterable, r):
 
1266
...     "combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC"
 
1267
...     pool = tuple(iterable)
 
1268
...     n = len(pool)
 
1269
...     indices = [0] * r
 
1270
...     yield tuple(pool[i] for i in indices)
 
1271
...     while 1:
 
1272
...         for i in reversed(range(r)):
 
1273
...             if indices[i] != n - 1:
 
1274
...                 break
 
1275
...         else:
 
1276
...             return
 
1277
...         indices[i:] = [indices[i] + 1] * (r - i)
 
1278
...         yield tuple(pool[i] for i in indices)
1287
1279
 
1288
1280
This is not part of the examples but it tests to make sure the definitions
1289
1281
perform as purported.
1300
1292
>>> nth('abcde', 3)
1301
1293
['d']
1302
1294
 
1303
 
>>> all([2, 4, 6, 8], lambda x: x%2==0)
1304
 
True
1305
 
 
1306
 
>>> all([2, 3, 6, 8], lambda x: x%2==0)
1307
 
False
1308
 
 
1309
 
>>> any([2, 4, 6, 8], lambda x: x%2==0)
1310
 
True
1311
 
 
1312
 
>>> any([1, 3, 5, 9], lambda x: x%2==0,)
1313
 
False
1314
 
 
1315
 
>>> no([1, 3, 5, 9], lambda x: x%2==0)
1316
 
True
1317
 
 
1318
 
>>> no([1, 2, 5, 9], lambda x: x%2==0)
1319
 
False
1320
 
 
1321
1295
>>> quantify(xrange(99), lambda x: x%2==0)
1322
1296
50
1323
1297
 
1362
1336
>>> list(compress('abcdef', [1,0,1,0,1,1]))
1363
1337
['a', 'c', 'e', 'f']
1364
1338
 
 
1339
>>> list(combinations_with_replacement('abc', 2))
 
1340
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'b'), ('b', 'c'), ('c', 'c')]
 
1341
 
1365
1342
"""
1366
1343
 
1367
1344
__test__ = {'libreftest' : libreftest}