__init__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. """
  2. compat
  3. ======
  4. Cross-compatible functions for different versions of Python.
  5. Other items:
  6. * platform checker
  7. """
  8. import platform
  9. import struct
  10. import sys
  11. import warnings
  12. PY37 = sys.version_info >= (3, 7)
  13. PY38 = sys.version_info >= (3, 8)
  14. PYPY = platform.python_implementation() == "PyPy"
  15. # ----------------------------------------------------------------------------
  16. # functions largely based / taken from the six module
  17. # Much of the code in this module comes from Benjamin Peterson's six library.
  18. # The license for this library can be found in LICENSES/SIX and the code can be
  19. # found at https://bitbucket.org/gutworth/six
  20. def set_function_name(f, name, cls):
  21. """
  22. Bind the name/qualname attributes of the function.
  23. """
  24. f.__name__ = name
  25. f.__qualname__ = f"{cls.__name__}.{name}"
  26. f.__module__ = cls.__module__
  27. return f
  28. # https://github.com/pandas-dev/pandas/pull/9123
  29. def is_platform_little_endian() -> bool:
  30. """
  31. Checking if the running platform is little endian.
  32. Returns
  33. -------
  34. bool
  35. True if the running platform is little endian.
  36. """
  37. return sys.byteorder == "little"
  38. def is_platform_windows() -> bool:
  39. """
  40. Checking if the running platform is windows.
  41. Returns
  42. -------
  43. bool
  44. True if the running platform is windows.
  45. """
  46. return sys.platform == "win32" or sys.platform == "cygwin"
  47. def is_platform_linux() -> bool:
  48. """
  49. Checking if the running platform is linux.
  50. Returns
  51. -------
  52. bool
  53. True if the running platform is linux.
  54. """
  55. return sys.platform == "linux2"
  56. def is_platform_mac() -> bool:
  57. """
  58. Checking if the running platform is mac.
  59. Returns
  60. -------
  61. bool
  62. True if the running platform is mac.
  63. """
  64. return sys.platform == "darwin"
  65. def is_platform_32bit() -> bool:
  66. """
  67. Checking if the running platform is 32-bit.
  68. Returns
  69. -------
  70. bool
  71. True if the running platform is 32-bit.
  72. """
  73. return struct.calcsize("P") * 8 < 64
  74. def _import_lzma():
  75. """
  76. Importing the `lzma` module.
  77. Warns
  78. -----
  79. When the `lzma` module is not available.
  80. """
  81. try:
  82. import lzma
  83. return lzma
  84. except ImportError:
  85. msg = (
  86. "Could not import the lzma module. "
  87. "Your installed Python is incomplete. "
  88. "Attempting to use lzma compression will result in a RuntimeError."
  89. )
  90. warnings.warn(msg)
  91. def _get_lzma_file(lzma):
  92. """
  93. Importing the `LZMAFile` class from the `lzma` module.
  94. Returns
  95. -------
  96. class
  97. The `LZMAFile` class from the `lzma` module.
  98. Raises
  99. ------
  100. RuntimeError
  101. If the `lzma` module was not imported correctly, or didn't exist.
  102. """
  103. if lzma is None:
  104. raise RuntimeError(
  105. "lzma module not available. "
  106. "A Python re-install with the proper dependencies, "
  107. "might be required to solve this issue."
  108. )
  109. return lzma.LZMAFile