Skip to content

Commit

Permalink
More pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Apr 4, 2012
1 parent 9f15334 commit e61551e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ load-plugins=
# can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once).
disable=W0142
disable=I0011,W0142,C0102


[REPORTS]
Expand Down Expand Up @@ -133,7 +133,7 @@ module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$
const-rgx=(([A-Za-z_][A-Za-z0-9_]*)|(__.*__))$

# Regular expression which should only match correct class names
class-rgx=[A-Z_][a-zA-Z0-9]+$
class-rgx=[a-zA-Z_][a-zA-Z0-9]+$

# Regular expression which should only match correct function names
function-rgx=[a-z_][a-z0-9_]{2,30}$
Expand All @@ -148,7 +148,7 @@ attr-rgx=[a-z_][a-z0-9_]{0,30}$
argument-rgx=[a-z_][a-z0-9_]{0,30}$

# Regular expression which should only match correct variable names
variable-rgx=[a-z_][a-z0-9_]{0,30}$
variable-rgx=[A-Za-z_][a-z0-9_]{0,30}$

# Regular expression which should only match correct list comprehension /
# generator expression variable names
Expand Down
12 changes: 6 additions & 6 deletions pygal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def add(self, title, values):
"""Add a serie to this graph"""
self.series.append(Serie(title, values, len(self.series)))

def _init(self):
"""Init the graph"""
def reinit(self):
"""(Re-)Init the graph"""
self.margin = Margin(*([20] * 4))
self._box = Box()

Expand Down Expand Up @@ -191,13 +191,13 @@ def _has_data(self):

def _render(self):
"""Make the graph internally"""
self._init()
self.svg._init()
self.reinit()
self.svg.reinit()
if self._has_data():
self._draw()
self.svg._pre_render(False)
self.svg.pre_render(False)
else:
self.svg._pre_render(True)
self.svg.pre_render(True)

def render(self, is_unicode=False):
"""Render the graph, and return the svg string"""
Expand Down
28 changes: 20 additions & 8 deletions pygal/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
"""
Svg helper
"""

from __future__ import division
import io
import os
from lxml import etree
from pygal.util import template
from pygal.util import template, coord_format
from pygal import __version__


Expand All @@ -30,8 +35,11 @@ class Svg(object):

def __init__(self, graph):
self.graph = graph
self.root = None
self.defs = None

