Skip to content

Commit

Permalink
Same code for python 2.7 and 3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed May 16, 2013
1 parent 88f437b commit c27bd13
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 63 deletions.
17 changes: 17 additions & 0 deletions pygal/compat.py → pygal/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
import sys

try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse


def to_str(string):
if sys.version_info[0] == 3:
base = (str, bytes)
coerce = str
else:
base = basestring
coerce = unicode
if not isinstance(string, base):
return coerce(string)
return string


def total_seconds(td):
if sys.version_info[:2] == (2, 6):
Expand Down
2 changes: 1 addition & 1 deletion pygal/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def int_to_country(x):
except:
return x
if x >= 0 and x < len(COUNTRIES):
return COUNTRIES.keys()[x]
return list(COUNTRIES.keys())[x]
return x


Expand Down
6 changes: 3 additions & 3 deletions pygal/graph/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def _compute(self):
self._box.ymin, self._box.ymax, self.logarithmic, self.order_min
) if not self.y_labels else map(float, self.y_labels)

self._x_labels = self.x_labels and zip(self.x_labels, [
(i + .5) / self._len for i in range(self._len)])
self._y_labels = zip(map(self._format, y_pos), y_pos)
self._x_labels = self.x_labels and list(zip(self.x_labels, [
(i + .5) / self._len for i in range(self._len)]))
self._y_labels = list(zip(map(self._format, y_pos), y_pos))

def _compute_secondary(self):
if self.secondary_series:
Expand Down
4 changes: 2 additions & 2 deletions pygal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def __init__(self, config, series, secondary_series):
else:
get = lambda x: x

positive_values = filter(
positive_values = list(filter(
lambda x: x > 0,
[get(val)
for serie in self.series for val in serie.safe_values])
for serie in self.series for val in serie.safe_values]))

