Browse Source

Add decimal to float adapter to at least prevent crash on decimal. Fix #172

pull/185/head
Florian Mounier 10 years ago
parent
commit
fe6fe4c8ea
  1. 3
      CHANGELOG
  2. 2
      pygal/__init__.py
  3. 8
      pygal/adapters.py
  4. 1
      pygal/graph/frenchmap.py
  5. 27
      pygal/test/__init__.py
  6. 1
      pygal/test/test_graph.py
  7. 3
      pygal/util.py

3
CHANGELOG

@ -1,3 +1,6 @@
V 1.6.1
Fix Decimal incompatibility
V 1.6.0
Adds config option missing_value_fill_truncation. (thanks sirlark)
Avoid HTTP 301 Moved Permanently (thanks jean)

2
pygal/__init__.py

@ -21,7 +21,7 @@ Pygal - A python svg graph plotting library
"""
__version__ = '1.6.0'
__version__ = '1.6.1'
import sys
from pygal.config import Config
from pygal.ghost import Ghost, REAL_CHARTS

8
pygal/adapters.py

@ -22,7 +22,7 @@ Value adapters to use when a chart doesn't accept all value types
"""
import datetime
from numbers import Number
from pygal.i18n import COUNTRIES
from decimal import Decimal
def positive(x):
@ -52,3 +52,9 @@ def date(x):
except OverflowError:
return None
return x
def decimal_to_float(x):
if isinstance(x, Decimal):
return float(x)
return x

1
pygal/graph/frenchmap.py

@ -193,7 +193,6 @@ class FrenchMapDepartments(Graph):
kind = 'departement'
svg_map = DPT_MAP
@cached_property
def _values(self):
"""Getter for series values (flattened)"""

27
pygal/test/__init__.py

@ -22,15 +22,15 @@ from pygal.util import cut
from datetime import datetime
from pygal.i18n import COUNTRIES
from pygal.graph.frenchmap import DEPARTMENTS, REGIONS
from decimal import Decimal
def get_data(i):
return [
[(-1, 1), (2, 0), (0, 4)],
[(0, 1), (None, 2), (3, 2)],
[(-3, 3), (1, 3), (1, 1)],
[(1, 1), (1, 1), (1, 1)],
[(3, 2), (2, 1), (1, 1)]][i]
[(1, 1), (Decimal('1.'), 1), (1, 1)],
[(3, 2), (2, 1), (1., 1)]][i]
def adapt(chart, data):
@ -46,14 +46,23 @@ def adapt(chart, data):
data = cut(data)
if isinstance(chart, pygal.Worldmap):
return list(map(lambda x: list(COUNTRIES.keys())[x % len(COUNTRIES)]
if x is not None else None, data))
return list(
map(lambda x: list(
COUNTRIES.keys())[
int(x) % len(COUNTRIES)]
if x is not None else None, data))
elif isinstance(chart, pygal.FrenchMap_Regions):
return list(map(lambda x: list(REGIONS.keys())[x % len(REGIONS)]
if x is not None else None, data))
return list(
map(lambda x: list(
REGIONS.keys())[
int(x) % len(REGIONS)]
if x is not None else None, data))
elif isinstance(chart, pygal.FrenchMap_Departments):
return list(map(lambda x: list(DEPARTMENTS.keys())[x % len(DEPARTMENTS)]
if x is not None else None, data))
return list(
map(lambda x: list(
DEPARTMENTS.keys())[
int(x) % len(DEPARTMENTS)]
if x is not None else None, data))
return data

1
pygal/test/test_graph.py

@ -132,7 +132,6 @@ def test_empty_lists_with_nones(Chart):
q = chart.render_pyquery()
assert len(q(".legend")) == 2
def test_non_iterable_value(Chart):
chart = Chart(no_prefix=True)
chart.add('A', 1)

3
pygal/util.py

@ -27,7 +27,7 @@ from decimal import Decimal
from math import floor, pi, log, log10, ceil
from itertools import cycle
from functools import reduce
from pygal.adapters import not_zero, positive
from pygal.adapters import not_zero, positive, decimal_to_float
ORDERS = u("yzafpnµm kMGTPEZY")
@ -346,6 +346,7 @@ def prepare_values(raw, config, cls, offset=0):
if fun in adapters:
adapters.remove(fun)
adapters = adapters + [positive, not_zero]
adapters = adapters + [decimal_to_float]
adapter = reduce(compose, adapters) if not config.strict else ident
series = []

Loading…
Cancel
Save