def test_valid_grouping(self): got = functional.chunks("aaaabbbbccccdddd", 4) expected = ["aaaa", "bbbb", "cccc", "dddd"] self.assertEqual(list(got), expected) got = functional.chunks([1, 1, 1, 2, 2, 2, 3, 3, 3], 3) expected = [[1, 1, 1], [2, 2, 2], [3, 3, 3]] self.assertEqual(list(got), expected)
def test_valid_grouping(self): got = chunks("aaaabbbbccccdddd", 4) expected = ["aaaa", "bbbb", "cccc", "dddd"] self.assertEqual(list(got), expected) got = chunks([1, 1, 1, 2, 2, 2, 3, 3, 3], 3) expected = [[1, 1, 1], [2, 2, 2], [3, 3, 3]] self.assertEqual(list(got), expected)
def test_filler_None(self): got = chunks("aaaabbbccccddd", 4, None) self.assertEqual(list(got), ["aaaa", "bbbc", "cccd", "dd"]) self.assertEqual(tuple(chunks((1, 1, 1, 2, 2), 3, None)), ((1, 1, 1, ), (2, 2, None))) self.assertEqual(tuple(chunks([1, 1, 1, 2, 2], 3, None)), ([1, 1, 1, ], [2, 2, None]))
def test_filler_None(self): got = functional.chunks("aaaabbbccccddd", 4, None) self.assertEqual(list(got), ["aaaa", "bbbc", "cccd", "dd"]) self.assertEqual(tuple(functional.chunks((1, 1, 1, 2, 2), 3, None)), ((1, 1, 1, ), (2, 2, None))) self.assertEqual(tuple(functional.chunks([1, 1, 1, 2, 2], 3, None)), ([1, 1, 1, ], [2, 2, None]))
def bin_decode(encoded): """ Decodes binary-encoded bytes into raw bytes. :param encoded: Binary representation. :returns: Raw bytes. """ if not is_bytes(encoded): raise TypeError("argument must be bytes: got %r" % type(encoded).__name__) return binascii.a2b_hex(EMPTY_BYTE.join(_BIN_TO_HEX_LOOKUP[nibble] for nibble in chunks(encoded, 4)))
def bin_decode(encoded): """ Decodes binary-encoded bytes into raw bytes. :param encoded: Binary representation. :returns: Raw bytes. """ if not builtins.is_bytes(encoded): raise TypeError("argument must be bytes: got %r" % type(encoded).__name__) return binascii.a2b_hex( EMPTY_BYTE.join(_BIN_TO_HEX_LOOKUP[nibble] for nibble in functional.chunks(encoded, 4)))
def test_odd_ball_grouping(self): got = chunks("aaabb", 3) self.assertEqual(list(got), ["aaa", "bb"])
def test_returns_generator_object(self): self.assertEqual(type(chunks("aaaabbbb", 4)).__name__, "generator")
def test_filler_iterable_not_same_type_as_filler(self): #self.assertRaises(TypeError, list, chunks("aaaabbbccccddd", 4, None)) self.assertRaises(TypeError, tuple, chunks((1, 1, 1, 2, 2), 3, [None, ]))
def test_filler(self): got = chunks("aaaabbbccccddd", 4, "-") self.assertEqual(list(got), ["aaaa", "bbbc", "cccd", "dd--"]) self.assertEqual(tuple(chunks((1, 1, 1, 2, 2), 3, (True,))), ((1, 1, 1, ), (2, 2, True)))
def test_odd_ball_grouping(self): got = functional.chunks("aaabb", 3) self.assertEqual(list(got), ["aaa", "bb"])
def test_returns_generator_object(self): self.assertEqual(type(functional.chunks("aaaabbbb", 4)).__name__, "generator")
def test_filler_iterable_not_same_type_as_filler(self): #self.assertRaises(TypeError, list, functional.chunks("aaaabbbccccddd", 4, None)) self.assertRaises(TypeError, tuple, functional.chunks((1, 1, 1, 2, 2), 3, [None, ]))
def test_filler(self): got = functional.chunks("aaaabbbccccddd", 4, "-") self.assertEqual(list(got), ["aaaa", "bbbc", "cccd", "dd--"]) self.assertEqual(tuple(functional.chunks((1, 1, 1, 2, 2), 3, (True,))), ((1, 1, 1, ), (2, 2, True)))