Example #1
0
def eat(iterator, amount):
  """
  Advance an iterator n-steps ahead. If n is None, eat entirely.

  Taken from the Python documentation. Under the PSF license.

  :param iterator:
      An iterator.
  :param amount:
      The number of steps to advance.
  :yields:
      An iterator.
  """
  # Use functions that consume iterators at C speed.
  if amount is None:
    # Feed the entire iterator into a zero-length deque.
    deque(iterator)
  else:
    # Advance to the empty slice starting at position n.
    next(islice(iterator, amount, amount), None)
Example #2
0
def nth(iterable, index, default=None):
  """
  Returns the nth element out of an iterable.

  :param iterable:
      Iterable sequence.
  :param index:
      Index
  :param default:
      If not found, this or ``None`` will be returned.
  :returns:
      nth element of the iterable sequence.
  """
  return next(islice(iterable, index, None), default)
Example #3
0
 def walker(path, topdown=topdown, followlinks=followlinks):
   """Alternative walker."""
   yield next(_walk(path, topdown=topdown, followlinks=followlinks))
Example #4
0
 def walk(path, topdown=topdown, followlinks=followlinks):
     yield next(_walk(path, topdown=topdown, followlinks=followlinks))