tea-bond 0.3.11__cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of tea-bond might be problematic. Click here for more details.

@@ -0,0 +1,415 @@
1
+ import datetime
2
+ import operator
3
+
4
+ from llvmlite import ir
5
+ from numba import types
6
+ from numba.core import cgutils
7
+ from numba.extending import (
8
+ NativeValue,
9
+ as_numba_type,
10
+ box,
11
+ lower_builtin,
12
+ make_attribute_wrapper,
13
+ models,
14
+ overload,
15
+ overload_attribute,
16
+ overload_method,
17
+ register_model,
18
+ type_callable,
19
+ typeof_impl,
20
+ unbox,
21
+ )
22
+
23
+ from pybond.ffi import (
24
+ datetime_add_duration,
25
+ datetime_sub_duration,
26
+ get_datetime_day,
27
+ get_datetime_hour,
28
+ get_datetime_minute,
29
+ get_datetime_month,
30
+ get_datetime_nanosecond,
31
+ get_datetime_second,
32
+ get_datetime_year,
33
+ timestamp_nanos,
34
+ )
35
+
36
+ from .ir_utils import (
37
+ ir_build_datetime,
38
+ ir_isinstance,
39
+ ir_local_timestamp_nanos,
40
+ ir_timestamp_nanos,
41
+ )
42
+ from .nb_duration import DurationType
43
+ from .nb_time import Time
44
+
45
+
46
+ class DateTime:
47
+ def __init__(self, val: int):
48
+ self.ptr = 0
49
+ self.val = int(val)
50
+
51
+ @property
52
+ def _pydt(self) -> datetime.datetime:
53
+ return datetime.datetime.fromtimestamp(self.val / 1e9)
54
+
55
+ @property
56
+ def time(self) -> datetime.time:
57
+ return self._pydt.time
58
+
59
+ @property
60
+ def year(self) -> int:
61
+ return self.pydt.year
62
+
63
+ @property
64
+ def month(self) -> int:
65
+ return self._pydt.month
66
+
67
+ @property
68
+ def day(self) -> int:
69
+ return self._pydt.day
70
+
71
+ @property
72
+ def hour(self) -> int:
73
+ return self._pydt.hour
74
+
75
+ @property
76
+ def minute(self) -> int:
77
+ return self._pydt.minute
78
+
79
+ @property
80
+ def second(self) -> int:
81
+ return self._pydt.second
82
+
83
+ @property
84
+ def nanosecond(self) -> int:
85
+ return int(self.val % 1e9)
86
+
87
+ def __str__(self):
88
+ return str(self._pydt)
89
+
90
+ def __repr__(self):
91
+ return repr(self._pydt)
92
+
93
+ def __ge__(self, other):
94
+ return self.val >= other.val
95
+
96
+ def __gt__(self, other):
97
+ return self.val > other.val
98
+
99
+ def __lt__(self, other):
100
+ return self.val < other.val
101
+
102
+ def __le__(self, other):
103
+ return self.val <= other.val
104
+
105
+ def __eq__(self, other):
106
+ return self.val == other.val
107
+
108
+ def __ne__(self, other):
109
+ return self.val != other.val
110
+
111
+
112
+ class DateTimeType(types.Type):
113
+ def __init__(self):
114
+ super().__init__(name="DateTime")
115
+
116
+
117
+ datetime_type = DateTimeType()
118
+ as_numba_type.register(datetime.datetime, datetime_type)
119
+
120
+
121
+ @typeof_impl.register(datetime.datetime)
122
+ @typeof_impl.register(DateTime)
123
+ def typeof_index(val, c):
124
+ return datetime_type
125
+
126
+
127
+ @type_callable(DateTime)
128
+ def type_datetime(context):
129
+ def typer(val):
130
+ return datetime_type
131
+
132
+ return typer
133
+
134
+
135
+ @register_model(DateTimeType)
136
+ class DateTimeModel(models.StructModel):
137
+ def __init__(self, dmm, fe_type):
138
+ members = [
139
+ ("ptr", types.voidptr),
140
+ ("val", types.int64),
141
+ ]
142
+ models.StructModel.__init__(self, dmm, fe_type, members)
143
+
144
+
145
+ make_attribute_wrapper(DateTimeType, "ptr", "ptr")
146
+ make_attribute_wrapper(DateTimeType, "val", "val")
147
+
148
+
149
+ @lower_builtin(DateTime, types.int64)
150
+ @lower_builtin(DateTime, types.float64)
151
+ def impl_datetime_builder(context, builder, sig, args):
152
+ typ = sig.return_type
153
+ (val,) = args
154
+ if isinstance(val.type, ir.DoubleType):
155
+ val = builder.fptosi(val, ir.IntType(64))
156
+ ptr = ir_build_datetime(val, builder)
157
+ datetime_struct = cgutils.create_struct_proxy(typ)(context, builder)
158
+ datetime_struct.ptr = ptr
159
+ datetime_struct.val = val
160
+ return datetime_struct._getvalue()
161
+
162
+
163
+ @unbox(DateTimeType)
164
+ def unbox_datetime(typ, obj, c):
165
+ """
166
+ Convert a Python datetime object or DateTime object to a native datetime structure.
167
+ """
168
+ # Get datetime.datetime type object
169
+ datetime_type_c = c.pyapi.unserialize(c.pyapi.serialize_object(datetime.datetime))
170
+
171
+ # Check if object is instance of datetime.datetime
172
+ is_datetime = ir_isinstance(c.pyapi, obj, datetime_type_c)
173
+ c.pyapi.decref(datetime_type_c)
174
+
175
+ val_ptr = cgutils.alloca_once(c.builder, ir.IntType(64))
176
+ ptr_ptr = cgutils.alloca_once(c.builder, ir.PointerType(ir.IntType(8)))
177
+
178
+ with c.builder.if_else(is_datetime) as (then, otherwise):
179
+ with then:
180
+ # Handle datetime.datetime
181
+ ts_obj = c.pyapi.call_method(obj, "timestamp")
182
+ val = c.pyapi.float_as_double(ts_obj)
183
+ val = c.builder.fmul(
184
+ val, ir.Constant(ir.DoubleType(), 1e9)
185
+ ) # Convert to nanoseconds
186
+ val = c.builder.fptosi(val, ir.IntType(64)) # Convert to int64
187
+ c.pyapi.decref(ts_obj)
188
+ ptr = ir_build_datetime(val, c.builder, from_utc=True)
189
+ c.builder.store(ptr, ptr_ptr)
190
+ c.builder.store(ir_timestamp_nanos(ptr, c.builder), val_ptr)
191
+
192
+ with otherwise:
193
+ # Handle DateTime object
194
+ val_obj = c.pyapi.object_getattr_string(obj, "val")
195
+ val = c.pyapi.long_as_longlong(val_obj)
196
+ c.pyapi.decref(val_obj)
197
+ ptr = ir_build_datetime(val, c.builder, from_utc=False)
198
+ c.builder.store(ptr, ptr_ptr)
199
+ c.builder.store(val, val_ptr)
200
+
201
+ ptr = c.builder.load(ptr_ptr)
202
+ val = c.builder.load(val_ptr)
203
+
204
+ # Build the datetime struct with the computed timestamp value
205
+ datetime_struct = cgutils.create_struct_proxy(typ)(c.context, c.builder)
206
+ datetime_struct.ptr = ptr
207
+ datetime_struct.val = val
208
+
209
+ # Check for errors
210
+ is_error = cgutils.is_not_null(c.builder, c.pyapi.err_occurred())
211
+ return NativeValue(datetime_struct._getvalue(), is_error=is_error)
212
+
213
+
214
+ @box(DateTimeType)
215
+ def box_datetime(typ, val, c):
216
+ """
217
+ Convert a native Datetime structure to python datetime.datetime.
218
+ """
219
+ dt = cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)
220
+ utc_timestamp_val = ir_local_timestamp_nanos(dt.ptr, c.builder)
221
+ val = c.builder.sitofp(utc_timestamp_val, ir.DoubleType()) # Convert to double
222
+ val = c.builder.fdiv(val, ir.Constant(ir.DoubleType(), 1e9)) # Convert to seconds
223
+ # Call datetime.datetime.fromtimestamp()
224
+ val_obj = c.pyapi.float_from_double(val)
225
+ class_obj = c.pyapi.unserialize(c.pyapi.serialize_object(datetime.datetime))
226
+ res = c.pyapi.call_method(class_obj, "fromtimestamp", (val_obj,))
227
+ c.pyapi.decref(val_obj)
228
+ c.pyapi.decref(class_obj)
229
+ return res
230
+
231
+
232
+ @overload_attribute(DateTimeType, "timestamp_nanos")
233
+ def get_timestamp_nanos_impl(dt):
234
+ def getter(dt):
235
+ return timestamp_nanos(dt.ptr)
236
+
237
+ return getter
238
+
239
+
240
+ @overload_attribute(DateTimeType, "year")
241
+ def get_year_impl(dt):
242
+ def getter(dt):
243
+ return get_datetime_year(dt.ptr)
244
+
245
+ return getter
246
+
247
+
248
+ @overload_attribute(DateTimeType, "month")
249
+ def get_month_impl(dt):
250
+ def getter(dt):
251
+ return get_datetime_month(dt.ptr)
252
+
253
+ return getter
254
+
255
+
256
+ @overload_attribute(DateTimeType, "day")
257
+ def get_day_impl(dt):
258
+ def getter(dt):
259
+ return get_datetime_day(dt.ptr)
260
+
261
+ return getter
262
+
263
+
264
+ @overload_attribute(DateTimeType, "hour")
265
+ def get_hour_impl(dt):
266
+ def getter(dt):
267
+ return get_datetime_hour(dt.ptr)
268
+
269
+ return getter
270
+
271
+
272
+ @overload_attribute(DateTimeType, "minute")
273
+ def get_minute_impl(dt):
274
+ def getter(dt):
275
+ return get_datetime_minute(dt.ptr)
276
+
277
+ return getter
278
+
279
+
280
+ @overload_attribute(DateTimeType, "second")
281
+ def get_second_impl(dt):
282
+ def getter(dt):
283
+ return get_datetime_second(dt.ptr)
284
+
285
+ return getter
286
+
287
+
288
+ @overload_attribute(DateTimeType, "nanosecond")
289
+ def get_nanosecond_impl(dt):
290
+ def getter(dt):
291
+ return get_datetime_nanosecond(dt.ptr)
292
+
293
+ return getter
294
+
295
+
296
+ @overload_attribute(DateTimeType, "date")
297
+ def get_date_impl(dt):
298
+ def getter(dt):
299
+ return datetime.date(dt.year, dt.month, dt.day)
300
+
301
+ return getter
302
+
303
+
304
+ @overload_attribute(DateTimeType, "time")
305
+ def get_time_impl(dt):
306
+ def getter(dt):
307
+ return Time(dt.hour, dt.minute, dt.second, dt.nanosecond)
308
+
309
+ return getter
310
+
311
+
312
+ @overload_method(DateTimeType, "__str__")
313
+ def str_datetime(dt):
314
+ """compile this method take a long time"""
315
+ if not isinstance(dt, DateTimeType):
316
+ return
317
+
318
+ def impl(dt):
319
+ return f"{str(dt.year).zfill(4)}-{str(dt.month).zfill(2)}-{str(dt.day).zfill(2)} {str(dt.hour).zfill(2)}:{str(dt.minute).zfill(2)}:{str(dt.second).zfill(2)}.{str(dt.nanosecond).zfill(9)[:6]}"
320
+
321
+ return impl
322
+
323
+
324
+ @overload(operator.ge)
325
+ def impl_ge(dt, dt2):
326
+ if not isinstance(dt, DateTimeType) or not isinstance(dt, DateTimeType):
327
+ return
328
+
329
+ def impl(dt, dt2):
330
+ return dt.val >= dt2.val
331
+
332
+ return impl
333
+
334
+
335
+ @overload(operator.gt)
336
+ def impl_gt(dt, dt2):
337
+ if not isinstance(dt, DateTimeType) or not isinstance(dt, DateTimeType):
338
+ return
339
+
340
+ def impl(dt, dt2):
341
+ return dt.val > dt2.val
342
+
343
+ return impl
344
+
345
+
346
+ @overload(operator.lt)
347
+ def impl_lt(dt, dt2):
348
+ if not isinstance(dt, DateTimeType) or not isinstance(dt, DateTimeType):
349
+ return
350
+
351
+ def impl(dt, dt2):
352
+ return dt.val < dt2.val
353
+
354
+ return impl
355
+
356
+
357
+ @overload(operator.le)
358
+ def impl_le(dt, dt2):
359
+ if not isinstance(dt, DateTimeType) or not isinstance(dt, DateTimeType):
360
+ return
361
+
362
+ def impl(dt, dt2):
363
+ return dt.val <= dt2.val
364
+
365
+ return impl
366
+
367
+
368
+ @overload(operator.eq)
369
+ def impl_eq(dt, dt2):
370
+ if not isinstance(dt, DateTimeType) or not isinstance(dt, DateTimeType):
371
+ return
372
+
373
+ def impl(dt, dt2):
374
+ return dt.val == dt2.val
375
+
376
+ return impl
377
+
378
+
379
+ @overload(operator.ne)
380
+ def impl_ne(dt, dt2):
381
+ if not isinstance(dt, DateTimeType) or not isinstance(dt, DateTimeType):
382
+ return
383
+
384
+ def impl(dt, dt2):
385
+ return dt.val != dt2.val
386
+
387
+ return impl
388
+
389
+
390
+ @overload(operator.add)
391
+ def impl_datetime_add(val1, val2):
392
+ if isinstance(val1, DateTimeType) and isinstance(val2, DurationType):
393
+
394
+ def impl(val1, val2):
395
+ return DateTime(datetime_add_duration(val1.val, val2.ptr))
396
+
397
+ return impl
398
+ elif isinstance(val1, DurationType) and isinstance(val2, DateTimeType):
399
+
400
+ def impl(val1, val2):
401
+ return DateTime(datetime_add_duration(val2.val, val1.ptr))
402
+
403
+ return impl
404
+ return
405
+
406
+
407
+ @overload(operator.sub)
408
+ def impl_datetime_sub(val1, val2):
409
+ if isinstance(val1, DateTimeType) and isinstance(val2, DurationType):
410
+
411
+ def impl(val1, val2):
412
+ return DateTime(datetime_sub_duration(val1.val, val2.ptr))
413
+
414
+ return impl
415
+ return
@@ -0,0 +1,70 @@
1
+ from llvmlite import ir
2
+ from numba import types
3
+ from numba.core import cgutils, utils
4
+ from numba.extending import (
5
+ as_numba_type,
6
+ lower_builtin,
7
+ make_attribute_wrapper,
8
+ models,
9
+ register_model,
10
+ type_callable,
11
+ typeof_impl,
12
+ )
13
+
14
+
15
+ class Duration:
16
+ def __init__(self, fmt: str):
17
+ self.fmt = fmt
18
+
19
+
20
+ class DurationType(types.Type):
21
+ def __init__(self):
22
+ super().__init__(name="Duration")
23
+
24
+
25
+ duration_type = DurationType()
26
+ as_numba_type.register(Duration, duration_type)
27
+
28
+
29
+ @typeof_impl.register(Duration)
30
+ def typeof_index(val, c):
31
+ return duration_type
32
+
33
+
34
+ @type_callable(Duration)
35
+ def type_datetime(context):
36
+ def typer(val):
37
+ return duration_type
38
+
39
+ return typer
40
+
41
+
42
+ @register_model(DurationType)
43
+ class DurationModel(models.StructModel):
44
+ def __init__(self, dmm, fe_type):
45
+ members = [
46
+ ("ptr", types.voidptr),
47
+ ]
48
+ models.StructModel.__init__(self, dmm, fe_type, members)
49
+
50
+
51
+ make_attribute_wrapper(DurationType, "ptr", "ptr")
52
+
53
+
54
+ @lower_builtin(Duration, types.string)
55
+ def impl_duration_builder(context, builder, sig, args):
56
+ typ = sig.return_type
57
+ (fmt,) = args
58
+ fmt = context.make_helper(builder, types.string, fmt)
59
+ fn = cgutils.get_or_insert_function(
60
+ builder.module,
61
+ ir.FunctionType(
62
+ ir.PointerType(ir.IntType(8)),
63
+ [ir.PointerType(ir.IntType(8)), ir.IntType(utils.MACHINE_BITS)],
64
+ ),
65
+ name="parse_duration",
66
+ )
67
+ ptr = builder.call(fn, [fmt.data, fmt.length])
68
+ duration = cgutils.create_struct_proxy(typ)(context, builder)
69
+ duration.ptr = ptr
70
+ return duration._getvalue()