Merge lp:~verterok/txstatsd/py3ksupport into lp:txstatsd

Proposed by Guillermo Gonzalez
Status: Merged
Approved by: Guillermo Gonzalez
Approved revision: 108
Merged at revision: 106
Proposed branch: lp:~verterok/txstatsd/py3ksupport
Merge into: lp:txstatsd
Diff against target: 337 lines (+58/-39)
11 files modified
.bzrignore (+1/-0)
txstatsd/metrics/distinctmetric.py (+8/-2)
txstatsd/metrics/metric.py (+1/-1)
txstatsd/stats/uniformsample.py (+4/-3)
txstatsd/tests/metrics/test_distinct.py (+4/-3)
txstatsd/tests/metrics/test_histogrammetric.py (+1/-1)
txstatsd/tests/metrics/test_sli.py (+6/-2)
txstatsd/tests/test_httpinfo.py (+2/-2)
txstatsd/tests/test_metrics.py (+22/-21)
txstatsd/tests/test_router.py (+1/-1)
txstatsd/tests/test_service.py (+8/-3)
To merge this branch: bzr merge lp:~verterok/txstatsd/py3ksupport
Reviewer Review Type Date Requested Status
Sidnei da Silva Approve
Review via email: mp+152169@code.launchpad.net

Commit message

Initial py3k support, only the client side.

Description of the change

Initial py3k support, only the client side.

To post a comment you must log in.
Revision history for this message
Sidnei da Silva (sidnei) :
review: Approve
lp:~verterok/txstatsd/py3ksupport updated
106. By Guillermo Gonzalez

merge with trunk

107. By Guillermo Gonzalez

also support py2.6

108. By Guillermo Gonzalez

