def test_strict__false(self): self.assertEquals( self.WITH_STRICT_KEYS_OMITTED, __unit__.omit(self.STRICT_KEYS, self.DICT, strict=False)) self.assertEquals( self.WITH_NONSTRICT_KEYS_OMITTED, __unit__.omit(self.NONSTRICT_KEYS, self.DICT, strict=False))
def test_strict__true(self): self.assertEquals( self.WITH_STRICT_KEYS_OMITTED, __unit__.omit(self.STRICT_KEYS, self.DICT, strict=True)) with self.assertRaises(KeyError) as r: __unit__.omit(self.NONSTRICT_KEYS, self.DICT, strict=True) self.assertIn(repr(self.EXTRANEOUS_KEY), str(r.exception))
def output_xml(repo_name, contribs, totals): """Outputs the repository statistics in general XML format.""" from StringIO import StringIO import xml.etree.ElementTree as ET stats = ET.Element('statistics', attrib={'repo': repo_name}) def write_contrib(contrib, parent, tag): ET.SubElement(parent, tag, attrib=dicts.mapvalues(str_, contrib)) contributors = ET.SubElement(stats, 'contributors') for contrib in contribs: write_contrib(contrib, parent=contributors, tag='contributor') write_contrib(dicts.omit(['name'], from_=totals), parent=stats, tag='totals') result = StringIO() ET.ElementTree(stats).write(result, encoding='utf-8', xml_declaration=True) result.seek(0) return result.read()
def output_sexp(repo_name, contribs, totals): """Output the repository statistics as an S-expression.""" from StringIO import StringIO result = StringIO() print >>result, '(repo "%s"' % repo_name def write_contrib(contrib, tag, indent=0): indent = ' ' * indent print >>result, indent +'(%s %s)' % (tag, ' '.join( '(%s "%s")' % item for item in contrib.items())) for contrib in contribs: write_contrib(contrib, tag='contributor', indent=1) write_contrib(dicts.omit(['name'], from_=totals), tag='totals', indent=1) # insert final closing parenthesis before the last newline result.seek(-len(os.linesep), 1) print >>result, ")" result.seek(0) return result.read()[:-len(os.linesep)] # remove that last newline
def output_sexp(repo_name, contribs, totals): """Output the repository statistics as an S-expression.""" from StringIO import StringIO result = StringIO() print >> result, '(repo "%s"' % repo_name def write_contrib(contrib, tag, indent=0): indent = ' ' * indent print >> result, indent + '(%s %s)' % (tag, ' '.join( '(%s "%s")' % item for item in contrib.items())) for contrib in contribs: write_contrib(contrib, tag='contributor', indent=1) write_contrib(dicts.omit(['name'], from_=totals), tag='totals', indent=1) # insert final closing parenthesis before the last newline result.seek(-len(os.linesep), 1) print >> result, ")" result.seek(0) return result.read()[:-len(os.linesep)] # remove that last newline
def output_yaml(repo_name, contribs, totals): """Output the repository statistics in YAML format.""" from StringIO import StringIO result = StringIO() print >>result, "repo:", repo_name def write_contrib(contrib, indent=0): prefix = "- " indent = " " * indent for key, value in contrib.items(): print >>result, indent + "%s%s: %s" % (prefix, key, value) prefix = " " print >>result, "contributors:" for contrib in contribs: write_contrib(contrib) print >>result, "totals:" write_contrib(dicts.omit(['name'], from_=totals)) result.seek(0) return result.read()
def output_yaml(repo_name, contribs, totals): """Output the repository statistics in YAML format.""" from StringIO import StringIO result = StringIO() print >> result, "repo:", repo_name def write_contrib(contrib, indent=0): prefix = "- " indent = " " * indent for key, value in contrib.items(): print >> result, indent + "%s%s: %s" % (prefix, key, value) prefix = " " print >> result, "contributors:" for contrib in contribs: write_contrib(contrib) print >> result, "totals:" write_contrib(dicts.omit(['name'], from_=totals)) result.seek(0) return result.read()
def test_keys__none(self): with self.assertRaises(TypeError): __unit__.omit(None, self.DICT)
def test_from__some_object(self): with self.assertRaises(TypeError): __unit__.omit(self.STRICT_KEYS, object())
def test_from__empty(self): with self.assertRaises(KeyError): __unit__.omit(self.STRICT_KEYS, {}, strict=True) self.assertEquals({}, __unit__.omit(self.NONSTRICT_KEYS, {}, strict=False))
def test_keys__empty(self): self.assertEquals(self.DICT, __unit__.omit((), self.DICT))
def test_from__none(self): with self.assertRaises(TypeError): __unit__.omit(self.STRICT_KEYS, None)
def test_keys__some_object(self): with self.assertRaises(TypeError): __unit__.omit(object(), self.DICT)
def test_from__empty(self): with self.assertRaises(KeyError): __unit__.omit(self.STRICT_KEYS, {}, strict=True) self.assertEquals( {}, __unit__.omit(self.NONSTRICT_KEYS, {}, strict=False))