pwnlib.util.itersitertools 标准库的扩展

This module includes and extends the standard module itertools.

pwnlib.util.iters.bruteforce(func, alphabet, length, method = 'upto', start = None)[源代码]

Bruteforce func to return True. func should take a string input and return a bool(). func will be called with strings from alphabet until it returns True or the search space has been exhausted.

The argument start can be used to split the search space, which is useful if multiple CPU cores are available.

参数:
  • func (function) – The function to bruteforce.
  • alphabet – The alphabet to draw symbols from.
  • length – Longest string to try.
  • method – If ‘upto’ try strings of length 1 .. length, if ‘fixed’ only try strings of length length and if ‘downfrom’ try strings of length length .. 1.
  • start – a tuple (i, N) which splits the search space up into N pieces and starts at piece i (1..N). None is equivalent to (1, 1).
返回:

A string s such that func(s) returns True or None if the search space was exhausted.

Example

>>> bruteforce(lambda x: x == 'hello', string.lowercase, length = 10)
'hello'
>>> bruteforce(lambda x: x == 'hello', 'hllo', 5) is None
True
pwnlib.util.iters.mbruteforce(func, alphabet, length, method = 'upto', start = None, threads = None)[源代码]

Same functionality as bruteforce(), but multithreaded.

参数:
  • alphabet, length, method, start (func,) – same as for bruteforce()
  • threads – Amount of threads to spawn, default is the amount of cores.
pwnlib.util.iters.chained(func)[源代码]

A decorator chaining the results of func. Useful for generators.

参数:func (function) – The function being decorated.
返回:A generator function whoose elements are the concatenation of the return values from func(*args, **kwargs).

Example

>>> @chained
... def g():
...     for x in count():
...         yield (x, -x)
>>> take(6, g())
[0, 0, 1, -1, 2, -2]
pwnlib.util.iters.consume(n, iterator)[源代码]

