MCPcopy Index your code
hub / github.com/python/cpython / islice

Method islice

Lib/test/test_itertools.py:1804–1822  ·  view source on GitHub ↗
(iterable, *args)

Source from the content-addressed store, hash-verified

1802
1803 @staticmethod
1804 def islice(iterable, *args):
1805 # islice('ABCDEFG', 2) → A B
1806 # islice('ABCDEFG', 2, 4) → C D
1807 # islice('ABCDEFG', 2, None) → C D E F G
1808 # islice('ABCDEFG', 0, None, 2) → A C E G
1809
1810 s = slice(*args)
1811 start = 0 if s.start is None else s.start
1812 stop = s.stop
1813 step = 1 if s.step is None else s.step
1814 if start < 0 or (stop is not None and stop < 0) or step <= 0:
1815 raise ValueError
1816
1817 indices = count() if stop is None else range(max(start, stop))
1818 next_i = start
1819 for i, element in zip(indices, iterable):
1820 if i == next_i:
1821 yield element
1822 next_i += step
1823
1824 def test_islice_recipe(self):
1825 self.assertEqual(list(self.islice('ABCDEFG', 2)), list('AB'))

Callers 10

test_islice_recipeMethod · 0.95
_get_code_positionFunction · 0.80
_get_code_positionFunction · 0.80
_get_sendmsg_bufferMethod · 0.80
make_namesMethod · 0.80
_parentsFunction · 0.80
_get_tasksMethod · 0.80
calc_pair_count_tableFunction · 0.80

Calls 2

sliceClass · 0.85
countFunction · 0.85

Tested by

no test coverage detected