def test_grouper(self): self.assertEqual(list(grouper([0, 1, 2, 3], 2)), [(0, 1), (2, 3)]) self.assertEqual(list(grouper([0, 1, 2, 3, 4, 5], 3)), [(0, 1, 2), (3, 4, 5)]) self.assertEqual(list(grouper([0, 1, 2, 3, 4], 3)), [(0, 1, 2), (3, 4, None)]) self.assertEqual(list(grouper([0, 1, 2, 3, 4], 3, 'x')), [(0, 1, 2), (3, 4, 'x')])
def test_grouper(self): self.assertEqual( list(grouper([0, 1, 2, 3], 2)), [(0, 1), (2, 3)]) self.assertEqual( list(grouper([0, 1, 2, 3, 4, 5], 3)), [(0, 1, 2), (3, 4, 5)]) self.assertEqual( list(grouper([0, 1, 2, 3, 4], 3)), [(0, 1, 2), (3, 4, None)]) self.assertEqual( list(grouper([0, 1, 2, 3, 4], 3, 'x')), [(0, 1, 2), (3, 4, 'x')])
def parse_release_file(content): """Parse Debian repo's Release file content. :param content: a Debian's Release file content :returns: a dict with repo's attributes """ # TODO(ikalnitsky): Consider to use some existing library for # parsing debian's release file (e.g. python-debian). _multivalued_fields = { 'SHA1': ['sha1', 'size', 'name'], 'SHA256': ['sha256', 'size', 'name'], 'SHA512': ['sha512', 'size', 'name'], 'MD5Sum': ['md5sum', 'size', 'name'], } # debian data format is very similiar to yaml, except # multivalued field. so we can parse it just like yaml # and then perform additional transformation for those # fields (we know which ones are multivalues). data = yaml.load(content) for attr, columns in six.iteritems(_multivalued_fields): if attr not in data: continue values = data[attr].split() data[attr] = [] for group in grouper(values, len(columns)): data[attr].append(dict(zip(columns, group))) return data