Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
my_itertools
import timeit
from mytools import *
#
#
print(
'slide_each_simple (iterator)',
timeit.timeit(
stmt='slide_each_simple(range(1000), 3)',
globals=globals()
)
)
print(
'slide_each_faster_A (iterator)',
timeit.timeit(
stmt='slide_each_faster_A(range(1000), 3)',
globals=globals()
)
)
print(
'slide_each_faster_B (iterator)',
timeit.timeit(
stmt='slide_each_faster_B(range(1000), 3)',
globals=globals()
)
)
#
#
t_ls = [i for i in range(1000)]
print(
'slide_each_simple (list)',
timeit.timeit(
stmt='slide_each_simple(t_ls, 3)',
globals=globals()
)
)
print(
'slide_each_faster_A (list)',
timeit.timeit(
stmt='slide_each_faster_A(t_ls, 3)',
globals=globals()
)
)
print(
'slide_each_faster_B (list)',
timeit.timeit(
stmt='slide_each_faster_B(t_ls, 3)',
globals=globals()
)
)
#
#
t_tp = tuple(i for i in range(1000))
print(
'slide_each_simple (tuple)',
timeit.timeit(
stmt='slide_each_simple(t_tp, 3)',
globals=globals()
)
)
print(
'slide_each_faster_A (tuple)',
timeit.timeit(
stmt='slide_each_faster_A(t_tp, 3)',
globals=globals()
)
)
print(
'slide_each_faster_B (tuple)',
timeit.timeit(
stmt='slide_each_faster_B(t_tp, 3)',
globals=globals()
)
)
from copy import copy
def split_each(iterable, n):
return zip(
* [iter(iterable)]*n
)
def slide_each_simple(iterable, n):
sequence = tuple(iterable)
return zip(
* [sequence[i:] for i in range(n)]
)
def slide_each_faster_A(iterable, n):
iterator = iter(iterable)
return zip(
* [
[copy(iterator), next(iterator)][0]
for _ in range(n)
]
)
def slide_each_faster_B(iterable, n):
def _func(iterator, n):
for _ in range(n):
yield copy(iterator)
next(iterator)
return zip(
*_func(iter(iterable), n)
)
slide_each = slide_each_faster_A
slide_each_gen = slide_each_faster_A
slide_each_seq = slide_each_simple
from copy import copy
import unittest
from mytools import *
class TestMySlideTools(unittest.TestCase):
def setUp(self):
self._t_it = range(5)
self.ans_it = [(0, 1, 2), (1, 2, 3), (2, 3, 4)]
self.t_ls = [3, 1, 4, 1, 5]
self.ans_ls = [(3, 1, 4), (1, 4, 1), (4, 1, 5)]
@property
def t_it(self):
return copy(self._t_it)
def test_slide_each_simple(self):
self.assertEqual(
list(
slide_each_simple(self.t_it, 3)
),
self.ans_it
)
self.assertEqual(
list(
slide_each_simple(self.t_ls, 3)
),
self.ans_ls
)
def test_slide_each_faster_A(self):
self.assertEqual(
list(
slide_each_faster_A(self.t_it, 3)
),
self.ans_it
)
self.assertEqual(
list(
slide_each_faster_A(self.t_ls, 3)
),
self.ans_ls
)
def test_slide_each_faster_B(self):
self.assertEqual(
list(
slide_each_faster_B(self.t_it, 3)
),
self.ans_it
)
self.assertEqual(
list(
slide_each_faster_B(self.t_ls, 3)
),
self.ans_ls
)
class TestMySplitTools(unittest.TestCase):
def setUp(self):
self._t_it = range(6)
self.ans_it = [(0, 1, 2), (3, 4, 5)]
self.t_ls = [3, 1, 4, 1, 5, 9]
self.ans_ls = [(3, 1, 4), (1, 5, 9)]
@property
def t_it(self):
return copy(self._t_it)
def test_split_each(self):
self.assertEqual(
list(
split_each(self.t_it, 3)
),
self.ans_it
)
self.assertEqual(
list(
split_each(self.t_ls, 3)
),
self.ans_ls
)
if __name__ == '__main__':
MORE_SPECIFIC = 2
unittest.main(verbosity=MORE_SPECIFIC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.