Beispiel #1
0
    def test_pad__default(self):
        padded = __unit__.pad(self.ITERABLE)

        self._assertGenerator(padded)
        for i, elem in izip(xrange(self.MUCH_MORE_THAN_LENGTH), padded):
            if i < self.LENGTH:
                self.assertIs(self.ITERABLE[i], elem)
            else:
                self.assertIsNone(elem)
Beispiel #2
0
    def test_pad__custom(self):
        padded = __unit__.pad(self.ITERABLE, with_=self.PADDING)

        self._assertGenerator(padded)
        for i, elem in izip(xrange(self.MUCH_MORE_THAN_LENGTH), padded):
            if i < self.LENGTH:
                self.assertIs(self.ITERABLE[i], elem)
            else:
                self.assertIs(self.PADDING, elem)
Beispiel #3
0
    def test_pad__custom(self):
        padded = __unit__.pad(self.ITERABLE, with_=self.PADDING)

        self._assertGenerator(padded)
        for i, elem in izip(xrange(self.MUCH_MORE_THAN_LENGTH), padded):
            if i < self.LENGTH:
                self.assertIs(self.ITERABLE[i], elem)
            else:
                self.assertIs(self.PADDING, elem)
Beispiel #4
0
    def test_pad__default(self):
        padded = __unit__.pad(self.ITERABLE)

        self._assertGenerator(padded)
        for i, elem in izip(xrange(self.MUCH_MORE_THAN_LENGTH), padded):
            if i < self.LENGTH:
                self.assertIs(self.ITERABLE[i], elem)
            else:
                self.assertIsNone(elem)
Beispiel #5
0
    def test_n__positive(self):
        cycled = __unit__.cycle(self.ITERABLE, self.CYCLES_COUNT)

        # iterate for an exactly the expected number of elements
        for i, elem in izip(xrange(self.LENGTH * self.CYCLES_COUNT), cycled):
            self.assertEquals(self.ITERABLE[i % self.LENGTH], elem)

        # make sure the cycled iterable has been exhausted this way
        with self.assertRaises(StopIteration):
            next(cycled)
Beispiel #6
0
    def test_n__none(self):
        cycled = __unit__.cycle(self.ITERABLE)

        # iterate for a few cycles and check if elements match
        for i, elem in izip(xrange(self.LENGTH * self.CYCLES_COUNT), cycled):
            self.assertEquals(self.ITERABLE[i % self.LENGTH], elem)

        # make sure there is still some more
        for _ in xrange(self.CYCLES_COUNT):
            next(cycled)
Beispiel #7
0
    def test_n__positive(self):
        cycled = __unit__.cycle(self.ITERABLE, self.CYCLES_COUNT)

        # iterate for an exactly the expected number of elements
        for i, elem in izip(xrange(self.LENGTH * self.CYCLES_COUNT), cycled):
            self.assertEquals(self.ITERABLE[i % self.LENGTH], elem)

        # make sure the cycled iterable has been exhausted this way
        with self.assertRaises(StopIteration):
            next(cycled)
Beispiel #8
0
    def test_n__none(self):
        cycled = __unit__.cycle(self.ITERABLE)

        # iterate for a few cycles and check if elements match
        for i, elem in izip(xrange(self.LENGTH * self.CYCLES_COUNT), cycled):
            self.assertEquals(self.ITERABLE[i % self.LENGTH], elem)

        # make sure there is still some more
        for _ in xrange(self.CYCLES_COUNT):
            next(cycled)
Beispiel #9
0
def invert(dict_):
    """Return an inverted dictionary, where former values are keys
    and former keys are values.

    .. warning::

        If more than one key maps to any given value in input dictionary,
        it is undefined which one will be chosen for the result.

    :param dict_: Dictionary to swap keys and values in
    :return: Inverted dictionary
    """
    ensure_mapping(dict_)
    return dict_.__class__(izip(itervalues(dict_), iterkeys(dict_)))
Beispiel #10
0
    def test_iterable__empty(self):
        padded = __unit__.pad([])

        self._assertGenerator(padded)
        for _, elem in izip(xrange(self.MUCH_MORE_THAN_LENGTH), padded):
            self.assertIsNone(elem)
Beispiel #11
0
    def test_iterable__empty(self):
        padded = __unit__.pad([])

        self._assertGenerator(padded)
        for _, elem in izip(xrange(self.MUCH_MORE_THAN_LENGTH), padded):
            self.assertIsNone(elem)