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