本文整理汇总了Python中more_itertools.take方法的典型用法代码示例。如果您正苦于以下问题:Python more_itertools.take方法的具体用法?Python more_itertools.take怎么用?Python more_itertools.take使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类more_itertools
的用法示例。
在下文中一共展示了more_itertools.take方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_result_index
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_result_index(self):
def stringify(*args, **kwargs):
self.assertEqual(args[0], 'arg_0')
iterable = args[1]
self.assertEqual(args[2], 'arg_2')
self.assertEqual(kwargs['kwarg_1'], 'kwarg_1')
return map(str, iterable)
stringifier = mi.make_decorator(stringify, result_index=1)
@stringifier('arg_0', 'arg_2', kwarg_1='kwarg_1')
def user_function(n):
return count(n)
it = user_function(1)
actual = mi.take(5, it)
expected = ['1', '2', '3', '4', '5']
self.assertEqual(actual, expected)
示例2: test_simple_take
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_simple_take(self):
"""Test basic usage"""
t = mi.take(5, range(10))
self.assertEqual(t, [0, 1, 2, 3, 4])
示例3: test_null_take
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_null_take(self):
"""Check the null case"""
t = mi.take(0, range(10))
self.assertEqual(t, [])
示例4: test_negative_take
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_negative_take(self):
"""Make sure taking negative items results in a ValueError"""
self.assertRaises(ValueError, lambda: mi.take(-3, range(10)))
示例5: test_take_too_much
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_take_too_much(self):
"""Taking more than an iterator has remaining should return what the
iterator has remaining.
"""
t = mi.take(10, range(5))
self.assertEqual(t, [0, 1, 2, 3, 4])
示例6: test_validator
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_validator(self):
iterable = count(0)
key = lambda x: int(str(x)[0]) # First digit of each number
validator = lambda x: 0 < x < 10 # No leading zeros
D = mi.bucket(iterable, key, validator=validator)
self.assertEqual(mi.take(3, D[1]), [1, 10, 11])
self.assertNotIn(0, D) # Non-valid entries don't return True
self.assertNotIn(0, D._cache) # Don't store non-valid entries
self.assertEqual(list(D[0]), [])
示例7: test_basic
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_basic(self):
expected = [
(0, 'a'), (0, 'b'), (0, 'c'),
(1, 'a'), (1, 'b'), (1, 'c'),
(2, 'a'), (2, 'b'), (2, 'c'),
]
for actual in [
mi.take(9, mi.count_cycle('abc')), # n=None
list(mi.count_cycle('abc', 3)), # n=3
]:
self.assertEqual(actual, expected)
示例8: test_partial_reset
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_partial_reset(self):
iterable = [str(n) for n in range(10)]
s = mi.seekable(iterable)
self.assertEqual(mi.take(5, s), iterable[:5]) # Normal iteration
s.seek(1)
self.assertEqual(list(s), iterable[1:]) # Get the rest of the iterable
示例9: test_forward
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_forward(self):
iterable = [str(n) for n in range(10)]
s = mi.seekable(iterable)
self.assertEqual(mi.take(1, s), iterable[:1]) # Normal iteration
s.seek(3) # Skip over index 2
self.assertEqual(list(s), iterable[3:]) # Result is similar to slicing
s.seek(0) # Back to 0
self.assertEqual(list(s), iterable) # No difference in result
示例10: test_past_end
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_past_end(self):
iterable = [str(n) for n in range(10)]
s = mi.seekable(iterable)
self.assertEqual(mi.take(1, s), iterable[:1]) # Normal iteration
s.seek(20)
self.assertEqual(list(s), []) # Iterable is exhausted
s.seek(0) # Back to 0
self.assertEqual(list(s), iterable) # No difference in result
示例11: test_elements
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_elements(self):
iterable = map(str, count())
s = mi.seekable(iterable)
mi.take(10, s)
elements = s.elements()
self.assertEqual(
[elements[i] for i in range(10)], [str(n) for n in range(10)]
)
self.assertEqual(len(elements), 10)
mi.take(10, s)
self.assertEqual(list(elements), [str(n) for n in range(20)])
示例12: test_no_n
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_no_n(self):
seq = [1, 2, 3]
# No fillvalue
self.assertEqual(mi.take(5, mi.padded(seq)), [1, 2, 3, None, None])
# With fillvalue
self.assertEqual(
mi.take(5, mi.padded(seq, fillvalue='')), [1, 2, 3, '', '']
)
示例13: test_encode
# 需要导入模块: import more_itertools [as 别名]
# 或者: from more_itertools import take [as 别名]
def test_encode(self):
iterable = (int(str(n)[0]) for n in count(800))
actual = mi.take(4, mi.run_length.encode(iterable))
expected = [(8, 100), (9, 100), (1, 1000), (2, 1000)]
self.assertEqual(actual, expected)