def __str__(self): hist_lines = [] hist_bar = '|' for key, count in self.data: if self.total: bar_len = count * self.scale / self.total hist_bar = '|%s|' % ('#' * bar_len).ljust(self.scale) line = '%s %s %s' % (str(key).ljust( self.max_key_len), hist_bar, self.formatter(count)) percent_str = format_utils.NumToPercent(count, self.total) if percent_str: line += ' (%s)' % percent_str hist_lines.append(line) return '\n'.join(hist_lines)
def testCustomMinPrecision(self): self.assertEqual(format_utils.NumToPercent(3, 9, min_precision=3), '33.333%') self.assertEqual(format_utils.NumToPercent(3, 9, min_precision=0), '33%')
def testCustomMaxPrecision(self): self.assertEqual(format_utils.NumToPercent(3, 900, max_precision=1), '0.3%') self.assertEqual(format_utils.NumToPercent(3, 9000, max_precision=1), '0%')
def testDefaultMaxPrecision(self): self.assertEqual(format_utils.NumToPercent(3, 9000000), '0.00003%') self.assertEqual(format_utils.NumToPercent(3, 90000000), '0%')
def testDefaultMinPrecision(self): self.assertEqual(format_utils.NumToPercent(3, 9), '33.3%') self.assertEqual(format_utils.NumToPercent(3, 900), '0.3%')
def testWholePercent(self): self.assertEqual(format_utils.NumToPercent(3, 10), '30%')
def testOverHundredPercent(self): self.assertEqual(format_utils.NumToPercent(5, 2), '250%')
def testHundredPercent(self): self.assertEqual(format_utils.NumToPercent(1, 1), '100%')