@@ -4,77 +4,78 @@ Itertools Idioms
4
4
Useful programming idioms with Python itertools.
5
5
6
6
Usage:
7
+ ``` python
8
+ >> > from itertools_idioms import idioms
9
+ # Select keys using variable constraints
7
10
8
- >>> from itertools_idioms import idioms
9
- # Select keys using variable constraints
10
-
11
- >>> import operator
12
- >>> prices = {'cake': 50, 'bread': 20, 'pie': 100}
13
- >>> constraints = {'cake': (operator.lt, 60), 'bread': (operator.le, 20), 'pie': (operator.lt, 80)}
14
- >>> list(idioms.select(prices,constraints))
15
- ['cake', 'bread']
16
-
17
- # second option - supply your own constraing mapping to functions
18
- >>> constraints = {'cake': ('less', 60), 'bread': ('less', 30), 'pie': ('lessorequal', 80)}
19
- >>> cmap = {'less': operator.lt, 'lessorequal': operator.le}
20
- >>> list(idioms.select(prices,constraints, cmap=cmap))
21
- ['cake', 'bread']
22
-
23
- # best option - use default operator module mapping for comparison constraints.
24
- >>> constraints = {'cake': ('<', 60), 'bread': ('<=', 20), 'pie': ('<', 80)}
25
- >>> list(idioms.select(prices,constraints, use_operator=True))
26
- ['cake', 'bread']
27
-
28
- # Use select2 if the conditions are directly expressed as functions
29
- >>> constraints = {'cake': lambda x: x<60,
30
- ... 'bread': lambda x: x<=20,
31
- ... 'pie': lambda x: x<80 }
32
- >>> list(idioms.select2(prices, constraints))
33
- ['cake', 'bread']
34
-
35
- # Use 'call' to return iterables from callables which vary
36
- # in output when called at different times. Use optional
37
- # 'times' and 'filter' arguments to control the output.
38
-
39
- # Return upto 10 random numbers between 1 and 100
40
- >>> list(idioms.call(random.randrange, 1, 100, times=10))
41
- [70, 97, 39, 6, 83, 89, 88, 49, 66, 60]
42
-
43
- # Return upto 10 random numbers between 1 and 100 which
44
- # are multiples of 3
45
- >>> list(idioms.call(random.randrange, 1, 100, times=10, filter=lambda x: x%3==0))
46
- [60, 9, 63, 12, 21]
47
-
48
- # Random streams - infinite and constrained
49
-
50
- # Infinite random stream from your iterable
51
- >>> for i in idioms.random_stream(iterable): print i
52
-
53
- # Specific random streaming functions
54
- # This would keep going forever
55
- >>> for i in idioms.random_alphabets(): print i
56
-
57
- # This is constrained and stops when it encounters W
58
- >>> list(idioms.random_alphabets(sentinel='W'))
59
- ['i', 'o', 'z', 'y', 'd', 'Z', 'Y', 's', 'S', 'O', 'Q']
60
-
61
- # Infinite generator of random digits
62
- >>> idioms.random_digits()
63
- <itertools.imap object at 0xa12f76c>
64
-
65
- # Would stop when encountering 8
66
- >>> list(idioms.random_digits(sentinel=8))
67
- [4, 2, 3, 2, 9, 5, 9, 3, 7, 7, 5, 5, 3, 5]
68
-
69
- # Flatten nested iterators upto any level
70
- >>> list(flatten([1,[2,[3,[4,[5]]]]]))
71
- [1, 2, 3, 4, 5]
72
- >>> list(flatten([1,[2,3],[4,5]]))
73
- [1, 2, 3, 4, 5]
74
- >>> list(flatten(dict(enumerate(range(5)))))
75
- [0, 1, 2, 3, 4]
76
- >>> list(flatten([1,2,'python',{3:4, 4:5}, ['perl']]))
77
- [1, 2, 'python', 3, 4, 'perl']
78
- >>>
79
- >>> 'More coming soon!'
80
- 'More coming soon!'
11
+ >> > import operator
12
+ >> > prices = {' cake' : 50 , ' bread' : 20 , ' pie' : 100 }
13
+ >> > constraints = {' cake' : (operator.lt, 60 ), ' bread' : (operator.le, 20 ), ' pie' : (operator.lt, 80 )}
14
+ >> > list (idioms.select(prices,constraints))
15
+ [' cake' , ' bread' ]
16
+
17
+ # second option - supply your own constraing mapping to functions
18
+ >> > constraints = {' cake' : (' less' , 60 ), ' bread' : (' less' , 30 ), ' pie' : (' lessorequal' , 80 )}
19
+ >> > cmap = {' less' : operator.lt, ' lessorequal' : operator.le}
20
+ >> > list (idioms.select(prices,constraints, cmap = cmap))
21
+ [' cake' , ' bread' ]
22
+
23
+ # best option - use default operator module mapping for comparison constraints.
24
+ >> > constraints = {' cake' : (' <' , 60 ), ' bread' : (' <=' , 20 ), ' pie' : (' <' , 80 )}
25
+ >> > list (idioms.select(prices,constraints, use_operator = True ))
26
+ [' cake' , ' bread' ]
27
+
28
+ # Use select2 if the conditions are directly expressed as functions
29
+ >> > constraints = {' cake' : lambda x : x< 60 ,
30
+ ... ' bread' : lambda x : x<= 20 ,
31
+ ... ' pie' : lambda x : x< 80 }
32
+ >> > list (idioms.select2(prices, constraints))
33
+ [' cake' , ' bread' ]
34
+
35
+ # Use 'call' to return iterables from callables which vary
36
+ # in output when called at different times. Use optional
37
+ # 'times' and 'filter' arguments to control the output.
38
+
39
+ # Return upto 10 random numbers between 1 and 100
40
+ >> > list (idioms.call(random.randrange, 1 , 100 , times = 10 ))
41
+ [70 , 97 , 39 , 6 , 83 , 89 , 88 , 49 , 66 , 60 ]
42
+
43
+ # Return upto 10 random numbers between 1 and 100 which
44
+ # are multiples of 3
45
+ >> > list (idioms.call(random.randrange, 1 , 100 , times = 10 , filter = lambda x : x% 3 == 0 ))
46
+ [60 , 9 , 63 , 12 , 21 ]
47
+
48
+ # Random streams - infinite and constrained
49
+
50
+ # Infinite random stream from your iterable
51
+ >> > for i in idioms.random_stream(iterable): print i
52
+
53
+ # Specific random streaming functions
54
+ # This would keep going forever
55
+ >> > for i in idioms.random_alphabets(): print i
56
+
57
+ # This is constrained and stops when it encounters W
58
+ >> > list (idioms.random_alphabets(sentinel = ' W' ))
59
+ [' i' , ' o' , ' z' , ' y' , ' d' , ' Z' , ' Y' , ' s' , ' S' , ' O' , ' Q' ]
60
+
61
+ # Infinite generator of random digits
62
+ >> > idioms.random_digits()
63
+ < itertools.imap object at 0x a12f76c>
64
+
65
+ # Would stop when encountering 8
66
+ >> > list (idioms.random_digits(sentinel = 8 ))
67
+ [4 , 2 , 3 , 2 , 9 , 5 , 9 , 3 , 7 , 7 , 5 , 5 , 3 , 5 ]
68
+
69
+ # Flatten nested iterators upto any level
70
+ >> > list (flatten([1 ,[2 ,[3 ,[4 ,[5 ]]]]]))
71
+ [1 , 2 , 3 , 4 , 5 ]
72
+ >> > list (flatten([1 ,[2 ,3 ],[4 ,5 ]]))
73
+ [1 , 2 , 3 , 4 , 5 ]
74
+ >> > list (flatten(dict (enumerate (range (5 )))))
75
+ [0 , 1 , 2 , 3 , 4 ]
76
+ >> > list (flatten([1 ,2 ,' python' ,{3 :4 , 4 :5 }, [' perl' ]]))
77
+ [1 , 2 , ' python' , 3 , 4 , ' perl' ]
78
+ >> >
79
+ >> > ' More coming soon!'
80
+ ' More coming soon!'
81
+ ```
0 commit comments