def PrefixWithIndex(logs):
  """Prefixes a bunch of lines with their index.

  Indicies are zero-padded so that everything aligns nicely.
  If there's a None log it's skipped and a linebreak is output.
  Trailing None logs are ignored.

  >>> list(PrefixWithIndex(['a', 'a']))
  ['1\\ta', '2\\ta']
  >>> list(PrefixWithIndex(['a' for _ in range(10)]))[:2]
  ['01\\ta', '02\\ta']
  >>> list(PrefixWithIndex(['a', None, 'a']))
  ['1\\ta', '', '2\\ta']

  Args:
    logs: The lines to index.
  Yields:
    The indexed lines.
  """
  # Makes sure we don't accidentally modify the real logs.
  # Also, makes the code not break if someone passes us a generator.
  logs = list(logs)
  while logs and logs[-1] is None:
    logs.pop()
  # Gods, I love this line. It creates a formatter that pads a number out to
  # match the largest number necessary to index all the non-null lines in logs.
  numberifier = '%%0%dd' % len(str(len(list(filter(bool, logs)))))
  adjustment = 0
  for (i, log) in enumerate(logs):
    if log is None:
      adjustment += 1
      yield ''
    else:
      index = (i + 1 - adjustment) % numberifier
      yield '%s\t%s' % (index, log)
dataset/ETHPy150Open google/vroom/vroom/output.py/PrefixWithIndex/swapped operands of `%`