[graphy commit] r77 - Fixed a data aliasing issue where LineStyle.solid and other constants like it were shared g...

0 views
Skip to first unread message

codesite...@google.com

unread,
Jun 26, 2009, 6:36:40 PM6/26/09
to gra...@googlegroups.com
Author: bugmaster
Date: Fri Jun 26 15:35:13 2009
New Revision: 77

Modified:
trunk/graphy/common.py
trunk/graphy/line_chart.py
trunk/graphy/line_chart_test.py

Log:
Fixed a data aliasing issue where LineStyle.solid and other constants like
it were shared globally across all charts. Changed LineStyle.solid() and
others into class methods, and changed DataSeries.style to automatically
call these methods in order to correct legacy code.


Modified: trunk/graphy/common.py
==============================================================================
--- trunk/graphy/common.py (original)
+++ trunk/graphy/common.py Fri Jun 26 15:35:13 2009
@@ -132,8 +132,24 @@
'DataSeries.style.color instead.', DeprecationWarning,
stacklevel=2)
self.style.color = color
-
- color = property(_GetColor, _SetColor)
+
+ color = property(_GetColor, _SetColor)
+
+ def _GetStyle(self):
+ return self._style;
+
+ def _SetStyle(self, style):
+ if style is not None and callable(style):
+ warnings.warn('Your code may be broken ! LineStyle.solid and
similar '
+ 'are no longer constants, but class methods that '
+ 'create LineStyle instances. Change your code to call '
+ 'LineStyle.solid() instead of passing it as a value.',
+ DeprecationWarning, stacklevel=2)
+ self._style = style()
+ else:
+ self._style = style
+
+ style = property(_GetStyle, _SetStyle)


class AxisPosition(object):

Modified: trunk/graphy/line_chart.py
==============================================================================
--- trunk/graphy/line_chart.py (original)
+++ trunk/graphy/line_chart.py Fri Jun 26 15:35:13 2009
@@ -21,7 +21,6 @@

from graphy import common

-
class LineStyle(object):

"""Represents the style for a line on a line chart. Also provides some
@@ -36,12 +35,12 @@
AutoColor will fill this in for you automatically if empty.

Some common styles, such as LineStyle.dashed, are available:
- solid
- dashed
- dotted
- thick_solid
- thick_dashed
- thick_dotted
+ LineStyle.solid()
+ LineStyle.dashed()
+ LineStyle.dotted()
+ LineStyle.thick_solid()
+ LineStyle.thick_dashed()
+ LineStyle.thick_dotted()
"""

# Widths
@@ -61,13 +60,29 @@
self.off = off
self.color = color

-
-LineStyle.solid = LineStyle(1, 1, 0)
-LineStyle.dashed = LineStyle(1, 8, 4)
-LineStyle.dotted = LineStyle(1, 2, 4)
-LineStyle.thick_solid = LineStyle(2, 1, 0)
-LineStyle.thick_dashed = LineStyle(2, 8, 4)
-LineStyle.thick_dotted = LineStyle(2, 2, 4)
+ @classmethod
+ def solid(cls):
+ return LineStyle(1, 1, 0)
+
+ @classmethod
+ def dashed(cls):
+ return LineStyle(1, 8, 4)
+
+ @classmethod
+ def dotted(cls):
+ return LineStyle(1, 2, 4)
+
+ @classmethod
+ def thick_solid(cls):
+ return LineStyle(2, 1, 0)
+
+ @classmethod
+ def thick_dashed(cls):
+ return LineStyle(2, 8, 4)
+
+ @classmethod
+ def thick_dotted(cls):
+ return LineStyle(2, 2, 4)


class LineChart(common.BaseChart):
@@ -110,7 +125,7 @@
label=None):
"""DEPRECATED"""
warnings.warn('LineChart.AddSeries is deprecated. Call AddLine
instead. ',
- DeprecationWarning, stacklevel=2)
+ DeprecationWarning, stacklevel=2)
return self.AddLine(points, color=color, width=style.width,
pattern=(style.on, style.off), markers=markers,
label=label)

Modified: trunk/graphy/line_chart_test.py
==============================================================================
--- trunk/graphy/line_chart_test.py (original)
+++ trunk/graphy/line_chart_test.py Fri Jun 26 15:35:13 2009
@@ -36,13 +36,13 @@
def testAddSeries(self):
warnings.filterwarnings('ignore')
chart = line_chart.LineChart()
- chart.AddSeries(points=[1, 2, 3], style=line_chart.LineStyle.solid,
+ chart.AddSeries(points=[1, 2, 3], style=line_chart.LineStyle.solid(),
markers='markers', label='label')
series = chart.data[0]
self.assertEqual(series.data, [1, 2, 3])
- self.assertEqual(series.style.width, line_chart.LineStyle.solid.width)
- self.assertEqual(series.style.on, line_chart.LineStyle.solid.on)
- self.assertEqual(series.style.off, line_chart.LineStyle.solid.off)
+ self.assertEqual(series.style.width,
line_chart.LineStyle.solid().width)
+ self.assertEqual(series.style.on, line_chart.LineStyle.solid().on)
+ self.assertEqual(series.style.off, line_chart.LineStyle.solid().off)
self.assertEqual(series.markers, 'markers')
self.assertEqual(series.label, 'label')

@@ -62,16 +62,36 @@
self.assertEqual('label', chart.data[0].label)
self.assertEqual([x], chart.data[0].markers)
self.assertEqual('color', chart.data[0].style.color)
+

class LineStyleTest(graphy_test.GraphyTest):

+ def tearDown(self):
+ warnings.resetwarnings()
+
def testPresets(self):
"""Test selected traits from the preset line styles."""
- self.assertEqual(0, line_chart.LineStyle.solid.off)
- self.assert_(line_chart.LineStyle.dashed.off > 0)
- self.assert_(line_chart.LineStyle.solid.width <
- line_chart.LineStyle.thick_solid.width)
-
+ self.assertEqual(0, line_chart.LineStyle.solid().off)
+ self.assert_(line_chart.LineStyle.dashed().off > 0)
+ self.assert_(line_chart.LineStyle.solid().width <
+ line_chart.LineStyle.thick_solid().width)
+
+ def testLineStyleByValueGivesWarning(self):
+ """Using LineStyle.foo as a value should throw a deprecation warning"""
+ warnings.filterwarnings('error')
+ self.assertRaises(DeprecationWarning, common.DataSeries, [],
+ style=line_chart.LineStyle.solid)
+ series = common.DataSeries([])
+ def _TestAssignment():
+ series.style = line_chart.LineStyle.solid
+ self.assertRaises(DeprecationWarning, _TestAssignment)
+ warnings.filterwarnings('ignore')
+ series.style = line_chart.LineStyle.solid
+ warnings.resetwarnings()
+ self.assertEqual(1, series.style.width)
+ self.assertEqual(1, series.style.on)
+ self.assertEqual(0, series.style.off)
+

if __name__ == '__main__':
graphy_test.main()

Reply all
Reply to author
Forward
0 new messages