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.

86 lines
2.5 KiB

6 months ago
  1. # Copyright 2016 Google Inc. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """ A tiny version of `six` to help with backwards compability. Also includes
  15. compatibility helpers for numpy. """
  16. import sys
  17. PY2 = sys.version_info[0] == 2
  18. PY26 = sys.version_info[0:2] == (2, 6)
  19. PY27 = sys.version_info[0:2] == (2, 7)
  20. PY275 = sys.version_info[0:3] >= (2, 7, 5)
  21. PY3 = sys.version_info[0] == 3
  22. PY34 = sys.version_info[0:2] >= (3, 4)
  23. if PY3:
  24. import importlib.machinery
  25. string_types = (str,)
  26. binary_types = (bytes,bytearray)
  27. range_func = range
  28. memoryview_type = memoryview
  29. struct_bool_decl = "?"
  30. else:
  31. import imp
  32. string_types = (unicode,)
  33. if PY26 or PY27:
  34. binary_types = (str,bytearray)
  35. else:
  36. binary_types = (str,)
  37. range_func = xrange
  38. if PY26 or (PY27 and not PY275):
  39. memoryview_type = buffer
  40. struct_bool_decl = "<b"
  41. else:
  42. memoryview_type = memoryview
  43. struct_bool_decl = "?"
  44. # Helper functions to facilitate making numpy optional instead of required
  45. def import_numpy():
  46. """
  47. Returns the numpy module if it exists on the system,
  48. otherwise returns None.
  49. """
  50. if PY3:
  51. numpy_exists = (
  52. importlib.machinery.PathFinder.find_spec('numpy') is not None)
  53. else:
  54. try:
  55. imp.find_module('numpy')
  56. numpy_exists = True
  57. except ImportError:
  58. numpy_exists = False
  59. if numpy_exists:
  60. # We do this outside of try/except block in case numpy exists
  61. # but is not installed correctly. We do not want to catch an
  62. # incorrect installation which would manifest as an
  63. # ImportError.
  64. import numpy as np
  65. else:
  66. np = None
  67. return np
  68. class NumpyRequiredForThisFeature(RuntimeError):
  69. """
  70. Error raised when user tries to use a feature that
  71. requires numpy without having numpy installed.
  72. """
  73. pass
  74. # NOTE: Future Jython support may require code here (look at `six`).