1# -*- coding: utf-8 -*-2# Copyright 2011 Takeshi KOMIYA3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.1516from itertools import cycle, islice171819def istep(seq, step=2):20 iterable = iter(seq)21 while True:22 item = list(islice(iterable, step))23 if len(item) < step:24 break25 yield item262728def stepslice(iterable, steps):29 try:30 iterable = iter(iterable)31 step = cycle(steps)3233 while True:34 # skip (1)35 n = next(step)36 if n == 0:37 pass38 elif n == 1:39 o = next(iterable)40 yield o41 yield o42 else:43 yield next(iterable)44 for _ in range(n - 2):45 next(iterable)46 yield next(iterable)4748 # skip (2)49 for _ in range(next(step)):50 next(iterable)51 except StopIteration:52 return