Browse Source

Add majorize function with tests

pull/98/head
Guillaume Ayoub 11 years ago
parent
commit
0a3740b954
  1. 24
      pygal/test/test_util.py
  2. 18
      pygal/util.py

24
pygal/test/test_util.py

@ -140,10 +140,30 @@ def test_minify_css():
def test_major(): def test_major():
assert majorize(()) == []
assert majorize((0,)) == []
assert majorize((0, 1)) == []
assert majorize((0, 1, 2)) == []
assert majorize((-1, 0, 1, 2)) == [0]
assert majorize((0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1)) == [0, .5, 1] assert majorize((0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1)) == [0, .5, 1]
assert majorize((0, .2, .4, .6, .8, 1)) == [0, 1] assert majorize((0, .2, .4, .6, .8, 1)) == [0, 1]
assert majorize((-.4, -.2, 0, .2, .4, .6, .8, 1)) == [0, 1]
assert majorize(
(-1, -.8, -.6, -.4, -.2, 0, .2, .4, .6, .8, 1)) == [-1, 0, 1]
assert majorize((0, .2, .4, .6, .8, 1, 1.2, 1.4, 1.6)) == [0, 1]
assert majorize((0, .2, .4, .6, .8, 1, 1.2, 1.4, 1.6, 1.8, 2)) == [0, 1, 2] assert majorize((0, .2, .4, .6, .8, 1, 1.2, 1.4, 1.6, 1.8, 2)) == [0, 1, 2]
assert majorize((0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120)) == [0, 50, 100] assert majorize(
assert majorize((0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36)) == [0, 10, 20, 30] (0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120)) == [0, 50, 100]
assert majorize(
(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
22, 24, 26, 28, 30, 32, 34, 36)) == [0, 10, 20, 30]
assert majorize((0, 1, 2, 3, 4, 5)) == [0, 5] assert majorize((0, 1, 2, 3, 4, 5)) == [0, 5]
assert majorize((-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5)) == [-5, 0, 5]
assert majorize((-5, 5, -4, 4, 0, 1, -1, 3, -2, 2, -3)) == [-5, 0, 5]
assert majorize((0, 1, 2, 3, 4)) == [0]
assert majorize((3, 4, 5, 6)) == [5]
assert majorize((0, 1, 2, 3, 4, 5, 6, 7, 8)) == [0, 5]
assert majorize((-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5)) == [-5, 0, 5]
assert majorize((-6, -5, -4, -3, -2, -1, 0, 1, 2, 3)) == [-5, 0]
assert majorize((-6, -5, -4, -3)) == [-5]
assert majorize((1, 10, 100, 1000, 10000, 100000)) == [] assert majorize((1, 10, 100, 1000, 10000, 100000)) == []

18
pygal/util.py

@ -47,9 +47,23 @@ def humanize(number):
human_readable[int(order) - int(order > 0)]) human_readable[int(order) - int(order > 0)])
def majorize(s): def majorize(values):
"""Filter s sequence te return only major considered numbers""" """Filter s sequence te return only major considered numbers"""
return [] sorted_values = sorted(values)
if len(values) <= 3 or (
abs(2 * sorted_values[1] - sorted_values[0] - sorted_values[2]) >
abs(1.5 * (sorted_values[1] - sorted_values[0]))):
return []
step = 10 ** int(log10(sorted_values[-1] - sorted_values[0]))
if step == sorted_values[1] - sorted_values[0]:
step *= 10
if sorted_values[-1] < 2 * step:
step *= .5
elif sorted_values[-1] >= 5 * step:
step *= 5
major_values = [
value for value in values if value / step == round(value / step)]
return [value for value in sorted_values if value in major_values]
def round_to_int(number, precision): def round_to_int(number, precision):

Loading…
Cancel
Save