simplify version_info check

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '.bzrignore'
2--- .bzrignore 2011-07-07 03:29:34 +0000
3+++ .bzrignore 2013-03-08 13:29:21 +0000
4@@ -1,2 +1,3 @@
5 _trial_temp*
6 twisted/plugins/dropin.cache
7+__pycache__
8
9=== modified file 'txstatsd/metrics/distinctmetric.py'
10--- txstatsd/metrics/distinctmetric.py 2012-06-28 17:29:26 +0000
11+++ txstatsd/metrics/distinctmetric.py 2013-03-08 13:29:21 +0000
12@@ -28,8 +28,9 @@
13 """
14 import random
15 import time
16+import sys
17
18-from zope.interface import implements
19+from zope.interface import implementer, implements
20
21 from txstatsd.metrics.metric import Metric
22 from txstatsd.itxstatsd import IMetric
23@@ -121,7 +122,8 @@
24 Keeps an estimate of the distinct numbers of items seen on various
25 sliding windows of time.
26 """
27- implements(IMetric)
28+ if sys.version_info[0:2] == (2,6):
29+ implements(IMetric)
30
31 def __init__(self, name, wall_time_func=time.time, prefix=""):
32 """Construct a metric we expect to be periodically updated.
33@@ -166,3 +168,7 @@
34 for item, value in items.iteritems():
35 metrics.append((self.prefix + self.name + item, value, timestamp))
36 return metrics
37+
38+# if we are running anything >= 2.7
39+if sys.version_info[0:2] >= (2,7):
40+ DistinctMetricReporter = implementer(IMetric)(DistinctMetricReporter)
41
42=== modified file 'txstatsd/metrics/metric.py'
43--- txstatsd/metrics/metric.py 2012-06-28 17:29:26 +0000
44+++ txstatsd/metrics/metric.py 2013-03-08 13:29:21 +0000
45@@ -64,4 +64,4 @@
46 def write(self, data):
47 """Message the C{data} to the C{StatsD} server."""
48 if self.connection is not None:
49- self.connection.write(data)
50+ self.connection.write(data.encode('utf-8'))
51
52=== modified file 'txstatsd/stats/uniformsample.py'
53--- txstatsd/stats/uniformsample.py 2012-06-28 17:29:26 +0000
54+++ txstatsd/stats/uniformsample.py 2013-03-08 13:29:21 +0000
55@@ -27,7 +27,7 @@
56 """
57 A random sample of a stream of values. Uses Vitter's Algorithm R to
58 produce a statistically representative sample.
59-
60+
61 See:
62 - U{Random Sampling with a Reservoir
63 <http://www.cs.umd.edu/~samir/498/vitter.pdf>}
64@@ -42,7 +42,8 @@
65 self._values = [0 for i in range(reservoir_size)]
66 self._count = 0
67 self.clear()
68-
69+ self.maxint = getattr(sys, 'maxint', sys.maxsize)
70+
71 def clear(self):
72 self._values = [0 for i in range(len(self._values))]
73 self._count = 0
74@@ -56,7 +57,7 @@
75 if self._count <= len(self._values):
76 self._values[self._count - 1] = value
77 else:
78- r = random.randint(1, sys.maxint) % self._count
79+ r = random.randint(1, self.maxint) % self._count
80 if r < len(self._values):
81 self._values[r] = value
82
83
84=== modified file 'txstatsd/tests/metrics/test_distinct.py'
85--- txstatsd/tests/metrics/test_distinct.py 2012-06-28 17:29:26 +0000
86+++ txstatsd/tests/metrics/test_distinct.py 2013-03-08 13:29:21 +0000
87@@ -18,6 +18,7 @@
88 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
89 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
90 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
91+from __future__ import print_function
92
93 import random
94
95@@ -45,7 +46,7 @@
96 from scipy.stats import chi2
97 except ImportError:
98 raise SkipTest("Missing chi2, skipping")
99-
100+
101 N = 10000
102
103 for (bits, buckets) in [(-1, 1024), (24, 256),
104@@ -61,7 +62,7 @@
105 value = sum(((x - N / buckets) ** 2) / (N / buckets) for x in bins)
106 pval = chi2.cdf(value, N)
107 if pval > 0.5:
108- print bins, pval
109+ print(bins, pval)
110 self.assertTrue(pval < 0.5, "bits %s, pval == %s" % (bits, pval))
111 test_chi_square.skip = "Takes too long to run every time."
112
113@@ -110,7 +111,7 @@
114
115
116 class TestPlugin(TestCase):
117-
118+
119 def test_factory(self):
120 self.assertTrue(distinct_plugin.distinct_metric_factory in \
121 list(getPlugins(IMetricFactory)))
122
123=== modified file 'txstatsd/tests/metrics/test_histogrammetric.py'
124--- txstatsd/tests/metrics/test_histogrammetric.py 2012-06-28 17:29:26 +0000
125+++ txstatsd/tests/metrics/test_histogrammetric.py 2013-03-08 13:29:21 +0000
126@@ -102,7 +102,7 @@
127 histogram.update(i)
128
129 hist = histogram.histogram()
130- self.assertEquals(sum(hist), 10000)
131+ self.assertEqual(sum(hist), 10000)
132
133 total = sum(hist)
134 binsize = int(total / len(hist))
135
136=== modified file 'txstatsd/tests/metrics/test_sli.py'
137--- txstatsd/tests/metrics/test_sli.py 2012-06-28 17:29:26 +0000
138+++ txstatsd/tests/metrics/test_sli.py 2013-03-08 13:29:21 +0000
139@@ -20,8 +20,12 @@
140 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
141 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
142
143-import ConfigParser
144-from cStringIO import StringIO
145+try:
146+ import ConfigParser
147+ from StringIO import StringIO
148+except ImportError:
149+ import configparser as ConfigParser
150+ from io import StringIO
151 from twisted.trial.unittest import TestCase
152
153 from twisted.plugins.sli_plugin import SLIMetricFactory
154
155=== modified file 'txstatsd/tests/test_httpinfo.py'
156--- txstatsd/tests/test_httpinfo.py 2012-06-28 17:29:26 +0000
157+++ txstatsd/tests/test_httpinfo.py 2013-03-08 13:29:21 +0000
158@@ -111,7 +111,7 @@
159 def test_httpinfo_error(self):
160 try:
161 data = yield self.get_results("status", last_flush_duration=30)
162- except HttpException, e:
163+ except HttpException as e:
164 self.assertEquals(e.response.code, 500)
165 else:
166 self.fail("Not 500")
167@@ -121,7 +121,7 @@
168 try:
169 data = yield self.get_results("metrics/gorets",
170 timer_metrics={'gorets': 100})
171- except HttpException, e:
172+ except HttpException as e:
173 self.assertEquals(e.response.code, 404)
174 else:
175 self.fail("Not 404")
176
177=== modified file 'txstatsd/tests/test_metrics.py'
178--- txstatsd/tests/test_metrics.py 2012-06-28 17:29:26 +0000
179+++ txstatsd/tests/test_metrics.py 2013-03-08 13:29:21 +0000
180@@ -52,28 +52,29 @@
181 """Test reporting of a gauge metric sample."""
182 self.metrics.gauge('gauge', 102)
183 self.assertEqual(self.connection.data,
184- 'txstatsd.tests.gauge:102|g')
185+ b'txstatsd.tests.gauge:102|g')
186
187 def test_meter(self):
188 """Test reporting of a meter metric sample."""
189 self.metrics.meter('meter', 3)
190 self.assertEqual(self.connection.data,
191- 'txstatsd.tests.meter:3|m')
192+ b'txstatsd.tests.meter:3|m')
193
194 def test_counter(self):
195 """Test the increment and decrement operations."""
196 self.metrics.increment('counter', 18)
197 self.assertEqual(self.connection.data,
198- 'txstatsd.tests.counter:18|c')
199+ b'txstatsd.tests.counter:18|c')
200 self.metrics.decrement('counter', 9)
201 self.assertEqual(self.connection.data,
202- 'txstatsd.tests.counter:-9|c')
203+ b'txstatsd.tests.counter:-9|c')
204
205 def test_timing(self):
206 """Test the timing operation."""
207 self.metrics.timing('timing', 101.1234)
208- self.assertEqual(self.connection.data,
209- 'txstatsd.tests.timing:101123.4|ms')
210+ # to support both 2 and 3, can't use assertRegex
211+ match = re.match(b'txstatsd.tests.timing:101123.4[0-9]*|ms', self.connection.data)
212+ self.assertFalse(match is None)
213
214 def test_timing_automatic(self):
215 """Test the automatic timing operation with explicit reset"""
216@@ -85,9 +86,9 @@
217
218 elapsed = time.time() - start_time
219
220- label, val, units = re.split(":|\|", self.connection.data)
221- self.assertEqual(label, 'txstatsd.tests.timing')
222- self.assertEqual(units, 'ms')
223+ label, val, units = re.split(b":|\|", self.connection.data)
224+ self.assertEqual(label, b'txstatsd.tests.timing')
225+ self.assertEqual(units, b'ms')
226 self.assertTrue(100 <= float(val) <= elapsed * 1000)
227
228 def test_timing_automatic_implicit_reset(self):
229@@ -100,34 +101,34 @@
230
231 elapsed = time.time() - start_time
232
233- label, val, units = re.split(":|\|", self.connection.data)
234- self.assertEqual(label, 'txstatsd.tests.timing')
235- self.assertEqual(units, 'ms')
236+ label, val, units = re.split(b":|\|", self.connection.data)
237+ self.assertEqual(label, b'txstatsd.tests.timing')
238+ self.assertEqual(units, b'ms')
239 self.assertTrue(100 <= float(val) <= elapsed * 1000)
240
241 def test_generic(self):
242 """Test the GenericMetric class."""
243 self.metrics.report('users', "pepe", "pd")
244 self.assertEqual(self.connection.data,
245- 'txstatsd.tests.users:pepe|pd')
246+ b'txstatsd.tests.users:pepe|pd')
247
248 def test_generic_extra(self):
249 """Test the GenericMetric class."""
250 self.metrics.report('users', "pepe", "pd", 100)
251 self.assertEqual(self.connection.data,
252- 'txstatsd.tests.users:pepe|pd|100')
253+ b'txstatsd.tests.users:pepe|pd|100')
254
255 def test_empty_namespace(self):
256 """Test reporting of an empty namespace."""
257 self.metrics.namespace = None
258 self.metrics.gauge('gauge', 213)
259 self.assertEqual(self.connection.data,
260- 'gauge:213|g')
261+ b'gauge:213|g')
262
263 self.metrics.namespace = ''
264 self.metrics.gauge('gauge', 413)
265 self.assertEqual(self.connection.data,
266- 'gauge:413|g')
267+ b'gauge:413|g')
268
269
270 class TestExtendedMetrics(TestMetrics):
271@@ -139,21 +140,21 @@
272 """Test the increment and decrement operations."""
273 self.metrics.increment('counter', 18)
274 self.assertEqual(self.connection.data,
275- 'txstatsd.tests.counter:18|c')
276+ b'txstatsd.tests.counter:18|c')
277 self.metrics.decrement('counter', 9)
278 self.assertEqual(self.connection.data,
279- 'txstatsd.tests.counter:9|c')
280+ b'txstatsd.tests.counter:9|c')
281
282 def test_sli(self):
283 """Test SLI call."""
284 self.metrics.sli('users', 100)
285 self.assertEqual(self.connection.data,
286- 'txstatsd.tests.users:100|sli')
287+ b'txstatsd.tests.users:100|sli')
288
289 self.metrics.sli('users', 200, 2)
290 self.assertEqual(self.connection.data,
291- 'txstatsd.tests.users:200|sli|2')
292+ b'txstatsd.tests.users:200|sli|2')
293
294 self.metrics.sli_error('users')
295 self.assertEqual(self.connection.data,
296- 'txstatsd.tests.users:error|sli')
297+ b'txstatsd.tests.users:error|sli')
298
299=== modified file 'txstatsd/tests/test_router.py'
300--- txstatsd/tests/test_router.py 2012-06-28 17:29:26 +0000
301+++ txstatsd/tests/test_router.py 2013-03-08 13:29:21 +0000
302@@ -179,7 +179,7 @@
303
304 class Collect(DatagramProtocol):
305
306- def datagramReceived(cself, data, (host, port)):
307+ def datagramReceived(cself, data, host_port):
308 self.got_data(data)
309
310 self.port = reactor.listenUDP(0, Collect())
311
312=== modified file 'txstatsd/tests/test_service.py'
313--- txstatsd/tests/test_service.py 2012-06-28 17:29:26 +0000
314+++ txstatsd/tests/test_service.py 2013-03-08 13:29:21 +0000
315@@ -20,8 +20,12 @@
316 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
317
318 import tempfile
319-import ConfigParser
320-from StringIO import StringIO
321+try:
322+ import ConfigParser
323+ from StringIO import StringIO
324+except ImportError:
325+ import configparser as ConfigParser
326+ from io import StringIO
327
328 from twisted.trial.unittest import TestCase
329
330@@ -203,7 +207,8 @@
331 def __init__(self):
332 self.monitor_response = None
333
334- def datagramReceived(self, data, (host, port)):
335+ def datagramReceived(self, data, host_port):
336+ host, port = host_port
337 self.monitor_response = data
338
339

Subscribers

People subscribed via source and target branches