m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.9 KiB

6 months ago
  1. # Human friendly input/output in Python.
  2. #
  3. # Author: Peter Odding <peter@peterodding.com>
  4. # Last Change: September 17, 2021
  5. # URL: https://humanfriendly.readthedocs.io
  6. """
  7. Compatibility with Python 2 and 3.
  8. This module exposes aliases and functions that make it easier to write Python
  9. code that is compatible with Python 2 and Python 3.
  10. .. data:: basestring
  11. Alias for :func:`python2:basestring` (in Python 2) or :class:`python3:str`
  12. (in Python 3). See also :func:`is_string()`.
  13. .. data:: HTMLParser
  14. Alias for :class:`python2:HTMLParser.HTMLParser` (in Python 2) or
  15. :class:`python3:html.parser.HTMLParser` (in Python 3).
  16. .. data:: interactive_prompt
  17. Alias for :func:`python2:raw_input()` (in Python 2) or
  18. :func:`python3:input()` (in Python 3).
  19. .. data:: StringIO
  20. Alias for :class:`python2:StringIO.StringIO` (in Python 2) or
  21. :class:`python3:io.StringIO` (in Python 3).
  22. .. data:: unicode
  23. Alias for :func:`python2:unicode` (in Python 2) or :class:`python3:str` (in
  24. Python 3). See also :func:`coerce_string()`.
  25. .. data:: monotonic
  26. Alias for :func:`python3:time.monotonic()` (in Python 3.3 and higher) or
  27. `monotonic.monotonic()` (a `conditional dependency
  28. <https://pypi.org/project/monotonic/>`_ on older Python versions).
  29. """
  30. __all__ = (
  31. 'HTMLParser',
  32. 'StringIO',
  33. 'basestring',
  34. 'coerce_string',
  35. 'interactive_prompt',
  36. 'is_string',
  37. 'is_unicode',
  38. 'monotonic',
  39. 'name2codepoint',
  40. 'on_macos',
  41. 'on_windows',
  42. 'unichr',
  43. 'unicode',
  44. 'which',
  45. )
  46. # Standard library modules.
  47. import sys
  48. # Differences between Python 2 and 3.
  49. try:
  50. # Python 2.
  51. unicode = unicode
  52. unichr = unichr
  53. basestring = basestring
  54. interactive_prompt = raw_input
  55. from distutils.spawn import find_executable as which
  56. from HTMLParser import HTMLParser
  57. from StringIO import StringIO
  58. from htmlentitydefs import name2codepoint
  59. except (ImportError, NameError):
  60. # Python 3.
  61. unicode = str
  62. unichr = chr
  63. basestring = str
  64. interactive_prompt = input
  65. from shutil import which
  66. from html.parser import HTMLParser
  67. from io import StringIO
  68. from html.entities import name2codepoint
  69. try:
  70. # Python 3.3 and higher.
  71. from time import monotonic
  72. except ImportError:
  73. # A replacement for older Python versions:
  74. # https://pypi.org/project/monotonic/
  75. try:
  76. from monotonic import monotonic
  77. except (ImportError, RuntimeError):
  78. # We fall back to the old behavior of using time.time() instead of
  79. # failing when {time,monotonic}.monotonic() are both missing.
  80. from time import time as monotonic
  81. def coerce_string(value):
  82. """
  83. Coerce any value to a Unicode string (:func:`python2:unicode` in Python 2 and :class:`python3:str` in Python 3).
  84. :param value: The value to coerce.
  85. :returns: The value coerced to a Unicode string.
  86. """
  87. return value if is_string(value) else unicode(value)
  88. def is_string(value):
  89. """
  90. Check if a value is a :func:`python2:basestring` (in Python 2) or :class:`python3:str` (in Python 3) object.
  91. :param value: The value to check.
  92. :returns: :data:`True` if the value is a string, :data:`False` otherwise.
  93. """
  94. return isinstance(value, basestring)
  95. def is_unicode(value):
  96. """
  97. Check if a value is a :func:`python2:unicode` (in Python 2) or :class:`python2:str` (in Python 3) object.
  98. :param value: The value to check.
  99. :returns: :data:`True` if the value is a Unicode string, :data:`False` otherwise.
  100. """
  101. return isinstance(value, unicode)
  102. def on_macos():
  103. """
  104. Check if we're running on Apple MacOS.
  105. :returns: :data:`True` if running MacOS, :data:`False` otherwise.
  106. """
  107. return sys.platform.startswith('darwin')
  108. def on_windows():
  109. """
  110. Check if we're running on the Microsoft Windows OS.
  111. :returns: :data:`True` if running Windows, :data:`False` otherwise.
  112. """
  113. return sys.platform.startswith('win')