Browse Source

Change list like check (which did not worked in python 3)

pull/63/merge
Florian Mounier 11 years ago
parent
commit
e9f36b3617
  1. 12
      pygal/_compat.py
  2. 4
      pygal/ghost.py
  3. 7
      pygal/util.py

12
pygal/_compat.py

@ -17,7 +17,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
import sys
from collections import Iterable
if sys.version_info[0] == 3:
base = (str, bytes)
@ -27,8 +27,16 @@ else:
coerce = unicode
def is_list_like(value):
return isinstance(value, Iterable) and not isinstance(value, (base, dict))
def is_str(string):
return isinstance(string, base)
def to_str(string):
if not isinstance(string, base):
if not is_str(string):
return coerce(string)
return string

4
pygal/ghost.py

@ -27,7 +27,7 @@ from __future__ import division
import io
import sys
from pygal.config import Config
from pygal._compat import u
from pygal._compat import u, is_list_like
from pygal.graph import CHARTS_NAMES
from pygal.util import prepare_values
from uuid import uuid4
@ -63,7 +63,7 @@ class Ghost(object):
def add(self, title, values, secondary=False):
"""Add a serie to this graph"""
if not hasattr(values, '__iter__') and not isinstance(values, dict):
if not is_list_like(values):
values = [values]
if secondary:
self.raw_series2.append((title, values))

7
pygal/util.py

@ -21,7 +21,7 @@ Various utils
"""
from __future__ import division
from pygal._compat import to_str, u
from pygal._compat import to_str, u, is_list_like
import re
from decimal import Decimal
from math import floor, pi, log, log10, ceil
@ -355,13 +355,13 @@ def prepare_values(raw, config, cls):
if issubclass(cls, Histogram):
if value is None:
value = (None, None, None)
elif not hasattr(value, '__iter__'):
elif not is_list_like(value):
value = (value, config.zero, config.zero)
value = list(map(adapter, value))
elif cls._dual:
if value is None:
value = (None, None)
elif not hasattr(value, '__iter__'):
elif not is_list_like(value):
value = (value, config.zero)
if issubclass(cls, DateY) or issubclass(cls, Worldmap):
value = (adapter(value[0]), value[1])
@ -369,7 +369,6 @@ def prepare_values(raw, config, cls):
value = list(map(adapter, value))
else:
value = adapter(value)
values.append(value)
series.append(Serie(title, values, metadata))
return series

Loading…
Cancel
Save