Python more_itertools.take() Examples

The following are 30 code examples of more_itertools.take(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module more_itertools , or try the search function .
Example #1
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #2
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
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) 
Example #3
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
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) 
Example #4
Source File: test_more.py    From pipenv with MIT License 6 votes vote down vote up
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) 
Example #5
Source File: test_recipes.py    From pipenv with MIT License 5 votes vote down vote up
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]) 
Example #6
Source File: test_recipes.py    From pipenv with MIT License 5 votes vote down vote up
def test_negative_take(self):
        """Make sure taking negative items results in a ValueError"""
        self.assertRaises(ValueError, lambda: mi.take(-3, range(10))) 
Example #7
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
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]), []) 
Example #8
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
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) 
Example #9
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
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 
Example #10
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
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 
Example #11
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
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 
Example #12
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
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)]) 
Example #13
Source File: expunger.py    From recordexpungPDX with MIT License 5 votes vote down vote up
def _most_recent_convictions(recent_convictions):
        recent_convictions.sort(key=lambda charge: charge.disposition.date, reverse=True)
        newer, older = take(2, padnone(recent_convictions))
        if newer and "violation" in newer.level.lower():
            return older
        else:
            return newer 
Example #14
Source File: IO.py    From data_pipeline with Apache License 2.0 5 votes vote down vote up
def make_iter_lines(iterable_of_filenames, first_n=0):
    """return an iterator of lines for all filenames in `iterable_of_filenames`. It returns
    each element from the iterator is  in the shape of (filenae, (line_n, line)) starting
    from line_n = 1. If `first_n` is > 0 then only first n lines will be taken from the iter.
    """
    it = iter(iterable_of_filenames)

    in_handles = map(open_to_read, it)

    it_lines = itertools.chain.from_iterable(filter(lambda e: e is not None, in_handles))

    return more_itertools.take(first_n, it_lines) \
        if first_n > 0 else it_lines 
Example #15
Source File: seqc_tests.py    From qupulse with MIT License 5 votes vote down vote up
def make_binary_waveform(waveform):
    if waveform.duration == 0:
        data = np.asarray(3 * [1, 2, 3, 4, 5], dtype=np.uint16)
        return BinaryWaveform(data)
    else:
        chs = sorted(waveform.defined_channels)
        t = np.arange(0., waveform.duration, 1.)

        sampled = [None if ch is None else waveform.get_sampled(ch, t)
                   for _, ch in zip_longest(range(6), take(6, chs), fillvalue=None)]
        ch1, ch2, *markers = sampled
        return BinaryWaveform.from_sampled(ch1, ch2, markers) 
Example #16
Source File: test_recipes.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_simple_take(self):
        """Test basic usage"""
        t = mi.take(5, range(10))
        self.assertEqual(t, [0, 1, 2, 3, 4]) 
Example #17
Source File: test_recipes.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_null_take(self):
        """Check the null case"""
        t = mi.take(0, range(10))
        self.assertEqual(t, []) 
Example #18
Source File: test_recipes.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_negative_take(self):
        """Make sure taking negative items results in a ValueError"""
        self.assertRaises(ValueError, lambda: mi.take(-3, range(10))) 
Example #19
Source File: test_recipes.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
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]) 
Example #20
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
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]), []) 
Example #21
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #22
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #23
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #24
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #25
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
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)]) 
Example #26
Source File: test_recipes.py    From pipenv with MIT License 5 votes vote down vote up
def test_null_take(self):
        """Check the null case"""
        t = mi.take(0, range(10))
        self.assertEqual(t, []) 
Example #27
Source File: test_recipes.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_null_take(self):
        """Check the null case"""
        t = mi.take(0, range(10))
        self.assertEqual(t, []) 
Example #28
Source File: test_recipes.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_negative_take(self):
        """Make sure taking negative items results in a ValueError"""
        self.assertRaises(ValueError, lambda: mi.take(-3, range(10))) 
Example #29
Source File: test_recipes.py    From python-netsurv with MIT License 5 votes vote down vote up
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]) 
Example #30
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
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]), [])