# stuf / stuf / iterable.py

 ``` 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106``` ```# -*- coding: utf-8 -*- '''stuf iterables.''' from functools import partial from itertools import starmap from stuf.six import items, map, next def _xhaust(mapfunc, call, iterable, exception=StopIteration, n=next): '''Call function `call` on an `iterable` until it's exhausted.''' iterable = mapfunc(call, iterable) try: while 1: n(iterable) except exception: pass def breakcount(call, length): '''Call function `call` until it reaches its original `length`.''' while length: yield call() length -= 1 def count(iterable, _n=next, S=StopIteration): '''Lazily calculate number of items in `iterable`.''' counter = enumerate(iterable, 1) idx = () while 1: try: idx = _n(counter) except S: try: return _n(iter(idx)) except S: return 0 def deferfunc(call): '''Defer running `call`.''' yield call() def deferiter(iterator): '''Defer running `iterator`.''' yield next(iterator) def exhaust(iterable, exception=StopIteration, _n=next): '''Call `next` on an `iterable` until it's exhausted.''' try: while 1: _n(iterable) except exception: pass def exhaustmap(call, mapping, filter=None, exception=StopIteration, _n=next): '''Call `call` with optional `filter` on a `mapping` until exhausted.''' iterable = starmap( call, items(mapping) if filter is None else filter(filter, items(mapping)), ) try: while 1: _n(iterable) except exception: pass def gauntlet(throws, this): '''Run sequence of callables in `thrown` on `this` object.''' for thrown in throws: this = thrown(this) return this def iterexcept(call, exception, start=None): ''' Call function `call` until `exception` is raised. from Raymond Hettinger Python Cookbook recipe # 577155 ''' try: if start is not None: yield start() while 1: yield call() except exception: pass _part = lambda m, p, c, d, *a, **k: m(p(c, *a, **k), d) partmap = partial(_part, map, partial,) partstar = partial(_part, starmap, partial) exhaustcall = partial(_xhaust, map) exhauststar = partial(_xhaust, starmap) xpartmap = partial(_part, exhaustcall, partial) xpartstar = partial(_part, exhauststar, partial) xpartitems = partial( lambda x, p, c, i, f=None, *a, **k: x(p(c, *a, **k), i, f), exhaustmap, partial, ) ```
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.