Advance the iterator n steps ahead. If n is :const:`None, consume everything.

参数:
  • n (int) – Number of elements to consume.
  • iterator (iterator) – An iterator.
返回:

None.

Examples

>>> i = count()
>>> consume(5, i)
>>> i.next()
5
>>> i = iter([1, 2, 3, 4, 5])
>>> consume(2, i)
>>> list(i)
[3, 4, 5]
pwnlib.util.iters.cyclen(n, iterable) → iterator[源代码]

Repeats the elements of iterable n times.

参数:
  • n (int) – The number of times to repeat iterable.
  • iterable – An iterable.
返回:

An iterator whoose elements are the elements of iterator repeated n times.

Examples

>>> take(4, cyclen(2, [1, 2]))
[1, 2, 1, 2]
>>> list(cyclen(10, []))
[]
pwnlib.util.iters.dotproduct(x, y) → int[源代码]

Computes the dot product of x and y.

参数:
  • x (iterable) – An iterable.
  • x – An iterable.
返回:

The dot product of x and y, i.e. – x[0] * y[0] + x[1] * y[1] + ....

Example

>>> dotproduct([1, 2, 3], [4, 5, 6])
... # 1 * 4 + 2 * 5 + 3 * 6 == 32
32
pwnlib.util.iters.flatten(xss) → iterator[源代码]

Flattens one level of nesting; when xss is an iterable of iterables, returns an iterator whoose elements is the concatenation of the elements of xss.

参数:xss – An iterable of iterables.
返回:An iterator whoose elements are the concatenation of the iterables in xss.

Examples

>>> list(flatten([[1, 2], [3, 4]]))
[1, 2, 3, 4]
>>> take(6, flatten([[43, 42], [41, 40], count()]))
[43, 42, 41, 40, 0, 1]
pwnlib.util.iters.group(n, iterable, fill_value = None) → iterator[源代码]

Similar to pwnlib.util.lists.group(), but returns an iterator and uses itertools fast build-in functions.

参数:
  • n (int) – The group size.
  • iterable – An iterable.
  • fill_value – The value to fill into the remaining slots of the last group if the n does not divide the number of elements in iterable.
返回:

An iterator whoose elements are n-tuples of the elements of iterable.

Examples

>>> list(group(2, range(5)))
[(0, 1), (2, 3), (4, None)]
>>> take(3, group(2, count()))
[(0, 1), (2, 3), (4, 5)]
>>> [''.join(x) for x in group(3, 'ABCDEFG', 'x')]
['ABC', 'DEF', 'Gxx']
pwnlib.util.iters.iter_except(func, exception)[源代码]

Calls func repeatedly until an exception is raised. Works like the build-in iter() but uses an exception instead of a sentinel to signal the end.

参数:
  • func (callable) – The function to call.
  • exception (Exception) – The exception that signals the end. Other exceptions will not be caught.
返回:

An iterator whoose elements are the results of calling func() until an exception matching exception is raised.

Examples

>>> s = {1, 2, 3}
>>> i = iter_except(s.pop, KeyError)
>>> i.next()
1
>>> i.next()
2
>>> i.next()
3
>>> i.next()
Traceback (most recent call last):
    ...
StopIteration
pwnlib.util.iters.lexicographic(alphabet) → iterator[源代码]

The words with symbols in alphabet, in lexicographic order (determined by the order of alphabet).

参数:alphabet – The alphabet to draw symbols from.
返回:An iterator of the words with symbols in alphabet, in lexicographic order.

Example

>>> take(8, imap(lambda x: ''.join(x), lexicographic('01')))
['', '0', '1', '00', '01', '10', '11', '000']
pwnlib.util.iters.lookahead(n, iterable) → object[源代码]

Inspects the upcoming element at index n without advancing the iterator. Raises IndexError if iterable has too few elements.

参数:
  • n (int) – Index of the element to return.
  • iterable – An iterable.
返回:

The element in iterable at index n.

Examples

>>> i = count()
>>> lookahead(4, i)
4
>>> i.next()
0
>>> i = count()
>>> nth(4, i)
4
>>> i.next()
5
>>> lookahead(4, i)
10
pwnlib.util.iters.nth(n, iterable, default = None) → object[源代码]

Returns the element at index n in iterable. If iterable is a iterator it will be advanced.

参数:
  • n (int) – Index of the element to return.
  • iterable – An iterable.
  • default (objext) – A default value.
返回:

The element at index n in iterable or default if iterable has too few elements.

Examples

>>> nth(2, [0, 1, 2, 3])
2
>>> nth(2, [0, 1], 42)
42
>>> i = count()
>>> nth(42, i)
42
>>> nth(42, i)
85
pwnlib.util.iters.pad(iterable, value = None) → iterator[源代码]

Pad an iterable with value, i.e. returns an iterator whoose elements are first the elements of iterable then value indefinitely.

参数:
  • iterable – An iterable.
  • value – The value to pad with.
返回:

An iterator whoose elements are first the elements of iterable then value indefinitely.

Examples

>>> take(3, pad([1, 2]))
[1, 2, None]
>>> i = pad(iter([1, 2, 3]), 42)
>>> take(2, i)
[1, 2]
>>> take(2, i)
[3, 42]
>>> take(2, i)
[42, 42]
pwnlib.util.iters.pairwise(iterable) → iterator[源代码]
参数:iterable – An iterable.
返回:An iterator whoose elements are pairs of neighbouring elements of iterable.

Examples

>>> list(pairwise([1, 2, 3, 4]))
[(1, 2), (2, 3), (3, 4)]
>>> i = starmap(operator.add, pairwise(count()))
>>> take(5, i)
[1, 3, 5, 7, 9]
pwnlib.util.iters.powerset(iterable, include_empty = True) → iterator[源代码]

The powerset of an iterable.

参数:
  • iterable – An iterable.
  • include_empty (bool) – Whether to include the empty set.
返回:

The powerset of iterable as an interator of tuples.

Examples

>>> list(powerset(range(3)))
[(), (0,), (1,), (2,), (0, 1), (0, 2), (1, 2), (0, 1, 2)]
>>> list(powerset(range(2), include_empty = False))
[(0,), (1,), (0, 1)]
pwnlib.util.iters.quantify(iterable, pred = bool) → int[源代码]

Count how many times the predicate pred is True.

参数:
  • iterable – An iterable.
  • pred – A function that given an element from iterable returns either True or False.
返回:

The number of elements in iterable for which pred returns True.

Examples

>>> quantify([1, 2, 3, 4], lambda x: x % 2 == 0)
2
>>> quantify(['1', 'two', '3', '42'], str.isdigit)
3
pwnlib.util.iters.random_combination(iterable, r) → tuple[源代码]
参数:
  • iterable – An iterable.
  • r (int) – Size of the combination.
返回:

A random element from itertools.combinations(iterable, r = r).

Examples

>>> random_combination(range(2), 2)
(0, 1)
>>> random_combination(range(10), r = 2) in combinations(range(10), r = 2)
True
pwnlib.util.iters.random_combination_with_replacement(iterable, r)[源代码]

random_combination(iterable, r) -> tuple

参数:
  • iterable – An iterable.
  • r (int) – Size of the combination.
返回:

A random element from itertools.combinations_with_replacement(iterable, r = r).

Examples

>>> cs = {(0, 0), (0, 1), (1, 1)}
>>> random_combination_with_replacement(range(2), 2) in cs
True
>>> i = combinations_with_replacement(range(10), r = 2)
>>> random_combination_with_replacement(range(10), r = 2) in i
True
pwnlib.util.iters.random_permutation(iterable, r=None)[源代码]

random_product(iterable, r = None) -> tuple

参数:
  • iterable – An iterable.
  • r (int) – Size of the permutation. If None select all elements in iterable.
返回:

A random element from itertools.permutations(iterable, r = r).

Examples

>>> random_permutation(range(2)) in {(0, 1), (1, 0)}
True
>>> random_permutation(range(10), r = 2) in permutations(range(10), r = 2)
True
pwnlib.util.iters.random_product(*args, repeat = 1) → tuple[源代码]
参数:
  • args – One or more iterables
  • repeat (int) – Number of times to repeat args.
返回:

A random element from itertools.product(*args, repeat = repeat).

Examples

>>> args = (range(2), range(2))
>>> random_product(*args) in {(0, 0), (0, 1), (1, 0), (1, 1)}
True
>>> args = (range(3), range(3), range(3))
>>> random_product(*args, repeat = 2) in product(*args, repeat = 2)
True
pwnlib.util.iters.repeat_func(func, *args, **kwargs) → iterator[源代码]

Repeatedly calls func with positional arguments args and keyword arguments kwargs. If no keyword arguments is given the resulting iterator will be computed using only functions from itertools which are very fast.

参数:
  • func (function) – The function to call.
  • args – Positional arguments.
  • kwargs – Keyword arguments.
返回:

An iterator whoose elements are the results of calling func(*args, **kwargs) repeatedly.

Examples

>>> def f(x):
...     x[0] += 1
...     return x[0]
>>> i = repeat_func(f, [0])
>>> take(2, i)
[1, 2]
>>> take(2, i)
[3, 4]
>>> def f(**kwargs):
...     return kwargs.get('x', 43)
>>> i = repeat_func(f, x = 42)
>>> take(2, i)
[42, 42]
>>> i = repeat_func(f, 42)
>>> take(2, i)
Traceback (most recent call last):
    ...
TypeError: f() takes exactly 0 arguments (1 given)
pwnlib.util.iters.roundrobin(*iterables)[源代码]

Take elements from iterables in a round-robin fashion.

参数:*iterables – One or more iterables.
返回:An iterator whoose elements are taken from iterables in a round-robin fashion.

Examples

>>> ''.join(roundrobin('ABC', 'D', 'EF'))
'ADEBFC'
>>> ''.join(take(10, roundrobin('ABC', 'DE', repeat('x'))))
'ADxBExCxxx'
pwnlib.util.iters.tabulate(func, start = 0) → iterator[源代码]
参数:
  • func (function) – The function to tabulate over.
  • start (int) – Number to start on.
返回:

An iterator with the elements func(start), func(start + 1), ....

Examples

>>> take(2, tabulate(str))
['0', '1']
>>> take(5, tabulate(lambda x: x**2, start = 1))
[1, 4, 9, 16, 25]
pwnlib.util.iters.take(n, iterable) → list[源代码]

Returns first n elements of iterable. If iterable is a iterator it will be advanced.

参数:
  • n (int) – Number of elements to take.
  • iterable – An iterable.
返回:

A list of the first n elements of iterable. If there are fewer than n elements in iterable they will all be returned.

Examples

>>> take(2, range(10))
[0, 1]
>>> i = count()
>>> take(2, i)
[0, 1]
>>> take(2, i)
[2, 3]
>>> take(9001, [1, 2, 3])
[1, 2, 3]
pwnlib.util.iters.unique_everseen(iterable, key = None) → iterator[源代码]

Get unique elements, preserving order. Remember all elements ever seen. If key is not None then for each element elm in iterable the element that will be rememberes is key(elm). Otherwise elm is remembered.

参数:
  • iterable – An iterable.
  • key – A function to map over each element in iterable before remembering it. Setting to None is equivalent to the identity function.
返回:

An iterator of the unique elements in iterable.

Examples

>>> ''.join(unique_everseen('AAAABBBCCDAABBB'))
'ABCD'
>>> ''.join(unique_everseen('ABBCcAD', str.lower))
'ABCD'
pwnlib.util.iters.unique_justseen(iterable, key=None)[源代码]

unique_everseen(iterable, key = None) -> iterator

Get unique elements, preserving order. Remember only the elements just seen. If key is not None then for each element elm in iterable the element that will be rememberes is key(elm). Otherwise elm is remembered.

参数:
  • iterable – An iterable.
  • key – A function to map over each element in iterable before remembering it. Setting to None is equivalent to the identity function.
返回:

An iterator of the unique elements in iterable.

Examples

>>> ''.join(unique_justseen('AAAABBBCCDAABBB'))
'ABCDAB'
>>> ''.join(unique_justseen('ABBCcAD', str.lower))
'ABCAD'
pwnlib.util.iters.unique_window(iterable, window, key=None)[源代码]

unique_everseen(iterable, window, key = None) -> iterator

Get unique elements, preserving order. Remember only the last window elements seen. If key is not None then for each element elm in iterable the element that will be rememberes is key(elm). Otherwise elm is remembered.

参数:
  • iterable – An iterable.
  • window (int) – The number of elements to remember.
  • key – A function to map over each element in iterable before remembering it. Setting to None is equivalent to the identity function.
返回:

An iterator of the unique elements in iterable.

Examples

>>> ''.join(unique_window('AAAABBBCCDAABBB', 6))
'ABCDA'
>>> ''.join(unique_window('ABBCcAD', 5, str.lower))
'ABCD'
>>> ''.join(unique_window('ABBCcAD', 4, str.lower))
'ABCAD'
pwnlib.util.iters.chain()[源代码]

Alias for itertools.chain().

pwnlib.util.iters.combinations()[源代码]

Alias for itertools.combinations()

pwnlib.util.iters.combinations_with_replacement()[源代码]

Alias for itertools.combinations_with_replacement()

pwnlib.util.iters.compress()[源代码]

Alias for itertools.compress()

pwnlib.util.iters.count()[源代码]

Alias for itertools.count()

pwnlib.util.iters.cycle()[源代码]

Alias for itertools.cycle()

pwnlib.util.iters.dropwhile()[源代码]

Alias for itertools.dropwhile()

pwnlib.util.iters.groupby()[源代码]

Alias for itertools.groupby()

pwnlib.util.iters.ifilter()[源代码]

Alias for itertools.ifilter()

pwnlib.util.iters.ifilterfalse()[源代码]

Alias for itertools.ifilterfalse()

pwnlib.util.iters.imap()[源代码]

Alias for itertools.imap()

pwnlib.util.iters.islice()[源代码]

Alias for itertools.islice()

pwnlib.util.iters.izip()[源代码]

Alias for itertools.izip()

pwnlib.util.iters.izip_longest()[源代码]

Alias for itertools.izip_longest()

pwnlib.util.iters.permutations()[源代码]

Alias for itertools.permutations()

pwnlib.util.iters.product()[源代码]

Alias for itertools.product()

pwnlib.util.iters.repeat()[源代码]

Alias for itertools.repeat()

pwnlib.util.iters.starmap()[源代码]

Alias for itertools.starmap()

pwnlib.util.iters.takewhile()[源代码]

Alias for itertools.takewhile()

pwnlib.util.iters.tee()[源代码]

Alias for itertools.tee()