self.zero = min(positive_values) if positive_values else 0
self._draw()
Expand Down
2 changes: 1 addition & 1 deletion pygal/graph/datey.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def jour(n) :
graph.render_in_browser()
"""

from pygal._compat import total_seconds
from pygal.adapters import date
from pygal.util import compute_scale
from pygal.graph.xy import XY
from pygal.compat import total_seconds
import datetime


Expand Down
7 changes: 4 additions & 3 deletions pygal/graph/dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _axes(self):

def dot(self, serie_node, serie, r_max):
"""Draw a dot line"""
view_values = map(self.view, serie.points)
view_values = list(map(self.view, serie.points))
for i, value in safe_enumerate(serie.values):
x, y = view_values[i]
size = r_max * value
Expand Down Expand Up @@ -69,8 +69,9 @@ def _compute(self):
(x_pos[i], y_pos[j])
for i in range(x_len)]

self._x_labels = self.x_labels and zip(self.x_labels, x_pos)
self._y_labels = zip(self.y_labels or cut(self.series, 'title'), y_pos)
self._x_labels = self.x_labels and list(zip(self.x_labels, x_pos))
self._y_labels = list(zip(
self.y_labels or cut(self.series, 'title'), y_pos))

def _plot(self):
r_max = min(
Expand Down
7 changes: 4 additions & 3 deletions pygal/graph/funnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ def _compute(self):
self._box.ymin, self._box.ymax, self.logarithmic, self.order_min
) if not self.y_labels else map(float, self.y_labels)

self._x_labels = zip(cut(self.series, 'title'),
map(lambda x: x - 1 / (2 * self._order), x_pos))
self._y_labels = zip(map(self._format, y_pos), y_pos)
self._x_labels = list(
zip(cut(self.series, 'title'),
map(lambda x: x - 1 / (2 * self._order), x_pos)))
self._y_labels = list(zip(map(self._format, y_pos), y_pos))

def _plot(self):
for index, serie in enumerate(self.series):
Expand Down
2 changes: 1 addition & 1 deletion pygal/graph/gauge.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _compute(self):
x_pos = compute_scale(
self.min_, self.max_, self.logarithmic, self.order_min
)
self._x_labels = zip(map(self._format, x_pos), x_pos)
self._x_labels = list(zip(map(self._format, x_pos), x_pos))

def _plot(self):
for index, serie in enumerate(self.series):
Expand Down
10 changes: 5 additions & 5 deletions pygal/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from pygal.util import (
is_major, truncate, reverse_text_len, get_texts_box, cut, rad)
from math import isnan, pi, sqrt, ceil, cos
from itertools import repeat, izip, chain
from itertools import repeat, chain


class Graph(BaseGraph):
Expand Down Expand Up @@ -296,13 +296,13 @@ def _legend(self):
# l - label
# tf - whether it is secondary label
gen = enumerate(enumerate(chain(
izip(self._legends, repeat(False)),
izip(self._secondary_legends, repeat(True)))))
zip(self._legends, repeat(False)),
zip(self._secondary_legends, repeat(True)))))
secondary_legends = legends # svg node is the same
else:
gen = enumerate(chain(
enumerate(izip(self._legends, repeat(False))),
enumerate(izip(self._secondary_legends, repeat(True)))))
enumerate(zip(self._legends, repeat(False))),
enumerate(zip(self._secondary_legends, repeat(True)))))

# draw secondary axis on right
x = self.margin.left + self.view.width + 10
Expand Down
8 changes: 4 additions & 4 deletions pygal/graph/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def line(self, serie_node, serie, rescale=False):
for x, y in serie.points]
else:
points = serie.points
view_values = map(self.view, points)
view_values = list(map(self.view, points))
if self.show_dots:
for i, (x, y) in enumerate(view_values):
if None in (x, y):
Expand Down Expand Up @@ -95,7 +95,7 @@ def line(self, serie_node, serie, rescale=False):

if self.stroke:
if self.interpolate:
view_values = map(self.view, serie.interpolated)
view_values = list(map(self.view, serie.interpolated))
if self.fill:
view_values = self._fill(view_values)
self.svg.line(
Expand All @@ -111,7 +111,7 @@ def _compute(self):
self._points(x_pos)

if self.x_labels:
self._x_labels = zip(self.x_labels, x_pos)
self._x_labels = list(zip(self.x_labels, x_pos))
else:
self._x_labels = None

Expand All @@ -127,7 +127,7 @@ def _compute(self):
self._box.ymin, self._box.ymax, self.logarithmic, self.order_min
) if not self.y_labels else map(float, self.y_labels)

self._y_labels = zip(map(self._format, y_pos), y_pos)
self._y_labels = list(zip(map(self._format, y_pos), y_pos))

def _plot(self):
for index, serie in enumerate(self.series):
Expand Down
4 changes: 2 additions & 2 deletions pygal/graph/radar.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ def _compute(self):
max_scale=8
) if not self.y_labels else map(int, self.y_labels)

self._x_labels = self.x_labels and zip(self.x_labels, x_pos)
self._y_labels = zip(map(self._format, y_pos), y_pos)
self._x_labels = self.x_labels and list(zip(self.x_labels, x_pos))
self._y_labels = list(zip(map(self._format, y_pos), y_pos))

self.x_pos = x_pos
self._self_close = True
10 changes: 5 additions & 5 deletions pygal/graph/stackedbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def _compute(self):
self._compute_box(positive_vals, negative_vals)

if self.logarithmic:
positive_vals = filter(lambda x: x > 0, positive_vals)
negative_vals = filter(lambda x: x > 0, negative_vals)
positive_vals = list(filter(lambda x: x > 0, positive_vals))
negative_vals = list(filter(lambda x: x > 0, negative_vals))

positive_vals = positive_vals or [self.zero]
negative_vals = negative_vals or [self.zero]
Expand All @@ -71,9 +71,9 @@ def _compute(self):
) if not self.y_labels else map(float, self.y_labels)
self._x_ranges = zip(x_pos, x_pos[1:])

self._x_labels = self.x_labels and zip(self.x_labels, [
sum(x_range) / 2 for x_range in self._x_ranges])
self._y_labels = zip(map(self._format, y_pos), y_pos)
self._x_labels = self.x_labels and list(zip(self.x_labels, [
sum(x_range) / 2 for x_range in self._x_ranges]))
self._y_labels = list(zip(map(self._format, y_pos), y_pos))

self.negative_cumulation = [0] * self._len
self.positive_cumulation = [0] * self._len
Expand Down
2 changes: 1 addition & 1 deletion pygal/graph/stackedline.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _points(self, x_pos):
for series_group in (self.series, self.secondary_series):
accumulation = [0] * self._len
for serie in series_group:
accumulation = map(sum, zip(accumulation, serie.values))
accumulation = list(map(sum, zip(accumulation, serie.values)))
serie.points = [
(x_pos[i], v)
for i, v in enumerate(accumulation)]
Expand Down
6 changes: 3 additions & 3 deletions pygal/graph/verticalpyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def _get_separated_values(self, secondary=False):
negative_vals = zip(*[serie.safe_values
for index, serie in enumerate(series)
if not index % 2])
return positive_vals, negative_vals
return list(positive_vals), list(negative_vals)

def _compute_box(self, positive_vals, negative_vals):
positive_sum = map(sum, positive_vals) or [self.zero]
negative_sum = map(sum, negative_vals) or [self.zero]
positive_sum = list(map(sum, positive_vals)) or [self.zero]
negative_sum = list(map(sum, negative_vals)) or [self.zero]
self._box.ymax = max(max(positive_sum), max(negative_sum))
self._box.ymin = - self._box.ymax

Expand Down
5 changes: 3 additions & 2 deletions pygal/graph/worldmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
class Worldmap(Graph):
"""Worldmap graph"""
_dual = True
x_labels = COUNTRIES.keys()
x_labels = list(COUNTRIES.keys())
country_names = COUNTRIES
_adapters = [int_to_country]

Expand All @@ -63,7 +63,8 @@ def _plot(self):
map.set('height', str(self.view.height))

for i, serie in enumerate(self.series):
safe_vals = filter(lambda x: x is not None, cut(serie.values, 1))
safe_vals = list(filter(
lambda x: x is not None, cut(serie.values, 1)))
if not safe_vals:
continue
min_ = min(safe_vals)
Expand Down
8 changes: 4 additions & 4 deletions pygal/graph/xy.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def _compute(self):
for serie in self.all_series:
serie.points = serie.values
if self.interpolate and xrng:
vals = zip(*sorted(
vals = list(zip(*sorted(
filter(lambda t: None not in t,
serie.points), key=lambda x: x[0]))
serie.points), key=lambda x: x[0])))
serie.interpolated = self._interpolate(
vals[1], vals[0], xy=True, xy_xmin=xmin, xy_rng=xrng)

Expand Down Expand Up @@ -102,5 +102,5 @@ def _compute(self):
y_pos = compute_scale(
self._box.ymin, self._box.ymax, self.logarithmic, self.order_min)

self._x_labels = zip(map(self._format, x_pos), x_pos)
self._y_labels = zip(map(self._format, y_pos), y_pos)
self._x_labels = list(zip(map(self._format, x_pos), x_pos))
self._y_labels = list(zip(map(self._format, y_pos), y_pos))
2 changes: 1 addition & 1 deletion pygal/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def interpolation(x, y, kind):
if len(y) < len(x):
x = x[:len(y)]

pack = zip(*filter(lambda t: None not in t, zip(x, y)))
pack = list(zip(*filter(lambda t: None not in t, zip(x, y))))
if len(pack) == 0:
return ident
x, y = pack
Expand Down
2 changes: 1 addition & 1 deletion pygal/serie.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, title, values, metadata=None):

@cached_property
def safe_values(self):
return filter(lambda x: x is not None, self.values)
return list(filter(lambda x: x is not None, self.values))


class Label(object):
Expand Down
10 changes: 5 additions & 5 deletions pygal/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"""

