Skip to content

Commit 129cb95

Browse files
committed
Merge pull request #2 from nickj21/python3-compatible
Python3 compatible
2 parents f0b0a44 + 7cb6e92 commit 129cb95

File tree

11 files changed

+113
-80
lines changed

11 files changed

+113
-80
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ ChangeLog
5656
.*sw?
5757
*cscope*
5858
.ropeproject/
59+
.idea/
5960

6061
*.local*
6162
*.json

hdrh/codec.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
See the License for the specific language governing permissions and
2323
limitations under the License.
2424
'''
25+
from __future__ import print_function
26+
from builtins import str
27+
from builtins import range
28+
from builtins import object
2529

2630
import base64
2731
import ctypes
@@ -233,10 +237,10 @@ def compress(self, counts_limit):
233237

234238
def dump(self, label=None):
235239
if label:
236-
print 'Payload Dump ' + label
237-
print ' payload cookie: %x' % (self.payload.cookie)
238-
print ' payload_len: %d' % (self.payload.payload_len)
239-
print ' counts_len: %d' % (self.counts_len)
240+
print('Payload Dump ' + label)
241+
print(' payload cookie: %x' % (self.payload.cookie))
242+
print(' payload_len: %d' % (self.payload.payload_len))
243+
print(' counts_len: %d' % (self.counts_len))
240244
dump_payload(self.get_counts(), self.counts_len)
241245

242246

@@ -297,7 +301,7 @@ def encode(self):
297301
if self.b64_wrap:
298302
self.header.length = len(cpayload)
299303
header_str = ctypes.string_at(addressof(self.header), ext_header_size)
300-
return base64.b64encode(''.join([header_str, cpayload]))
304+
return base64.b64encode(header_str + cpayload)
301305
return cpayload
302306

303307
@staticmethod
@@ -355,19 +359,19 @@ def add(self, other_encoder):
355359
def _dump_series(start, stop, count):
356360
if stop <= start + 1:
357361
# single index range
358-
print '[%06d] %d' % (start, count)
362+
print('[%06d] %d' % (start, count))
359363
else:
360-
print '[%06d] %d (%d identical)' % (start, count, stop - start)
364+
print('[%06d] %d (%d identical)' % (start, count, stop - start))
361365

362366
def dump_payload(counts, max_index):
363-
print 'counts array size = %d entries' % (max_index)
367+
print('counts array size = %d entries' % (max_index))
364368
if not max_index:
365369
return
366370
series_start_index = 0
367371
total_count = 0
368372
current_series_count = counts[0]
369373
index = 0
370-
for index in xrange(1, max_index):
374+
for index in range(1, max_index):
371375
total_count += counts[index]
372376
if counts[index] != current_series_count:
373377
# dump the current series
@@ -377,8 +381,8 @@ def dump_payload(counts, max_index):
377381
series_start_index = index
378382
# there is always a last series to dump
379383
_dump_series(series_start_index, index, counts[index])
380-
print '[%06d] --END-- total count=%d' % (index + 1, total_count)
384+
print('[%06d] --END-- total count=%d' % (index + 1, total_count))
381385

382386
def hex_dump(label, str):
383-
print label
384-
print ':'.join(x.encode('hex') for x in str)
387+
print(label)
388+
print(':'.join(x.encode('hex') for x in str))

hdrh/histogram.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
See the License for the specific language governing permissions and
2020
limitations under the License.
2121
'''
22+
from __future__ import division
23+
from builtins import range
24+
from builtins import object
2225
import math
2326
import sys
2427
from hdrh.iterators import AllValuesIterator
@@ -32,7 +35,7 @@ def get_bucket_count(value, subb_count, unit_mag):
3235
smallest_untrackable_value = subb_count << unit_mag
3336
buckets_needed = 1
3437
while smallest_untrackable_value <= value:
35-
if smallest_untrackable_value > sys.maxint / 2:
38+
if smallest_untrackable_value > sys.maxsize // 2:
3639
return buckets_needed + 1
3740
smallest_untrackable_value <<= 1
3841
buckets_needed += 1
@@ -96,22 +99,20 @@ def __init__(self,
9699
self.lowest_trackable_value = lowest_trackable_value
97100
self.highest_trackable_value = highest_trackable_value
98101
self.significant_figures = significant_figures
99-
self.unit_magnitude = int(math.floor(math.log(lowest_trackable_value) /
100-
math.log(2)))
102+
self.unit_magnitude = int(math.floor(math.log(lowest_trackable_value) / math.log(2)))
101103
largest_value_single_unit_res = 2 * math.pow(10, significant_figures)
102-
subb_count_mag = int(math.ceil(math.log(largest_value_single_unit_res) /
103-
math.log(2)))
104+
subb_count_mag = int(math.ceil(math.log(largest_value_single_unit_res) / math.log(2)))
104105
self.sub_bucket_half_count_magnitude = subb_count_mag - 1 if subb_count_mag > 1 else 0
105106
self.sub_bucket_count = int(math.pow(2, self.sub_bucket_half_count_magnitude + 1))
106-
self.sub_bucket_half_count = self.sub_bucket_count / 2
107+
self.sub_bucket_half_count = self.sub_bucket_count // 2
107108
self.sub_bucket_mask = (self.sub_bucket_count - 1) << self.unit_magnitude
108109
self.bucket_count = get_bucket_count(highest_trackable_value,
109110
self.sub_bucket_count,
110111
self.unit_magnitude)
111-
self.min_value = sys.maxint
112+
self.min_value = sys.maxsize
112113
self.max_value = 0
113114
self.total_count = 0
114-
self.counts_len = (self.bucket_count + 1) * (self.sub_bucket_count / 2)
115+
self.counts_len = (self.bucket_count + 1) * (self.sub_bucket_count // 2)
115116
self.word_size = word_size
116117

117118
if hdr_payload:
@@ -273,7 +274,7 @@ def get_value_at_percentile(self, percentile):
273274
'''
274275
count_at_percentile = self.get_target_count_at_percentile(percentile)
275276
total = 0
276-
for index in xrange(self.counts_len):
277+
for index in range(self.counts_len):
277278
total += self.get_count_at_index(index)
278279
if total >= count_at_percentile:
279280
value_at_index = self.get_value_from_index(index)
@@ -299,7 +300,7 @@ def get_percentile_to_value_dict(self, percentile_list):
299300
percentile_list = list(set(percentile_list))
300301
percentile_list.sort()
301302

302-
for index in xrange(self.counts_len):
303+
for index in range(self.counts_len):
303304
total += self.get_count_at_index(index)
304305
while True:
305306
# recalculate target based on next requested percentile
@@ -347,8 +348,8 @@ def get_max_value(self):
347348
def get_min_value(self):
348349
if 0 < self.counts[0] or self.total_count == 0:
349350
return 0
350-
if sys.maxint == self.min_value:
351-
return sys.maxint
351+
if sys.maxsize == self.min_value:
352+
return sys.maxsize
352353
return self.get_lowest_equivalent_value(self.min_value)
353354

354355
def _hdr_size_of_equiv_value_range(self, value):
@@ -384,10 +385,10 @@ def get_stddev(self):
384385
def reset(self):
385386
'''Reset the histogram to a pristine state
386387
'''
387-
for index in xrange(self.counts_len):
388+
for index in range(self.counts_len):
388389
self.counts[index] = 0
389390
self.total_count = 0
390-
self.min_value = sys.maxint
391+
self.min_value = sys.maxsize
391392
self.max_value = 0
392393

393394
def __iter__(self):
@@ -513,7 +514,7 @@ def add(self, other_hist):
513514
else:
514515
# Arrays are not a direct match, so we can't just stream through and add them.
515516
# Instead, go through the array and add each non-zero value found at it's proper value:
516-
for index in xrange(other_hist.counts_len):
517+
for index in range(other_hist.counts_len):
517518
other_count = other_hist.get_count_at_index(index)
518519
if other_count > 0:
519520
self.record_value(other_hist.get_value_from_index(index), other_count)

hdrh/iterators.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
See the License for the specific language governing permissions and
2222
limitations under the License.
2323
'''
24+
from __future__ import division
25+
from builtins import object
2426
from abc import abstractmethod
2527
import math
2628

@@ -103,7 +105,7 @@ def reset_iterator(self, histogram):
103105
def has_next(self):
104106
return self.total_count_to_current_index < self.total_count
105107

106-
def next(self):
108+
def __next__(self):
107109
if self.total_count != self.histogram.total_count:
108110
raise HdrConcurrentModificationException()
109111
while self.has_next():
@@ -303,8 +305,7 @@ def increment_iteration_level(self):
303305
self.percentile_to_iterate_from = self.percentile_to_iterate_to
304306
percentile_gap = 100.0 - (self.percentile_to_iterate_to)
305307
if percentile_gap:
306-
half_distance = math.pow(2,
307-
(math.log(100 / percentile_gap) / math.log(2)) + 1)
308+
half_distance = math.pow(2, (math.log(100 / percentile_gap) / math.log(2)) + 1)
308309
percentile_reporting_ticks = self.percentile_ticks_per_half_distance * half_distance
309310
self.percentile_to_iterate_to += 100.0 / percentile_reporting_ticks
310311

hdrh/log.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
See the License for the specific language governing permissions and
3030
limitations under the License.
3131
'''
32+
from __future__ import division
33+
from builtins import object
3234
import datetime
3335
import re
3436
import sys
@@ -86,8 +88,8 @@ def output_interval_histogram(self,
8688
self.log.write("%f,%f,%f,%s\n" %
8789
(start_time_stamp_sec,
8890
end_time_stamp_sec - start_time_stamp_sec,
89-
histogram.get_max_value() / max_value_unit_ratio,
90-
cpayload))
91+
histogram.get_max_value() // max_value_unit_ratio,
92+
cpayload.decode('utf-8')))
9193

9294
def output_start_time(self, start_time_msec):
9395
'''Log a start time in the log.
@@ -170,7 +172,7 @@ def get_start_time_sec(self):
170172
def _decode_next_interval_histogram(self,
171173
dest_histogram,
172174
range_start_time_sec=0.0,
173-
range_end_time_sec=sys.maxint,
175+
range_end_time_sec=sys.maxsize,
174176
absolute=False):
175177
'''Read the next interval histogram from the log, if interval falls
176178
within an absolute or relative time range.
@@ -287,7 +289,7 @@ def _decode_next_interval_histogram(self,
287289

288290
def get_next_interval_histogram(self,
289291
range_start_time_sec=0.0,
290-
range_end_time_sec=sys.maxint,
292+
range_end_time_sec=sys.maxsize,
291293
absolute=False):
292294
'''Read the next interval histogram from the log, if interval falls
293295
within an absolute or relative time range.
@@ -336,7 +338,7 @@ def get_next_interval_histogram(self,
336338
def add_next_interval_histogram(self,
337339
dest_histogram=None,
338340
range_start_time_sec=0.0,
339-
range_end_time_sec=sys.maxint,
341+
range_end_time_sec=sys.maxsize,
340342
absolute=False):
341343
'''Read the next interval histogram from the log, if interval falls
342344
within an absolute or relative time range, and add it to the destination

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pbr>=1.4
2+
future>=0.15.2

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ classifier =
1616
Programming Language :: Python
1717
Programming Language :: Python :: 2
1818
Programming Language :: Python :: 2.7
19+
Programming Language :: Python :: 3
1920

2021
[files]
2122
packages =

setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
32
from setuptools import setup
43
from setuptools import Extension
54
from setuptools.command.test import test
@@ -19,11 +18,13 @@ def run_tests(self):
1918
import tox
2019
sys.exit(tox.cmdline())
2120

21+
2222
if __name__ == '__main__':
2323
setup(setup_requires=['pbr'], pbr=True,
24+
install_requires=['future>=0.15.2'],
2425
keywords='hdrhistogram hdr histogram high dynamic range',
2526
tests_require=['tox'],
2627
cmdclass={'test': Tox},
27-
ext_modules = [Extension('pyhdrh',
28-
sources = ['src/python-codec.c'])]
29-
)
28+
ext_modules=[Extension('pyhdrh',
29+
sources=['src/python-codec.c'])]
30+
)

src/python-codec.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ static PyObject *py_hdr_decode(PyObject *self, PyObject *args) {
269269
int min_nonzero_index = -1;
270270
int max_nonzero_index = 0;
271271

272-
if (!PyArg_ParseTuple(args, "t#ilii", &src, &src_len,
272+
if (!PyArg_ParseTuple(args, "s#ilii", &src, &src_len,
273273
&read_index,
274274
&vdst, &max_index,
275275
&word_size)) {
@@ -460,6 +460,24 @@ static PyMethodDef HdrhMethods[] = {
460460
{NULL, NULL, 0, NULL}
461461
};
462462

463-
PyMODINIT_FUNC initpyhdrh(void) {
464-
(void) Py_InitModule("pyhdrh", HdrhMethods);
465-
}
463+
#if PY_MAJOR_VERSION >= 3
464+
static struct PyModuleDef hdrhdef = {
465+
PyModuleDef_HEAD_INIT,
466+
"pyhdrh", /* m_name */
467+
NULL, /* m_doc */
468+
-1, /* m_size */
469+
HdrhMethods, /* m_methods */
470+
NULL, /* m_reload */
471+
NULL, /* m_traverse */
472+
NULL, /* m_clear */
473+
NULL, /* m_free */
474+
};
475+
476+
PyMODINIT_FUNC PyInit_pyhdrh(void) {
477+
return PyModule_Create(&hdrhdef);
478+
}
479+
#else
480+
PyMODINIT_FUNC initpyhdrh(void) {
481+
(void) Py_InitModule("pyhdrh", HdrhMethods);
482+
}
483+
#endif

0 commit comments

Comments
 (0)