compat.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright 2014 Baidu, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  4. # except in compliance with the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the
  9. # License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  10. # either express or implied. See the License for the specific language governing permissions
  11. # and limitations under the License.
  12. """
  13. This module provides string converting tools and compatibility on py2 vs py3
  14. """
  15. import functools
  16. import itertools
  17. import operator
  18. import sys
  19. import types
  20. PY2 = sys.version_info[0]==2
  21. PY3 = sys.version_info[0]==3
  22. if PY3:
  23. string_types = str,
  24. integer_types = int,
  25. class_types = type,
  26. text_type = str
  27. binary_type = bytes
  28. def convert_to_bytes(idata):
  29. """
  30. convert source type idata to bytes string
  31. :type idata: any valid python type
  32. :param idata: source data
  33. :return : bytes string
  34. """
  35. # unicode
  36. if isinstance(idata, str):
  37. return idata.encode(encoding='utf-8')
  38. # Ascii
  39. elif isinstance(idata, bytes):
  40. return idata
  41. # int,dict,list
  42. else:
  43. return str(idata).encode(encoding='utf-8')
  44. def convert_to_string(idata):
  45. """
  46. convert source data to str string on py3
  47. :type idata:any valid python type
  48. :param idata:source data
  49. :return :uniocde string on py3
  50. """
  51. return convert_to_unicode(idata)
  52. def convert_to_unicode(idata):
  53. """
  54. convert source type idata to unicode string
  55. :type idata: any valid python type
  56. :param idata: source data
  57. :return : unicode string
  58. """
  59. # Ascii
  60. if isinstance(idata, bytes):
  61. return idata.decode(encoding='utf-8')
  62. # unicode
  63. elif isinstance(idata, str):
  64. return idata
  65. # int,dict,list
  66. else:
  67. return str(idata)
  68. else: # py2
  69. string_types = basestring,
  70. integer_types = (int, long)
  71. class_types = (type, types.ClassType)
  72. text_type = unicode
  73. binary_type = str
  74. def convert_to_bytes(idata):
  75. """
  76. convert source type idata to bytes string
  77. :type idata: any valid python type
  78. :param idata: source data
  79. :return : bytes string
  80. """
  81. if isinstance(idata, unicode):
  82. return idata.encode(encoding='utf-8')
  83. elif isinstance(idata, str):
  84. return idata
  85. # int ,long, dict, list
  86. else:
  87. return str(idata)
  88. def convert_to_string(idata):
  89. """
  90. convert source data to str string on py2
  91. :type idata:any valid python type
  92. :param idata:source data
  93. :return :bytes string on py2
  94. """
  95. return convert_to_bytes(idata)
  96. def convert_to_unicode(idata):
  97. """
  98. convert source type idata to unicode string
  99. :type idata: any valid python type
  100. :param idata: source data
  101. :return : unicode string
  102. """
  103. if isinstance(idata, str): #Ascii
  104. return idata.decode(encoding='utf-8')
  105. elif isinstance(idata, unicode):
  106. return idata
  107. else:
  108. return str(idata).decode(encoding='utf-8')