From 6041f70960c59e7528f91a1213011ec593de3096 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Thu, 23 Feb 2012 17:35:35 +0100 Subject: [PATCH] Code golf --- demo/simple_test.py | 2 +- pygal/util.py | 20 ++++++-------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/demo/simple_test.py b/demo/simple_test.py index 3c62199..4493069 100755 --- a/demo/simple_test.py +++ b/demo/simple_test.py @@ -72,7 +72,7 @@ hstackedbar.add('--->', rng3) with open('out-horizontalstackedbar.svg', 'w') as f: f.write(hstackedbar.render()) -line = Line(Config(y_scale=.0005, style=NeonStyle, +line = Line(Config(y_scale=.0005, style=NeonStyle, human_readable=True, interpolate='univariate')) rng = range(-30, 31, 5) line.add('test1', [1000 ** cos(x / 10.) for x in rng]) diff --git a/pygal/util.py b/pygal/util.py index 888c9db..49ebb91 100644 --- a/pygal/util.py +++ b/pygal/util.py @@ -17,7 +17,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with pygal. If not, see . from decimal import Decimal -from math import floor, pi, log, ceil +from math import floor, pi, log +ORDERS = u"yzafpnµm kMGTPEZY" def float_format(number): @@ -25,22 +26,13 @@ def float_format(number): def humanize(number): - if number == 0: - return '0' - order = int(floor(log(abs(number)) / log(1000))) - if order > 0: - human_readable = "kMGTPEZY" - if order > len(human_readable): - return str(number) - elif order < 0: - human_readable = u"yzafpnµm " - if order > len(human_readable): - return str(number) - else: + order = number and int(floor(log(abs(number)) / log(1000))) + human_readable = ORDERS.split(" ")[int(order > 0)] + if order == 0 or order > len(human_readable): return str(number) return ( float_format(number / float(1000 ** int(order))) + - human_readable[int(order) - 1]) + human_readable[int(order) - int(order > 0)]) def round_to_int(number, precision):