__init__.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. """
  2. compat
  3. ======
  4. Cross-compatible functions for different versions of Python.
  5. Other items:
  6. * platform checker
  7. """
  8. from __future__ import annotations
  9. import os
  10. import platform
  11. import sys
  12. from typing import TYPE_CHECKING
  13. from pandas.compat._constants import (
  14. IS64,
  15. ISMUSL,
  16. PY310,
  17. PY311,
  18. PY312,
  19. PY314,
  20. PYPY,
  21. WARNING_CHECK_DISABLED,
  22. )
  23. import pandas.compat.compressors
  24. from pandas.compat.numpy import is_numpy_dev
  25. from pandas.compat.pyarrow import (
  26. HAS_PYARROW,
  27. pa_version_under10p1,
  28. pa_version_under11p0,
  29. pa_version_under13p0,
  30. pa_version_under14p0,
  31. pa_version_under14p1,
  32. pa_version_under16p0,
  33. pa_version_under17p0,
  34. pa_version_under18p0,
  35. pa_version_under19p0,
  36. pa_version_under20p0,
  37. pa_version_under21p0,
  38. )
  39. if TYPE_CHECKING:
  40. from pandas._typing import F
  41. def set_function_name(f: F, name: str, cls: type) -> F:
  42. """
  43. Bind the name/qualname attributes of the function.
  44. """
  45. f.__name__ = name
  46. f.__qualname__ = f"{cls.__name__}.{name}"
  47. f.__module__ = cls.__module__
  48. return f
  49. def is_platform_little_endian() -> bool:
  50. """
  51. Checking if the running platform is little endian.
  52. Returns
  53. -------
  54. bool
  55. True if the running platform is little endian.
  56. """
  57. return sys.byteorder == "little"
  58. def is_platform_windows() -> bool:
  59. """
  60. Checking if the running platform is windows.
  61. Returns
  62. -------
  63. bool
  64. True if the running platform is windows.
  65. """
  66. return sys.platform in ["win32", "cygwin"]
  67. def is_platform_linux() -> bool:
  68. """
  69. Checking if the running platform is linux.
  70. Returns
  71. -------
  72. bool
  73. True if the running platform is linux.
  74. """
  75. return sys.platform == "linux"
  76. def is_platform_mac() -> bool:
  77. """
  78. Checking if the running platform is mac.
  79. Returns
  80. -------
  81. bool
  82. True if the running platform is mac.
  83. """
  84. return sys.platform == "darwin"
  85. def is_platform_arm() -> bool:
  86. """
  87. Checking if the running platform use ARM architecture.
  88. Returns
  89. -------
  90. bool
  91. True if the running platform uses ARM architecture.
  92. """
  93. return platform.machine() in ("arm64", "aarch64") or platform.machine().startswith(
  94. "armv"
  95. )
  96. def is_platform_power() -> bool:
  97. """
  98. Checking if the running platform use Power architecture.
  99. Returns
  100. -------
  101. bool
  102. True if the running platform uses ARM architecture.
  103. """
  104. return platform.machine() in ("ppc64", "ppc64le")
  105. def is_ci_environment() -> bool:
  106. """
  107. Checking if running in a continuous integration environment by checking
  108. the PANDAS_CI environment variable.
  109. Returns
  110. -------
  111. bool
  112. True if the running in a continuous integration environment.
  113. """
  114. return os.environ.get("PANDAS_CI", "0") == "1"
  115. def get_lzma_file() -> type[pandas.compat.compressors.LZMAFile]:
  116. """
  117. Importing the `LZMAFile` class from the `lzma` module.
  118. Returns
  119. -------
  120. class
  121. The `LZMAFile` class from the `lzma` module.
  122. Raises
  123. ------
  124. RuntimeError
  125. If the `lzma` module was not imported correctly, or didn't exist.
  126. """
  127. if not pandas.compat.compressors.has_lzma:
  128. raise RuntimeError(
  129. "lzma module not available. "
  130. "A Python re-install with the proper dependencies, "
  131. "might be required to solve this issue."
  132. )
  133. return pandas.compat.compressors.LZMAFile
  134. def get_bz2_file() -> type[pandas.compat.compressors.BZ2File]:
  135. """
  136. Importing the `BZ2File` class from the `bz2` module.
  137. Returns
  138. -------
  139. class
  140. The `BZ2File` class from the `bz2` module.
  141. Raises
  142. ------
  143. RuntimeError
  144. If the `bz2` module was not imported correctly, or didn't exist.
  145. """
  146. if not pandas.compat.compressors.has_bz2:
  147. raise RuntimeError(
  148. "bz2 module not available. "
  149. "A Python re-install with the proper dependencies, "
  150. "might be required to solve this issue."
  151. )
  152. return pandas.compat.compressors.BZ2File
  153. __all__ = [
  154. "is_numpy_dev",
  155. "pa_version_under10p1",
  156. "pa_version_under11p0",
  157. "pa_version_under13p0",
  158. "pa_version_under14p0",
  159. "pa_version_under14p1",
  160. "pa_version_under16p0",
  161. "pa_version_under17p0",
  162. "pa_version_under18p0",
  163. "pa_version_under19p0",
  164. "pa_version_under20p0",
  165. "pa_version_under21p0",
  166. "HAS_PYARROW",
  167. "IS64",
  168. "ISMUSL",
  169. "PY310",
  170. "PY311",
  171. "PY312",
  172. "PY314",
  173. "PYPY",
  174. "WARNING_CHECK_DISABLED",
  175. ]