scalars.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import sys
  2. import datetime as dt
  3. import pytest
  4. import numpy as np
  5. b = np.bool_()
  6. u8 = np.uint64()
  7. i8 = np.int64()
  8. f8 = np.float64()
  9. c16 = np.complex128()
  10. U = np.str_()
  11. S = np.bytes_()
  12. # Construction
  13. class D:
  14. def __index__(self) -> int:
  15. return 0
  16. class C:
  17. def __complex__(self) -> complex:
  18. return 3j
  19. class B:
  20. def __int__(self) -> int:
  21. return 4
  22. class A:
  23. def __float__(self) -> float:
  24. return 4.0
  25. np.complex64(3j)
  26. np.complex64(A())
  27. np.complex64(C())
  28. np.complex128(3j)
  29. np.complex128(C())
  30. np.complex128(None)
  31. np.complex64("1.2")
  32. np.complex128(b"2j")
  33. np.int8(4)
  34. np.int16(3.4)
  35. np.int32(4)
  36. np.int64(-1)
  37. np.uint8(B())
  38. np.uint32()
  39. np.int32("1")
  40. np.int64(b"2")
  41. np.float16(A())
  42. np.float32(16)
  43. np.float64(3.0)
  44. np.float64(None)
  45. np.float32("1")
  46. np.float16(b"2.5")
  47. np.uint64(D())
  48. np.float32(D())
  49. np.complex64(D())
  50. np.bytes_(b"hello")
  51. np.bytes_("hello", 'utf-8')
  52. np.bytes_("hello", encoding='utf-8')
  53. np.str_("hello")
  54. np.str_(b"hello", 'utf-8')
  55. np.str_(b"hello", encoding='utf-8')
  56. # Array-ish semantics
  57. np.int8().real
  58. np.int16().imag
  59. np.int32().data
  60. np.int64().flags
  61. np.uint8().itemsize * 2
  62. np.uint16().ndim + 1
  63. np.uint32().strides
  64. np.uint64().shape
  65. # Time structures
  66. np.datetime64()
  67. np.datetime64(0, "D")
  68. np.datetime64(0, b"D")
  69. np.datetime64(0, ('ms', 3))
  70. np.datetime64("2019")
  71. np.datetime64(b"2019")
  72. np.datetime64("2019", "D")
  73. np.datetime64(np.datetime64())
  74. np.datetime64(dt.datetime(2000, 5, 3))
  75. np.datetime64(dt.date(2000, 5, 3))
  76. np.datetime64(None)
  77. np.datetime64(None, "D")
  78. np.timedelta64()
  79. np.timedelta64(0)
  80. np.timedelta64(0, "D")
  81. np.timedelta64(0, ('ms', 3))
  82. np.timedelta64(0, b"D")
  83. np.timedelta64("3")
  84. np.timedelta64(b"5")
  85. np.timedelta64(np.timedelta64(2))
  86. np.timedelta64(dt.timedelta(2))
  87. np.timedelta64(None)
  88. np.timedelta64(None, "D")
  89. np.void(1)
  90. np.void(np.int64(1))
  91. np.void(True)
  92. np.void(np.bool_(True))
  93. np.void(b"test")
  94. np.void(np.bytes_("test"))
  95. np.void(object(), [("a", "O"), ("b", "O")])
  96. np.void(object(), dtype=[("a", "O"), ("b", "O")])
  97. # Protocols
  98. i8 = np.int64()
  99. u8 = np.uint64()
  100. f8 = np.float64()
  101. c16 = np.complex128()
  102. b_ = np.bool_()
  103. td = np.timedelta64()
  104. U = np.str_("1")
  105. S = np.bytes_("1")
  106. AR = np.array(1, dtype=np.float64)
  107. int(i8)
  108. int(u8)
  109. int(f8)
  110. int(b_)
  111. int(td)
  112. int(U)
  113. int(S)
  114. int(AR)
  115. with pytest.warns(np.ComplexWarning):
  116. int(c16)
  117. float(i8)
  118. float(u8)
  119. float(f8)
  120. float(b_)
  121. float(td)
  122. float(U)
  123. float(S)
  124. float(AR)
  125. with pytest.warns(np.ComplexWarning):
  126. float(c16)
  127. complex(i8)
  128. complex(u8)
  129. complex(f8)
  130. complex(c16)
  131. complex(b_)
  132. complex(td)
  133. complex(U)
  134. complex(AR)
  135. # Misc
  136. c16.dtype
  137. c16.real
  138. c16.imag
  139. c16.real.real
  140. c16.real.imag
  141. c16.ndim
  142. c16.size
  143. c16.itemsize
  144. c16.shape
  145. c16.strides
  146. c16.squeeze()
  147. c16.byteswap()
  148. c16.transpose()
  149. # Aliases
  150. np.string_()
  151. np.byte()
  152. np.short()
  153. np.intc()
  154. np.intp()
  155. np.int_()
  156. np.longlong()
  157. np.ubyte()
  158. np.ushort()
  159. np.uintc()
  160. np.uintp()
  161. np.uint()
  162. np.ulonglong()
  163. np.half()
  164. np.single()
  165. np.double()
  166. np.float_()
  167. np.longdouble()
  168. np.longfloat()
  169. np.csingle()
  170. np.singlecomplex()
  171. np.cdouble()
  172. np.complex_()
  173. np.cfloat()
  174. np.clongdouble()
  175. np.clongfloat()
  176. np.longcomplex()
  177. b.item()
  178. i8.item()
  179. u8.item()
  180. f8.item()
  181. c16.item()
  182. U.item()
  183. S.item()
  184. b.tolist()
  185. i8.tolist()
  186. u8.tolist()
  187. f8.tolist()
  188. c16.tolist()
  189. U.tolist()
  190. S.tolist()
  191. b.ravel()
  192. i8.ravel()
  193. u8.ravel()
  194. f8.ravel()
  195. c16.ravel()
  196. U.ravel()
  197. S.ravel()
  198. b.flatten()
  199. i8.flatten()
  200. u8.flatten()
  201. f8.flatten()
  202. c16.flatten()
  203. U.flatten()
  204. S.flatten()
  205. b.reshape(1)
  206. i8.reshape(1)
  207. u8.reshape(1)
  208. f8.reshape(1)
  209. c16.reshape(1)
  210. U.reshape(1)
  211. S.reshape(1)