1 import sys
2 import decimal
3 from decimal import Context
4
5 if sys.version_info > (3,):
6 long = int
7 xrange = range
8 else:
9 long = long
10 xrange = xrange
11
12
13 if sys.version_info > (3,):
14 text_type = str
15 binary_type = bytes
16 string_types = (str,)
17 unichr = chr
22 else:
23 text_type = unicode
24 binary_type = str
25 string_types = (
26 basestring,
27 )
28 unichr = unichr
33
34
36 """
37 Python 2 and Python 3 use different rounding strategies in round(). This
38 function ensures that results are python2/3 compatible and backward
39 compatible with previous py2 releases
40 :param what: float
41 :return: rounded long
42 """
43 d = Context(
44 prec=len(str(long(what))),
45 rounding=decimal.ROUND_HALF_UP
46 ).create_decimal(str(what))
47 return long(d)
48