|
1 |
| -itertools_idioms |
| 1 | +Itertools Idioms |
2 | 2 | ================
|
3 | 3 |
|
4 |
| -Idioms with Python itertools |
| 4 | +Useful programming idioms with Python itertools. |
| 5 | + |
| 6 | +Usage: |
| 7 | + |
| 8 | + >>> from itertools_idioms import idioms |
| 9 | + # Select keys using variable constraints |
| 10 | + >>> import operator |
| 11 | + >>> prices = {'cake': 50, 'bread': 20, 'pie': 100} |
| 12 | + >>> constraints = {'cake': (operator.lt, 60), 'bread': (operator.le, 20), 'pie': (operator.lt, 80)} |
| 13 | + >>> list(idioms.select(prices,constraints)) |
| 14 | + ['cake', 'bread'] |
| 15 | + |
| 16 | + # Random streams - infinite and constrained |
| 17 | + |
| 18 | + # Infinite random stream from your iterable |
| 19 | + >>> for i in idioms.random_stream(iterable): print i |
| 20 | + |
| 21 | + # Specific random streaming functions |
| 22 | + # This would keep going forever |
| 23 | + >>> for i in idioms.random_alphabets(): print i |
| 24 | + |
| 25 | + # This is constrained and stops when it encounters W |
| 26 | + >>> list(idioms.random_alphabets(sentinel='W')) |
| 27 | + ['i', 'o', 'z', 'y', 'd', 'Z', 'Y', 's', 'S', 'O', 'Q'] |
| 28 | + |
| 29 | + # Infinite generator of random digits |
| 30 | + >>> idioms.random_digits() |
| 31 | + <itertools.imap object at 0xa12f76c> |
| 32 | + |
| 33 | + # Would stop when encountering 8 |
| 34 | + >>> list(idioms.random_digits(sentinel=8)) |
| 35 | + [4, 2, 3, 2, 9, 5, 9, 3, 7, 7, 5, 5, 3, 5] |
| 36 | + |
0 commit comments