from __future__ import division
from pygal._compat import urlparse, to_str
import io
import os
import json
from datetime import date
from numbers import Number
from lxml import etree
from math import cos, sin, pi
from urlparse import urlparse
from pygal.util import template, coord_format, minify_css
from pygal import __version__

Expand Down Expand Up @@ -109,12 +109,12 @@ def in_attrib_and_number(key):
if in_attrib_and_number(pos):
attrib[pos] = attrib[pos] - attrib[dim]

for key, value in attrib.items():
for key, value in dict(attrib).items():
if value is None:
del attrib[key]
elif not isinstance(value, basestring):
attrib[key] = str(value)
elif key.endswith('_'):

attrib[key] = to_str(value)
if key.endswith('_'):
attrib[key.rstrip('_')] = attrib[key]
del attrib[key]
elif key == 'href':
Expand Down
8 changes: 4 additions & 4 deletions pygal/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ def test_human_readable():
line = Line()
line.add('_', [10 ** 4, 10 ** 5, 23 * 10 ** 4])
q = line.render_pyquery()
assert q(".axis.y text").map(texts) == map(
str, map(float, range(20000, 240000, 20000)))
assert q(".axis.y text").map(texts) == list(map(
str, map(float, range(20000, 240000, 20000))))
line.human_readable = True
q = line.render_pyquery()
assert q(".axis.y text").map(texts) == map(
lambda x: '%dk' % x, range(20, 240, 20))
assert q(".axis.y text").map(texts) == list(map(
lambda x: '%dk' % x, range(20, 240, 20)))


def test_show_legend():
Expand Down
2 changes: 1 addition & 1 deletion pygal/test/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_metadata(Chart):
chart = Chart()
v = range(7)
if Chart == pygal.XY:
v = map(lambda x: (x, x + 1), v)
v = list(map(lambda x: (x, x + 1), v))

chart.add('Serie with metadata', [
v[0],
Expand Down
Loading

0 comments on commit c27bd13

Please sign in to comment.