transit-python 0.8.321__cp310-abi3-win_amd64.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.
@@ -0,0 +1,353 @@
1
+ ## Copyright 2014 Cognitect. All Rights Reserved.
2
+ ## Copyright 2026 Vendekagon Labs LLC.
3
+ ##
4
+ ## Licensed under the Apache License, Version 2.0 (the "License");
5
+ ## you may not use this file except in compliance with the License.
6
+ ## You may obtain a copy of the License at
7
+ ##
8
+ ## http://www.apache.org/licenses/LICENSE-2.0
9
+ ##
10
+ ## Unless required by applicable law or agreed to in writing, software
11
+ ## distributed under the License is distributed on an "AS-IS" BASIS,
12
+ ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ ## See the License for the specific language governing permissions and
14
+ ## limitations under the License.
15
+
16
+ import datetime
17
+ import struct
18
+ import uuid
19
+ from decimal import Decimal
20
+ from math import isnan
21
+
22
+ from transit.class_hash import ClassDict
23
+ from transit.transit_types import Keyword, Symbol, URI, frozendict, TaggedValue, Link, Boolean
24
+
25
+ MAX_INT = 2**63 - 1
26
+ MIN_INT = -2**63
27
+
28
+ ## This file contains Write Handlers - all the top-level objects used when
29
+ ## writing Transit data. These object must all be immutable and pickleable.
30
+
31
+
32
+ class TaggedMap:
33
+ def __init__(self, tag, rep, str):
34
+ self._tag = tag
35
+ self._rep = rep
36
+ self._str = str
37
+
38
+ def tag(self):
39
+ return self._tag
40
+
41
+ def rep(self):
42
+ return self._rep
43
+
44
+ def string_rep(self):
45
+ return self._str
46
+
47
+
48
+ class NoneHandler:
49
+ @staticmethod
50
+ def tag(_):
51
+ return '_'
52
+
53
+ @staticmethod
54
+ def rep(_):
55
+ return None
56
+
57
+ @staticmethod
58
+ def string_rep(n):
59
+ return None
60
+
61
+
62
+ class IntHandler:
63
+ @staticmethod
64
+ def tag(n):
65
+ if MIN_INT <= n <= MAX_INT:
66
+ return "i"
67
+ return "n"
68
+
69
+ @staticmethod
70
+ def rep(n):
71
+ return n
72
+
73
+ @staticmethod
74
+ def string_rep(n):
75
+ return str(n)
76
+
77
+
78
+ class BigIntHandler:
79
+ @staticmethod
80
+ def tag(_):
81
+ return "n"
82
+
83
+ @staticmethod
84
+ def rep(n):
85
+ return str(n)
86
+
87
+ @staticmethod
88
+ def string_rep(n):
89
+ return str(n)
90
+
91
+
92
+ class BigDecimalHandler:
93
+ @staticmethod
94
+ def tag(_):
95
+ return "f"
96
+
97
+ @staticmethod
98
+ def rep(n):
99
+ return str(n)
100
+
101
+ @staticmethod
102
+ def string_rep(n):
103
+ return str(n)
104
+
105
+
106
+ class FloatHandler:
107
+ @staticmethod
108
+ def tag(f):
109
+ return "z" if isnan(f) or f in (float('Inf'), float('-Inf')) else "d"
110
+
111
+ @staticmethod
112
+ def rep(f):
113
+ if isnan(f):
114
+ return "NaN"
115
+ if f == float('Inf'):
116
+ return "INF"
117
+ if f == float("-Inf"):
118
+ return "-INF"
119
+ return f
120
+
121
+ @staticmethod
122
+ def string_rep(f):
123
+ return str(f)
124
+
125
+
126
+ class StringHandler:
127
+ @staticmethod
128
+ def tag(s):
129
+ return 's'
130
+
131
+ @staticmethod
132
+ def rep(s):
133
+ return s
134
+
135
+ @staticmethod
136
+ def string_rep(s):
137
+ return s
138
+
139
+
140
+ class BooleanHandler:
141
+ @staticmethod
142
+ def tag(_):
143
+ return '?'
144
+
145
+ @staticmethod
146
+ def rep(b):
147
+ return bool(b)
148
+
149
+ @staticmethod
150
+ def string_rep(b):
151
+ return 't' if b else 'f'
152
+
153
+
154
+ class ArrayHandler:
155
+ @staticmethod
156
+ def tag(a):
157
+ return 'array'
158
+
159
+ @staticmethod
160
+ def rep(a):
161
+ return a
162
+
163
+ @staticmethod
164
+ def string_rep(a):
165
+ return None
166
+
167
+
168
+ class MapHandler:
169
+ @staticmethod
170
+ def tag(m):
171
+ return 'map'
172
+
173
+ @staticmethod
174
+ def rep(m):
175
+ return m
176
+
177
+ @staticmethod
178
+ def string_rep(m):
179
+ return None
180
+
181
+
182
+ class KeywordHandler:
183
+ @staticmethod
184
+ def tag(k):
185
+ return ':'
186
+
187
+ @staticmethod
188
+ def rep(k):
189
+ return str(k)
190
+
191
+ @staticmethod
192
+ def string_rep(k):
193
+ return str(k)
194
+
195
+
196
+ class SymbolHandler:
197
+ @staticmethod
198
+ def tag(s):
199
+ return '$'
200
+
201
+ @staticmethod
202
+ def rep(s):
203
+ return str(s)
204
+
205
+ @staticmethod
206
+ def string_rep(s):
207
+ return str(s)
208
+
209
+
210
+ class UuidHandler:
211
+ @staticmethod
212
+ def tag(_):
213
+ return "u"
214
+
215
+ @staticmethod
216
+ def rep(u):
217
+ return struct.unpack('>qq', u.bytes)
218
+
219
+ @staticmethod
220
+ def string_rep(u):
221
+ return str(u)
222
+
223
+
224
+ class UriHandler:
225
+ @staticmethod
226
+ def tag(_):
227
+ return "r"
228
+
229
+ @staticmethod
230
+ def rep(u):
231
+ return u.rep
232
+
233
+ @staticmethod
234
+ def string_rep(u):
235
+ return u.rep
236
+
237
+
238
+ class DateTimeHandler:
239
+ epoch = datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
240
+
241
+ @staticmethod
242
+ def tag(_):
243
+ return "m"
244
+
245
+ @staticmethod
246
+ def rep(d):
247
+ return (d - DateTimeHandler.epoch) // datetime.timedelta(milliseconds=1)
248
+
249
+ @staticmethod
250
+ def verbose_handler():
251
+ return VerboseDateTimeHandler
252
+
253
+ @staticmethod
254
+ def string_rep(d):
255
+ return str(DateTimeHandler.rep(d))
256
+
257
+
258
+ def format_rfc3339(d):
259
+ """d as an RFC 3339 UTC time to the millisecond, as transit-java writes it."""
260
+ ms = DateTimeHandler.rep(d)
261
+ t = DateTimeHandler.epoch + datetime.timedelta(milliseconds=ms)
262
+ return "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ" % (t.year, t.month, t.day, t.hour, t.minute, t.second,
263
+ ms % 1000)
264
+
265
+
266
+ class VerboseDateTimeHandler:
267
+ @staticmethod
268
+ def tag(_):
269
+ return "t"
270
+
271
+ @staticmethod
272
+ def rep(d):
273
+ return format_rfc3339(d)
274
+
275
+ @staticmethod
276
+ def string_rep(d):
277
+ return format_rfc3339(d)
278
+
279
+
280
+ class SetHandler:
281
+ @staticmethod
282
+ def tag(_):
283
+ return "set"
284
+
285
+ @staticmethod
286
+ def rep(s):
287
+ return TaggedMap("array", tuple(s), None)
288
+
289
+ @staticmethod
290
+ def string_rep(_):
291
+ return None
292
+
293
+
294
+ class TaggedValueHandler:
295
+ @staticmethod
296
+ def tag(tv):
297
+ return tv.tag
298
+
299
+ @staticmethod
300
+ def rep(tv):
301
+ return tv.rep
302
+
303
+ @staticmethod
304
+ def string_rep(_):
305
+ return None
306
+
307
+
308
+ class LinkHandler:
309
+ @staticmethod
310
+ def tag(_):
311
+ return "link"
312
+
313
+ @staticmethod
314
+ def rep(l):
315
+ return l.as_map
316
+
317
+ @staticmethod
318
+ def string_rep(_):
319
+ return None
320
+
321
+
322
+ class WriteHandler(ClassDict):
323
+ """This is the top-level handler for encoding/writing Python data into
324
+ Transit data, based on its type.
325
+ The Handler itself is a dispatch map, that resolves on full type/object
326
+ inheritance.
327
+
328
+ These handlers can be overriden during the creation of a Transit Writer.
329
+ """
330
+
331
+ def __init__(self):
332
+ super(WriteHandler, self).__init__()
333
+ self[type(None)] = NoneHandler
334
+ self[bool] = BooleanHandler
335
+ self[Boolean] = BooleanHandler
336
+ self[str] = StringHandler
337
+ self[list] = ArrayHandler
338
+ self[tuple] = ArrayHandler
339
+ self[int] = IntHandler
340
+ self[float] = FloatHandler
341
+ self[Keyword] = KeywordHandler
342
+ self[Symbol] = SymbolHandler
343
+ self[uuid.UUID] = UuidHandler
344
+ self[URI] = UriHandler
345
+ self[datetime.datetime] = DateTimeHandler
346
+ self[set] = SetHandler
347
+ self[frozenset] = SetHandler
348
+ self[TaggedMap] = TaggedMap
349
+ self[dict] = MapHandler
350
+ self[frozendict] = MapHandler
351
+ self[TaggedValue] = TaggedValueHandler
352
+ self[Link] = LinkHandler
353
+ self[Decimal] = BigDecimalHandler