當前位置: 首頁>>代碼示例>>Python>>正文


Python more_itertools.take方法代碼示例

本文整理匯總了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) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:20,代碼來源:test_more.py

示例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]) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:6,代碼來源:test_recipes.py

示例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, []) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:6,代碼來源:test_recipes.py

示例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))) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:5,代碼來源:test_recipes.py

示例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]) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:9,代碼來源:test_recipes.py

示例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]), []) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:11,代碼來源:test_more.py

示例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) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:13,代碼來源:test_more.py

示例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 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:10,代碼來源:test_more.py

示例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 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:13,代碼來源:test_more.py

示例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 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:13,代碼來源:test_more.py

示例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)]) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:16,代碼來源:test_more.py

示例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, '', '']
        ) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:12,代碼來源:test_more.py

示例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) 
開發者ID:sofia-netsurv,項目名稱:python-netsurv,代碼行數:7,代碼來源:test_more.py


注:本文中的more_itertools.take方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。