Browse Source

Code golf

pull/8/head
Florian Mounier 13 years ago
parent
commit
6041f70960
  1. 2
      demo/simple_test.py
  2. 20
      pygal/util.py

2
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])

20
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 <http://www.gnu.org/licenses/>.
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):

Loading…
Cancel
Save