Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

1617ed50-3edd-49bc-8389-aca37c386a73 7.1 KB
Raw

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  1. import enum
  2. import numpy as np
  3. from .abstract import Dummy, Hashable, Literal, Number, Type
  4. from functools import total_ordering, cached_property
  5. from numba.core import utils
  6. from numba.core.typeconv import Conversion
  7. from numba.np import npdatetime_helpers
  8. class Boolean(Hashable):
  9. def cast_python_value(self, value):
  10. return bool(value)
  11. def parse_integer_bitwidth(name):
  12. for prefix in ('int', 'uint'):
  13. if name.startswith(prefix):
  14. bitwidth = int(name[len(prefix):])
  15. return bitwidth
  16. def parse_integer_signed(name):
  17. signed = name.startswith('int')
  18. return signed
  19. @total_ordering
  20. class Integer(Number):
  21. def __init__(self, name, bitwidth=None, signed=None):
  22. super(Integer, self).__init__(name)
  23. if bitwidth is None:
  24. bitwidth = parse_integer_bitwidth(name)
  25. if signed is None:
  26. signed = parse_integer_signed(name)
  27. self.bitwidth = bitwidth
  28. self.signed = signed
  29. @classmethod
  30. def from_bitwidth(cls, bitwidth, signed=True):
  31. name = ('int%d' if signed else 'uint%d') % bitwidth
  32. return cls(name)
  33. def cast_python_value(self, value):
  34. return getattr(np, self.name)(value)
  35. def __lt__(self, other):
  36. if self.__class__ is not other.__class__:
  37. return NotImplemented
  38. if self.signed != other.signed:
  39. return NotImplemented
  40. return self.bitwidth < other.bitwidth
  41. @property
  42. def maxval(self):
  43. """
  44. The maximum value representable by this type.
  45. """
  46. if self.signed:
  47. return (1 << (self.bitwidth - 1)) - 1
  48. else:
  49. return (1 << self.bitwidth) - 1
  50. @property
  51. def minval(self):
  52. """
  53. The minimal value representable by this type.
  54. """
  55. if self.signed:
  56. return -(1 << (self.bitwidth - 1))
  57. else:
  58. return 0
  59. class IntegerLiteral(Literal, Integer):
  60. def __init__(self, value):
  61. self._literal_init(value)
  62. name = 'Literal[int]({})'.format(value)
  63. basetype = self.literal_type
  64. Integer.__init__(
  65. self,
  66. name=name,
  67. bitwidth=basetype.bitwidth,
  68. signed=basetype.signed,
  69. )
  70. def can_convert_to(self, typingctx, other):
  71. conv = typingctx.can_convert(self.literal_type, other)
  72. if conv is not None:
  73. return max(conv, Conversion.promote)
  74. Literal.ctor_map[int] = IntegerLiteral
  75. class BooleanLiteral(Literal, Boolean):
  76. def __init__(self, value):
  77. self._literal_init(value)
  78. name = 'Literal[bool]({})'.format(value)
  79. Boolean.__init__(
  80. self,
  81. name=name
  82. )
  83. def can_convert_to(self, typingctx, other):
  84. conv = typingctx.can_convert(self.literal_type, other)
  85. if conv is not None:
  86. return max(conv, Conversion.promote)
  87. Literal.ctor_map[bool] = BooleanLiteral
  88. @total_ordering
  89. class Float(Number):
  90. def __init__(self, *args, **kws):
  91. super(Float, self).__init__(*args, **kws)
  92. # Determine bitwidth
  93. assert self.name.startswith('float')
  94. bitwidth = int(self.name[5:])
  95. self.bitwidth = bitwidth
  96. def cast_python_value(self, value):
  97. return getattr(np, self.name)(value)
  98. def __lt__(self, other):
  99. if self.__class__ is not other.__class__:
  100. return NotImplemented
  101. return self.bitwidth < other.bitwidth
  102. @total_ordering
  103. class Complex(Number):
  104. def __init__(self, name, underlying_float, **kwargs):
  105. super(Complex, self).__init__(name, **kwargs)
  106. self.underlying_float = underlying_float
  107. # Determine bitwidth
  108. assert self.name.startswith('complex')
  109. bitwidth = int(self.name[7:])
  110. self.bitwidth = bitwidth
  111. def cast_python_value(self, value):
  112. return getattr(np, self.name)(value)
  113. def __lt__(self, other):
  114. if self.__class__ is not other.__class__:
  115. return NotImplemented
  116. return self.bitwidth < other.bitwidth
  117. class _NPDatetimeBase(Type):
  118. """
  119. Common base class for np.datetime64 and np.timedelta64.
  120. """
  121. def __init__(self, unit, *args, **kws):
  122. name = '%s[%s]' % (self.type_name, unit)
  123. self.unit = unit
  124. self.unit_code = npdatetime_helpers.DATETIME_UNITS[self.unit]
  125. super(_NPDatetimeBase, self).__init__(name, *args, **kws)
  126. def __lt__(self, other):
  127. if self.__class__ is not other.__class__:
  128. return NotImplemented
  129. # A coarser-grained unit is "smaller", i.e. less precise values
  130. # can be represented (but the magnitude of representable values is
  131. # also greater...).
  132. return self.unit_code < other.unit_code
  133. def cast_python_value(self, value):
  134. cls = getattr(np, self.type_name)
  135. if self.unit:
  136. return cls(value, self.unit)
  137. else:
  138. return cls(value)
  139. @total_ordering
  140. class NPTimedelta(_NPDatetimeBase):
  141. type_name = 'timedelta64'
  142. @total_ordering
  143. class NPDatetime(_NPDatetimeBase):
  144. type_name = 'datetime64'
  145. class EnumClass(Dummy):
  146. """
  147. Type class for Enum classes.
  148. """
  149. basename = "Enum class"
  150. def __init__(self, cls, dtype):
  151. assert isinstance(cls, type)
  152. assert isinstance(dtype, Type)
  153. self.instance_class = cls
  154. self.dtype = dtype
  155. name = "%s<%s>(%s)" % (self.basename, self.dtype, self.instance_class.__name__)
  156. super(EnumClass, self).__init__(name)
  157. @property
  158. def key(self):
  159. return self.instance_class, self.dtype
  160. @cached_property
  161. def member_type(self):
  162. """
  163. The type of this class' members.
  164. """
  165. return EnumMember(self.instance_class, self.dtype)
  166. class IntEnumClass(EnumClass):
  167. """
  168. Type class for IntEnum classes.
  169. """
  170. basename = "IntEnum class"
  171. @cached_property
  172. def member_type(self):
  173. """
  174. The type of this class' members.
  175. """
  176. return IntEnumMember(self.instance_class, self.dtype)
  177. class EnumMember(Type):
  178. """
  179. Type class for Enum members.
  180. """
  181. basename = "Enum"
  182. class_type_class = EnumClass
  183. def __init__(self, cls, dtype):
  184. assert isinstance(cls, type)
  185. assert isinstance(dtype, Type)
  186. self.instance_class = cls
  187. self.dtype = dtype
  188. name = "%s<%s>(%s)" % (self.basename, self.dtype, self.instance_class.__name__)
  189. super(EnumMember, self).__init__(name)
  190. @property
  191. def key(self):
  192. return self.instance_class, self.dtype
  193. @property
  194. def class_type(self):
  195. """
  196. The type of this member's class.
  197. """
  198. return self.class_type_class(self.instance_class, self.dtype)
  199. class IntEnumMember(EnumMember):
  200. """
  201. Type class for IntEnum members.
  202. """
  203. basename = "IntEnum"
  204. class_type_class = IntEnumClass
  205. def can_convert_to(self, typingctx, other):
  206. """
  207. Convert IntEnum members to plain integers.
  208. """
  209. if issubclass(self.instance_class, enum.IntEnum):
  210. conv = typingctx.can_convert(self.dtype, other)
  211. return max(conv, Conversion.safe)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...