/usr/lib/python2.7/dist-packages/stuf/iterable.py is in python-stuf 0.9.16-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 .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,
)
|