def _init(self):
def reinit(self):
"""(Re-)initialization"""
self.root = etree.Element(
"{%s}svg" % self.ns,
nsmap={
Expand All @@ -45,6 +53,7 @@ def _init(self):
self.defs = self.node(tag='defs')

def add_style(self, css):
"""Add the css to the svg"""
style = self.node(self.defs, 'style', type='text/css')
with io.open(css, encoding='utf-8') as f:
templ = template(
Expand All @@ -55,6 +64,7 @@ def add_style(self, css):
style.text = templ

def add_script(self, js):
"""Add the js to the svg"""
script = self.node(self.defs, 'script', type='text/javascript')
with io.open(js, encoding='utf-8') as f:
templ = template(
Expand All @@ -65,6 +75,7 @@ def add_script(self, js):
script.text = templ

def node(self, parent=None, tag='g', attrib=None, **extras):
"""Make a new svg node"""
if parent is None:
parent = self.root
attrib = attrib or {}
Expand All @@ -80,31 +91,31 @@ def node(self, parent=None, tag='g', attrib=None, **extras):
return etree.SubElement(parent, tag, attrib)

def transposable_node(self, parent=None, tag='g', attrib=None, **extras):
"""Make a new svg node which can be transposed if horizontal"""
if self.graph._horizontal:
for key1, key2 in (('x', 'y'), ('width', 'height')):
attr1 = extras.get(key1, None)
attr2 = extras.get(key2, None)
extras[key1], extras[key2] = attr2, attr1
return self.node(parent, tag, attrib, **extras)

def format(self, xy):
return '%f %f' % xy

def line(self, node, coords, close=False, **kwargs):
"""Draw a svg line"""
if len(coords) < 2:
return
root = 'M%s L%s Z' if close else 'M%s L%s'
origin_index = 0
while None in coords[origin_index]:
origin_index += 1
origin = self.format(coords[origin_index])
line = ' '.join([self.format(c)
origin = coord_format(coords[origin_index])
line = ' '.join([coord_format(c)
for c in coords[origin_index + 1:]
if None not in c])
self.node(node, 'path',
d=root % (origin, line), **kwargs)

def _pre_render(self, no_data=False):
def pre_render(self, no_data=False):
"""Last things to do before rendering"""
self.add_style(self.graph.base_css or os.path.join(
os.path.dirname(__file__), 'css', 'graph.css'))
self.add_script(self.graph.base_js or os.path.join(
Expand All @@ -122,6 +133,7 @@ def _pre_render(self, no_data=False):
no_data.text = self.graph.no_data_text

def render(self, is_unicode=False):
"""Last thing to do before rendering"""
svg = etree.tostring(
self.root, pretty_print=True,
xml_declaration=not self.graph.disable_xml_declaration,
Expand Down
35 changes: 27 additions & 8 deletions pygal/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
"""
Various utils
"""

from __future__ import division
from decimal import Decimal
from math import floor, pi, log, log10
ORDERS = u"yzafpnµm kMGTPEZY"


def float_format(number):
"""Format a float to a precision of 3, without zeroes or dots"""
return ("%.3f" % number).rstrip('0').rstrip('.')


def humanize(number):
"""Format a number to engineer scale"""
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):
Expand All @@ -42,37 +49,43 @@ def is_major(number):


def round_to_int(number, precision):
"""Round a number to a precision"""
precision = int(precision)
rounded = (int(number) + precision / 2) // precision * precision
return rounded


def round_to_float(number, precision):
"""Round a float to a precision"""
rounded = Decimal(str(floor((number + precision / 2) // precision))
) * Decimal(str(precision))
return float(rounded)


def round_to_scale(number, precision):
"""Round a number or a float to a precision"""
if precision < 1:
return round_to_float(number, precision)
return round_to_int(number, precision)


def cut(list_, index=0):
"""Cut a list by index or arg"""
if isinstance(index, int):
cut = lambda x: x[index]
cut_ = lambda x: x[index]
else:
cut = lambda x: getattr(x, index)
return map(cut, list_)
cut_ = lambda x: getattr(x, index)
return map(cut_, list_)


def rad(deg):
return pi * deg / 180
def rad(degrees):
"""Convert degrees in radiants"""
return pi * degrees / 180


def deg(deg):
return 180 * deg / pi
def deg(radiants):
"""Convert radiants in degrees"""
return 180 * radiants / pi


def _swap_curly(string):
Expand All @@ -92,19 +105,25 @@ def template(string, **kwargs):
"""Format a string using double braces"""
return _swap_curly(string).format(**kwargs)


def coord_format(xy):
"""Format x y coords to svg"""
return '%f %f' % xy

swap = lambda tuple_: tuple(reversed(tuple_))
ident = lambda x: x


# Stolen from brownie http://packages.python.org/Brownie/
class cached_property(object):
"""Optimize a static property"""
def __init__(self, getter, doc=None):
self.getter = getter
self.__module__ = getter.__module__
self.__name__ = getter.__name__
self.__doc__ = doc or getter.__doc__

def __get__(self, obj, type=None):
def __get__(self, obj, type_=None):
if obj is None:
return self
value = obj.__dict__[self.__name__] = self.getter(obj)
Expand Down
1 change: 1 addition & 0 deletions pygal/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def __init__(self, width, height, box):
self.log10_ymin = log10(self.box.ymin)
self.box.fix(False)

# pylint: enable-msg=W0231
def y(self, y):
"""Project y"""
if y == None or y <= 0:
Expand Down

0 comments on commit e61551e

Please sign in to comment.