__init__.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 pandas._typing import F
  13. from pandas.compat._constants import (
  14. IS64,
  15. PY39,
  16. PY310,
  17. PY311,
  18. PYPY,
  19. )
  20. import pandas.compat.compressors
  21. from pandas.compat.numpy import (
  22. is_numpy_dev,
  23. np_version_under1p21,
  24. )
  25. from pandas.compat.pyarrow import (
  26. pa_version_under7p0,
  27. pa_version_under8p0,
  28. pa_version_under9p0,
  29. pa_version_under11p0,
  30. )
  31. def set_function_name(f: F, name: str, cls) -> F:
  32. """
  33. Bind the name/qualname attributes of the function.
  34. """
  35. f.__name__ = name
  36. f.__qualname__ = f"{cls.__name__}.{name}"
  37. f.__module__ = cls.__module__
  38. return f
  39. def is_platform_little_endian() -> bool:
  40. """
  41. Checking if the running platform is little endian.
  42. Returns
  43. -------
  44. bool
  45. True if the running platform is little endian.
  46. """
  47. return sys.byteorder == "little"
  48. def is_platform_windows() -> bool:
  49. """
  50. Checking if the running platform is windows.
  51. Returns
  52. -------
  53. bool
  54. True if the running platform is windows.
  55. """
  56. return sys.platform in ["win32", "cygwin"]
  57. def is_platform_linux() -> bool:
  58. """
  59. Checking if the running platform is linux.
  60. Returns
  61. -------
  62. bool
  63. True if the running platform is linux.
  64. """
  65. return sys.platform == "linux"
  66. def is_platform_mac() -> bool:
  67. """
  68. Checking if the running platform is mac.
  69. Returns
  70. -------
  71. bool
  72. True if the running platform is mac.
  73. """
  74. return sys.platform == "darwin"
  75. def is_platform_arm() -> bool:
  76. """
  77. Checking if the running platform use ARM architecture.
  78. Returns
  79. -------
  80. bool
  81. True if the running platform uses ARM architecture.
  82. """
  83. return platform.machine() in ("arm64", "aarch64") or platform.machine().startswith(
  84. "armv"
  85. )
  86. def is_platform_power() -> bool:
  87. """
  88. Checking if the running platform use Power architecture.
  89. Returns
  90. -------
  91. bool
  92. True if the running platform uses ARM architecture.
  93. """
  94. return platform.machine() in ("ppc64", "ppc64le")
  95. def is_ci_environment() -> bool:
  96. """
  97. Checking if running in a continuous integration environment by checking
  98. the PANDAS_CI environment variable.
  99. Returns
  100. -------
  101. bool
  102. True if the running in a continuous integration environment.
  103. """
  104. return os.environ.get("PANDAS_CI", "0") == "1"
  105. def get_lzma_file() -> type[pandas.compat.compressors.LZMAFile]:
  106. """
  107. Importing the `LZMAFile` class from the `lzma` module.
  108. Returns
  109. -------
  110. class
  111. The `LZMAFile` class from the `lzma` module.
  112. Raises
  113. ------
  114. RuntimeError
  115. If the `lzma` module was not imported correctly, or didn't exist.
  116. """
  117. if not pandas.compat.compressors.has_lzma:
  118. raise RuntimeError(
  119. "lzma module not available. "
  120. "A Python re-install with the proper dependencies, "
  121. "might be required to solve this issue."
  122. )
  123. return pandas.compat.compressors.LZMAFile
  124. __all__ = [
  125. "is_numpy_dev",
  126. "np_version_under1p21",
  127. "pa_version_under7p0",
  128. "pa_version_under8p0",
  129. "pa_version_under9p0",
  130. "pa_version_under11p0",
  131. "IS64",
  132. "PY39",
  133. "PY310",
  134. "PY311",
  135. "PYPY",
  136. ]