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)
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)
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)
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)
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_)))
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)