File: itertools.py

package info (click to toggle)
py-postgresql 1.2.1%2Bgit20180803.ef7b9a9-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: python: 18,317; ansic: 2,024; sql: 282; sh: 26; makefile: 22
file content (48 lines) | stat: -rw-r--r-- 924 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
##
# .python.itertools
##
"""
itertools extensions
"""
import collections
from itertools import cycle, islice

def interlace(*iters, next = next) -> collections.Iterable:
	"""
	interlace(i1, i2, ..., in) -> (
		i1-0, i2-0, ..., in-0,
		i1-1, i2-1, ..., in-1,
		.
		.
		.
		i1-n, i2-n, ..., in-n,
	)
	"""
	return map(next, cycle([iter(x) for x in iters]))

def chunk(iterable, chunksize = 256):
	"""
	Given an iterable, return an iterable producing chunks of the objects
	produced by the given iterable.

	chunks([o1,o2,o3,o4], chunksize = 2) -> [
		[o1,o2],
		[o3,o4],
	]
	"""
	iterable = iter(iterable)
	last = ()
	lastsize = chunksize
	while lastsize == chunksize:
		last = list(islice(iterable, chunksize))
		lastsize = len(last)
		yield last

def find(iterable, selector):
	"""
	Return the first item in the `iterable` that causes the `selector` to return
	`True`.
	"""
	for x in iterable:
		if selector(x):
			return x