1 import sys
2 import decimal
3 from decimal import Context
4
5 PY3 = sys.version_info[0] == 3
6 PY2 = sys.version_info[0] == 2
7
8
9 if PY3:
10 long = int
11 xrange = range
12 else:
13 long = long
14 xrange = xrange
15
16
17 if PY3:
18 text_type = str
19 binary_type = bytes
20 string_types = (str,)
21 unichr = chr
30 else:
31 text_type = unicode
32 binary_type = str
33 string_types = (
34 basestring,
35 )
36 unichr = unichr
45
46
48 """
49 Python 2 and Python 3 use different rounding strategies in round(). This
50 function ensures that results are python2/3 compatible and backward
51 compatible with previous py2 releases
52 :param what: float
53 :return: rounded long
54 """
55 d = Context(
56 prec=len(str(long(what))),
57 rounding=decimal.ROUND_HALF_UP
58 ).create_decimal(str(what))
59 return long(d)
60