Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

60 lignes
1.5 KiB

  1. import sys
  2. import decimal
  3. from decimal import Context
  4. PY3 = sys.version_info[0] == 3
  5. PY2 = sys.version_info[0] == 2
  6. if PY3:
  7. long = int
  8. xrange = range
  9. else:
  10. long = long # pylint: disable=long-builtin
  11. xrange = xrange # pylint: disable=xrange-builtin
  12. # unicode / binary types
  13. if PY3:
  14. text_type = str
  15. binary_type = bytes
  16. string_types = (str,)
  17. unichr = chr
  18. def maybe_decode(x):
  19. return x.decode()
  20. def maybe_encode(x):
  21. return x.encode()
  22. def maybe_chr(x):
  23. return x
  24. def maybe_ord(x):
  25. return x
  26. else:
  27. text_type = unicode # pylint: disable=unicode-builtin, undefined-variable
  28. binary_type = str
  29. string_types = (
  30. basestring, # pylint: disable=basestring-builtin, undefined-variable
  31. )
  32. unichr = unichr # pylint: disable=unichr-builtin
  33. def maybe_decode(x):
  34. return x
  35. def maybe_encode(x):
  36. return x
  37. def maybe_chr(x):
  38. return chr(x)
  39. def maybe_ord(x):
  40. return ord(x)
  41. def round_py2_compat(what):
  42. """
  43. Python 2 and Python 3 use different rounding strategies in round(). This
  44. function ensures that results are python2/3 compatible and backward
  45. compatible with previous py2 releases
  46. :param what: float
  47. :return: rounded long
  48. """
  49. d = Context(
  50. prec=len(str(long(what))), # round to integer with max precision
  51. rounding=decimal.ROUND_HALF_UP
  52. ).create_decimal(str(what)) # str(): python 2.6 compat
  53. return long(d)