dataclass-native 1.0.0__py3-none-any.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.
- dataclass_native/__init__.py +60 -0
- dataclass_native/__main__.py +5 -0
- dataclass_native/_arrow.py +822 -0
- dataclass_native/_json.py +783 -0
- dataclass_native/_mode.py +57 -0
- dataclass_native/_msgpack.py +207 -0
- dataclass_native/_native.py +234 -0
- dataclass_native/_runtime.py +607 -0
- dataclass_native/_version.py +24 -0
- dataclass_native/_wire.py +901 -0
- dataclass_native/arrays.py +13 -0
- dataclass_native/backends/__init__.py +0 -0
- dataclass_native/backends/bindings.py +74 -0
- dataclass_native/backends/cpp.py +574 -0
- dataclass_native/backends/templates/bindings.cpp.j2 +58 -0
- dataclass_native/backends/templates/extensions.cmake.j2 +12 -0
- dataclass_native/backends/templates/header.hpp.j2 +75 -0
- dataclass_native/cli.py +125 -0
- dataclass_native/cmake/dataclass_native-config.cmake +67 -0
- dataclass_native/include/dataclass_native/arrow.hpp +2022 -0
- dataclass_native/include/dataclass_native/json.hpp +1903 -0
- dataclass_native/include/dataclass_native/msgpack.hpp +1513 -0
- dataclass_native/include/dataclass_native/python.hpp +594 -0
- dataclass_native/include/dataclass_native/reflect.hpp +63 -0
- dataclass_native/include/dataclass_native/types.hpp +417 -0
- dataclass_native/introspect.py +811 -0
- dataclass_native/py.typed +0 -0
- dataclass_native/schema.py +553 -0
- dataclass_native/types.py +103 -0
- dataclass_native-1.0.0.dist-info/METADATA +339 -0
- dataclass_native-1.0.0.dist-info/RECORD +35 -0
- dataclass_native-1.0.0.dist-info/WHEEL +5 -0
- dataclass_native-1.0.0.dist-info/entry_points.txt +2 -0
- dataclass_native-1.0.0.dist-info/licenses/LICENSE +21 -0
- dataclass_native-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
// Conversions between Python objects and the types of dataclass_native/types.hpp, for nanobind bindings. Include it
|
|
2
|
+
// before any use of those types in a binding, as it converts Bytes (a std::vector) to bytes, not a list.
|
|
3
|
+
#pragma once
|
|
4
|
+
|
|
5
|
+
#include <algorithm>
|
|
6
|
+
#include <chrono>
|
|
7
|
+
#include <cstdint>
|
|
8
|
+
#include <cstring>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <utility>
|
|
11
|
+
|
|
12
|
+
#include <nanobind/nanobind.h>
|
|
13
|
+
#include <nanobind/ndarray.h>
|
|
14
|
+
#include <nanobind/stl/shared_ptr.h>
|
|
15
|
+
#include <nanobind/stl/variant.h>
|
|
16
|
+
#include <nanobind/stl/vector.h>
|
|
17
|
+
|
|
18
|
+
#include <datetime.h>
|
|
19
|
+
|
|
20
|
+
#include "dataclass_native/arrow.hpp"
|
|
21
|
+
#include "dataclass_native/json.hpp"
|
|
22
|
+
#include "dataclass_native/msgpack.hpp"
|
|
23
|
+
#include "dataclass_native/types.hpp"
|
|
24
|
+
|
|
25
|
+
namespace dataclass_native::python
|
|
26
|
+
{
|
|
27
|
+
// Python's datetime C API. Not datetime.h's PyDateTimeAPI, which is a variable of each translation unit
|
|
28
|
+
inline PyDateTime_CAPI* datetime_api()
|
|
29
|
+
{
|
|
30
|
+
static PyDateTime_CAPI* api = static_cast<PyDateTime_CAPI*>(PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0));
|
|
31
|
+
return api;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// What a module has of a name, or null with Python's error set
|
|
35
|
+
inline PyObject* imported(const char* module, const char* name)
|
|
36
|
+
{
|
|
37
|
+
PyObject* held = PyImport_ImportModule(module);
|
|
38
|
+
PyObject* found = held ? PyObject_GetAttrString(held, name) : nullptr;
|
|
39
|
+
Py_XDECREF(held);
|
|
40
|
+
return found;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
inline PyObject* uuid_type()
|
|
44
|
+
{
|
|
45
|
+
static PyObject* type = imported("uuid", "UUID");
|
|
46
|
+
return type;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
inline PyObject* decimal_type()
|
|
50
|
+
{
|
|
51
|
+
static PyObject* type = imported("decimal", "Decimal");
|
|
52
|
+
return type;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// The integer which a decimal.Decimal is of so many places, if it is that exactly and in less than the limit
|
|
56
|
+
inline bool unscaled(PyObject* o, int places, std::int64_t limit, std::int64_t& value)
|
|
57
|
+
{
|
|
58
|
+
PyObject* type = decimal_type();
|
|
59
|
+
if (!type || reinterpret_cast<PyObject*>(Py_TYPE(o)) != type)
|
|
60
|
+
return false;
|
|
61
|
+
// Its sign, digits and exponent: which is not an integer, if it is not a number
|
|
62
|
+
PyObject* parts = PyObject_CallMethod(o, "as_tuple", nullptr);
|
|
63
|
+
bool valid = parts && PyTuple_Check(parts) && PyTuple_GET_SIZE(parts) == 3 &&
|
|
64
|
+
PyTuple_Check(PyTuple_GET_ITEM(parts, 1)) && PyLong_Check(PyTuple_GET_ITEM(parts, 2));
|
|
65
|
+
if (valid)
|
|
66
|
+
{
|
|
67
|
+
PyObject* digits = PyTuple_GET_ITEM(parts, 1);
|
|
68
|
+
const auto count = static_cast<long long>(PyTuple_GET_SIZE(digits));
|
|
69
|
+
int beyond = 0;
|
|
70
|
+
long long exponent = PyLong_AsLongLongAndOverflow(PyTuple_GET_ITEM(parts, 2), &beyond);
|
|
71
|
+
// Whatever it is, more than any number of digits
|
|
72
|
+
constexpr long long vast = 1'000'000'000'000;
|
|
73
|
+
exponent = beyond ? beyond * vast : std::clamp(exponent, -vast, vast);
|
|
74
|
+
// As many of its digits as are of the integer, the rest of which must be zeros: then zeros, if too few
|
|
75
|
+
const long long kept = count + exponent + places;
|
|
76
|
+
std::int64_t magnitude = 0;
|
|
77
|
+
for (long long i = 0; valid && i != count; ++i)
|
|
78
|
+
{
|
|
79
|
+
const long digit = PyLong_AsLong(PyTuple_GET_ITEM(digits, static_cast<Py_ssize_t>(i)));
|
|
80
|
+
// Before it is of another digit, which may be more than an integer holds
|
|
81
|
+
valid = i < kept ? magnitude < limit / 10 : digit == 0;
|
|
82
|
+
if (valid && i < kept)
|
|
83
|
+
magnitude = magnitude * 10 + digit;
|
|
84
|
+
}
|
|
85
|
+
for (long long i = count; valid && magnitude && i < kept; ++i)
|
|
86
|
+
if ((valid = magnitude < limit / 10))
|
|
87
|
+
magnitude *= 10;
|
|
88
|
+
value = PyObject_IsTrue(PyTuple_GET_ITEM(parts, 0)) ? -magnitude : magnitude;
|
|
89
|
+
}
|
|
90
|
+
if (PyErr_Occurred())
|
|
91
|
+
{
|
|
92
|
+
PyErr_Clear();
|
|
93
|
+
valid = false;
|
|
94
|
+
}
|
|
95
|
+
Py_XDECREF(parts);
|
|
96
|
+
return valid;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
inline bool is_exactly(PyObject* o, PyTypeObject* type)
|
|
100
|
+
{
|
|
101
|
+
return Py_TYPE(o) == type;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// The date and time of day of a datetime, whatever its timezone: as microseconds since 1970-01-01T00:00
|
|
105
|
+
inline std::chrono::microseconds date_and_time(PyObject* o)
|
|
106
|
+
{
|
|
107
|
+
const std::chrono::sys_days day = std::chrono::year{PyDateTime_GET_YEAR(o)} /
|
|
108
|
+
std::chrono::month{static_cast<unsigned>(PyDateTime_GET_MONTH(o))} /
|
|
109
|
+
std::chrono::day{static_cast<unsigned>(PyDateTime_GET_DAY(o))};
|
|
110
|
+
// In microseconds from the first, as the minutes of ten thousand years are more than those of MSVC hold
|
|
111
|
+
return std::chrono::microseconds{day.time_since_epoch()} + std::chrono::hours{PyDateTime_DATE_GET_HOUR(o)} +
|
|
112
|
+
std::chrono::minutes{PyDateTime_DATE_GET_MINUTE(o)} +
|
|
113
|
+
std::chrono::seconds{PyDateTime_DATE_GET_SECOND(o)} +
|
|
114
|
+
std::chrono::microseconds{PyDateTime_DATE_GET_MICROSECOND(o)};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// The datetime of that date and time of day, in a timezone or None
|
|
118
|
+
inline PyObject* datetime(std::chrono::microseconds since_epoch, PyObject* tzinfo)
|
|
119
|
+
{
|
|
120
|
+
auto* api = datetime_api();
|
|
121
|
+
const auto day = std::chrono::floor<std::chrono::days>(since_epoch);
|
|
122
|
+
const std::chrono::year_month_day ymd{std::chrono::sys_days{day}};
|
|
123
|
+
const std::chrono::hh_mm_ss time{since_epoch - day};
|
|
124
|
+
return api->DateTime_FromDateAndTime(
|
|
125
|
+
static_cast<int>(ymd.year()), static_cast<int>(static_cast<unsigned>(ymd.month())),
|
|
126
|
+
static_cast<int>(static_cast<unsigned>(ymd.day())), static_cast<int>(time.hours().count()),
|
|
127
|
+
static_cast<int>(time.minutes().count()), static_cast<int>(time.seconds().count()),
|
|
128
|
+
static_cast<int>(time.subseconds().count()), tzinfo, api->DateTimeType);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// The microseconds of a timedelta, if they fit
|
|
132
|
+
inline bool microseconds(PyObject* delta, std::chrono::microseconds& value)
|
|
133
|
+
{
|
|
134
|
+
// Beyond this many days, the microseconds overflow 64 bits
|
|
135
|
+
constexpr std::int64_t max_days = 106'751'990;
|
|
136
|
+
const std::int64_t days = PyDateTime_DELTA_GET_DAYS(delta);
|
|
137
|
+
if (days < -max_days || days > max_days)
|
|
138
|
+
return false;
|
|
139
|
+
value = std::chrono::microseconds{(days * 86'400 + PyDateTime_DELTA_GET_SECONDS(delta)) * 1'000'000 +
|
|
140
|
+
PyDateTime_DELTA_GET_MICROSECONDS(delta)};
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// A native object as msgpack, and one read from it: see msgpack.hpp
|
|
145
|
+
template <class T>
|
|
146
|
+
nanobind::bytes to_msgpack(const T& value)
|
|
147
|
+
{
|
|
148
|
+
const Bytes data = msgpack::to_msgpack(value);
|
|
149
|
+
return nanobind::bytes(reinterpret_cast<const char*>(data.data()), data.size());
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
template <class T>
|
|
153
|
+
T from_msgpack(nanobind::bytes data)
|
|
154
|
+
{
|
|
155
|
+
return msgpack::from_msgpack<T>({reinterpret_cast<const std::uint8_t*>(data.c_str()), data.size()});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// A native object as JSON, and one read from it: see json.hpp
|
|
159
|
+
template <class T>
|
|
160
|
+
nanobind::bytes to_json(const T& value)
|
|
161
|
+
{
|
|
162
|
+
const std::string data = json::to_json(value);
|
|
163
|
+
return nanobind::bytes(data.data(), data.size());
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
template <class T>
|
|
167
|
+
T from_json(nanobind::bytes data)
|
|
168
|
+
{
|
|
169
|
+
return json::from_json<T>({data.c_str(), data.size()});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// What a capsule of the Arrow PyCapsule interface holds, of the name it is to have
|
|
173
|
+
template <class A>
|
|
174
|
+
A* arrow_pointer(PyObject* capsule, const char* name)
|
|
175
|
+
{
|
|
176
|
+
auto* held = static_cast<A*>(PyCapsule_GetPointer(capsule, name));
|
|
177
|
+
if (!held)
|
|
178
|
+
throw nanobind::python_error();
|
|
179
|
+
return held;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// A table as the capsules of that interface, which then own it: of its schema, and of its array
|
|
183
|
+
inline nanobind::tuple arrow_capsules(arrow::Table& table)
|
|
184
|
+
{
|
|
185
|
+
auto* schema = new ArrowSchema(std::exchange(table.schema, {}));
|
|
186
|
+
auto* array = new ArrowArray(std::exchange(table.array, {}));
|
|
187
|
+
return nanobind::make_tuple(nanobind::steal(PyCapsule_New(schema, "arrow_schema",
|
|
188
|
+
[](PyObject* capsule)
|
|
189
|
+
{
|
|
190
|
+
auto* held = static_cast<ArrowSchema*>(
|
|
191
|
+
PyCapsule_GetPointer(capsule, "arrow_schema"));
|
|
192
|
+
if (held->release)
|
|
193
|
+
held->release(held);
|
|
194
|
+
delete held;
|
|
195
|
+
})),
|
|
196
|
+
nanobind::steal(PyCapsule_New(array, "arrow_array",
|
|
197
|
+
[](PyObject* capsule)
|
|
198
|
+
{
|
|
199
|
+
auto* held = static_cast<ArrowArray*>(
|
|
200
|
+
PyCapsule_GetPointer(capsule, "arrow_array"));
|
|
201
|
+
if (held->release)
|
|
202
|
+
held->release(held);
|
|
203
|
+
delete held;
|
|
204
|
+
})));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Native objects as Arrow tables: the capsules of that of the objects, and those of each class they may share,
|
|
208
|
+
// with its tag. See arrow.hpp
|
|
209
|
+
template <class T>
|
|
210
|
+
nanobind::tuple to_arrow(nanobind::sequence objects)
|
|
211
|
+
{
|
|
212
|
+
std::vector<const T*> pointers;
|
|
213
|
+
for (nanobind::handle object : objects)
|
|
214
|
+
pointers.push_back(&nanobind::cast<const T&>(object));
|
|
215
|
+
arrow::Tables tables = arrow::to_arrow<T>(std::span<const T* const>(pointers));
|
|
216
|
+
|
|
217
|
+
nanobind::list shared;
|
|
218
|
+
for (auto& [tag, table] : tables.shared)
|
|
219
|
+
shared.append(nanobind::make_tuple(nanobind::str(tag.data(), tag.size()), arrow_capsules(table)));
|
|
220
|
+
return nanobind::make_tuple(arrow_capsules(tables.root), shared);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// The native objects which tables hold, given as their capsules: shared is of those of the objects they share,
|
|
224
|
+
// by tag. None if the tables are not read, for whatever reason: that in Python says why, if it does not read them
|
|
225
|
+
template <class T>
|
|
226
|
+
nanobind::object from_arrow(nanobind::handle schema, nanobind::handle array, nanobind::dict shared)
|
|
227
|
+
{
|
|
228
|
+
std::vector<std::string> tags;
|
|
229
|
+
tags.reserve(nanobind::len(shared));
|
|
230
|
+
std::vector<arrow::SharedTable> tables;
|
|
231
|
+
for (auto [tag, capsules] : shared)
|
|
232
|
+
{
|
|
233
|
+
tags.push_back(nanobind::cast<std::string>(tag));
|
|
234
|
+
const auto pair = nanobind::borrow<nanobind::tuple>(capsules);
|
|
235
|
+
tables.push_back({tags.back(), arrow_pointer<ArrowSchema>(pair[0].ptr(), "arrow_schema"),
|
|
236
|
+
arrow_pointer<ArrowArray>(pair[1].ptr(), "arrow_array")});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
try
|
|
240
|
+
{
|
|
241
|
+
return nanobind::cast(arrow::from_arrow<T>(*arrow_pointer<ArrowSchema>(schema.ptr(), "arrow_schema"),
|
|
242
|
+
*arrow_pointer<ArrowArray>(array.ptr(), "arrow_array"), tables));
|
|
243
|
+
}
|
|
244
|
+
catch (const arrow::Error&)
|
|
245
|
+
{
|
|
246
|
+
return nanobind::none();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
} // namespace dataclass_native::python
|
|
250
|
+
|
|
251
|
+
NAMESPACE_BEGIN(NB_NAMESPACE)
|
|
252
|
+
NAMESPACE_BEGIN(detail)
|
|
253
|
+
|
|
254
|
+
template <>
|
|
255
|
+
struct type_caster<dataclass_native::Bytes>
|
|
256
|
+
{
|
|
257
|
+
NB_TYPE_CASTER(dataclass_native::Bytes, const_name("bytes"))
|
|
258
|
+
|
|
259
|
+
bool from_python(handle src, uint32_t, cleanup_list*) noexcept
|
|
260
|
+
{
|
|
261
|
+
if (!PyBytes_Check(src.ptr()))
|
|
262
|
+
return false;
|
|
263
|
+
const auto* data = reinterpret_cast<const std::uint8_t*>(PyBytes_AsString(src.ptr()));
|
|
264
|
+
value.assign(data, data + PyBytes_Size(src.ptr()));
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
static handle from_cpp(const Value& value, rv_policy, cleanup_list*) noexcept
|
|
269
|
+
{
|
|
270
|
+
return PyBytes_FromStringAndSize(reinterpret_cast<const char*>(value.data()),
|
|
271
|
+
static_cast<Py_ssize_t>(value.size()));
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// decimal.Decimal, exactly: one of no more places than it holds, which it is read back with
|
|
276
|
+
template <int Places>
|
|
277
|
+
struct type_caster<dataclass_native::Decimal<Places>>
|
|
278
|
+
{
|
|
279
|
+
using Decimal = dataclass_native::Decimal<Places>;
|
|
280
|
+
NB_TYPE_CASTER(Decimal, const_name("decimal.Decimal"))
|
|
281
|
+
|
|
282
|
+
bool from_python(handle src, uint32_t, cleanup_list*) noexcept
|
|
283
|
+
{
|
|
284
|
+
return dataclass_native::python::unscaled(src.ptr(), Places, Decimal::limit, value.unscaled);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
static handle from_cpp(const Value& value, rv_policy, cleanup_list*) noexcept
|
|
288
|
+
{
|
|
289
|
+
// From its digits, which is exact whatever Python's context
|
|
290
|
+
PyObject* type = dataclass_native::python::decimal_type();
|
|
291
|
+
return type ? PyObject_CallFunction(type, "s", value.str().c_str()) : nullptr;
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
// uuid.UUID, exactly
|
|
296
|
+
template <>
|
|
297
|
+
struct type_caster<dataclass_native::Uuid>
|
|
298
|
+
{
|
|
299
|
+
NB_TYPE_CASTER(dataclass_native::Uuid, const_name("uuid.UUID"))
|
|
300
|
+
|
|
301
|
+
bool from_python(handle src, uint32_t, cleanup_list*) noexcept
|
|
302
|
+
{
|
|
303
|
+
PyObject* type = dataclass_native::python::uuid_type();
|
|
304
|
+
if (!type || reinterpret_cast<PyObject*>(Py_TYPE(src.ptr())) != type)
|
|
305
|
+
return false;
|
|
306
|
+
PyObject* bytes = PyObject_GetAttrString(src.ptr(), "bytes");
|
|
307
|
+
const bool valid = bytes && PyBytes_Check(bytes) && PyBytes_GET_SIZE(bytes) == 16;
|
|
308
|
+
if (valid)
|
|
309
|
+
std::memcpy(value.bytes.data(), PyBytes_AS_STRING(bytes), 16);
|
|
310
|
+
else
|
|
311
|
+
PyErr_Clear();
|
|
312
|
+
Py_XDECREF(bytes);
|
|
313
|
+
return valid;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
static handle from_cpp(const Value& value, rv_policy, cleanup_list*) noexcept
|
|
317
|
+
{
|
|
318
|
+
PyObject* type = dataclass_native::python::uuid_type();
|
|
319
|
+
if (!type)
|
|
320
|
+
return {};
|
|
321
|
+
try
|
|
322
|
+
{
|
|
323
|
+
const bytes held(reinterpret_cast<const char*>(value.bytes.data()), value.bytes.size());
|
|
324
|
+
return handle(type)(arg("bytes") = held).release();
|
|
325
|
+
}
|
|
326
|
+
catch (python_error& error)
|
|
327
|
+
{
|
|
328
|
+
error.restore();
|
|
329
|
+
return {};
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
template <>
|
|
335
|
+
struct type_caster<dataclass_native::Date>
|
|
336
|
+
{
|
|
337
|
+
NB_TYPE_CASTER(dataclass_native::Date, const_name("datetime.date"))
|
|
338
|
+
|
|
339
|
+
bool from_python(handle src, uint32_t, cleanup_list*) noexcept
|
|
340
|
+
{
|
|
341
|
+
PyObject* o = src.ptr();
|
|
342
|
+
// Exactly, as a datetime is a date
|
|
343
|
+
if (!dataclass_native::python::is_exactly(o, dataclass_native::python::datetime_api()->DateType))
|
|
344
|
+
return false;
|
|
345
|
+
value = std::chrono::year{PyDateTime_GET_YEAR(o)} /
|
|
346
|
+
std::chrono::month{static_cast<unsigned>(PyDateTime_GET_MONTH(o))} /
|
|
347
|
+
std::chrono::day{static_cast<unsigned>(PyDateTime_GET_DAY(o))};
|
|
348
|
+
return true;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
static handle from_cpp(const Value& value, rv_policy, cleanup_list*) noexcept
|
|
352
|
+
{
|
|
353
|
+
auto* api = dataclass_native::python::datetime_api();
|
|
354
|
+
const std::chrono::year_month_day ymd{value};
|
|
355
|
+
return api->Date_FromDate(static_cast<int>(ymd.year()), static_cast<int>(static_cast<unsigned>(ymd.month())),
|
|
356
|
+
static_cast<int>(static_cast<unsigned>(ymd.day())), api->DateType);
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
template <>
|
|
361
|
+
struct type_caster<dataclass_native::TimeDelta>
|
|
362
|
+
{
|
|
363
|
+
NB_TYPE_CASTER(dataclass_native::TimeDelta, const_name("datetime.timedelta"))
|
|
364
|
+
|
|
365
|
+
bool from_python(handle src, uint32_t, cleanup_list*) noexcept
|
|
366
|
+
{
|
|
367
|
+
return dataclass_native::python::is_exactly(src.ptr(), dataclass_native::python::datetime_api()->DeltaType) &&
|
|
368
|
+
dataclass_native::python::microseconds(src.ptr(), value);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
static handle from_cpp(const Value& value, rv_policy, cleanup_list*) noexcept
|
|
372
|
+
{
|
|
373
|
+
auto* api = dataclass_native::python::datetime_api();
|
|
374
|
+
const auto days = std::chrono::floor<std::chrono::days>(value);
|
|
375
|
+
const auto seconds = std::chrono::floor<std::chrono::seconds>(value - days);
|
|
376
|
+
return api->Delta_FromDelta(static_cast<int>(days.count()), static_cast<int>(seconds.count()),
|
|
377
|
+
static_cast<int>((value - days - seconds).count()), 0, api->DeltaType);
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
template <>
|
|
382
|
+
struct type_caster<dataclass_native::TimeOfDay>
|
|
383
|
+
{
|
|
384
|
+
NB_TYPE_CASTER(dataclass_native::TimeOfDay, const_name("datetime.time"))
|
|
385
|
+
|
|
386
|
+
bool from_python(handle src, uint32_t, cleanup_list*) noexcept
|
|
387
|
+
{
|
|
388
|
+
PyObject* o = src.ptr();
|
|
389
|
+
if (!dataclass_native::python::is_exactly(o, dataclass_native::python::datetime_api()->TimeType) ||
|
|
390
|
+
PyDateTime_TIME_GET_TZINFO(o) != Py_None)
|
|
391
|
+
return false;
|
|
392
|
+
value.since_midnight =
|
|
393
|
+
std::chrono::hours{PyDateTime_TIME_GET_HOUR(o)} + std::chrono::minutes{PyDateTime_TIME_GET_MINUTE(o)} +
|
|
394
|
+
std::chrono::seconds{PyDateTime_TIME_GET_SECOND(o)} +
|
|
395
|
+
std::chrono::microseconds{PyDateTime_TIME_GET_MICROSECOND(o)};
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
static handle from_cpp(const Value& value, rv_policy, cleanup_list*) noexcept
|
|
400
|
+
{
|
|
401
|
+
auto* api = dataclass_native::python::datetime_api();
|
|
402
|
+
const std::chrono::hh_mm_ss time{value.since_midnight};
|
|
403
|
+
return api->Time_FromTime(static_cast<int>(time.hours().count()), static_cast<int>(time.minutes().count()),
|
|
404
|
+
static_cast<int>(time.seconds().count()),
|
|
405
|
+
static_cast<int>(time.subseconds().count()), Py_None, api->TimeType);
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
// An aware datetime, of any timezone: read back as UTC
|
|
410
|
+
template <>
|
|
411
|
+
struct type_caster<dataclass_native::DateTime>
|
|
412
|
+
{
|
|
413
|
+
NB_TYPE_CASTER(dataclass_native::DateTime, const_name("datetime.datetime"))
|
|
414
|
+
|
|
415
|
+
bool from_python(handle src, uint32_t, cleanup_list*) noexcept
|
|
416
|
+
{
|
|
417
|
+
auto* api = dataclass_native::python::datetime_api();
|
|
418
|
+
PyObject* o = src.ptr();
|
|
419
|
+
if (!dataclass_native::python::is_exactly(o, api->DateTimeType))
|
|
420
|
+
return false;
|
|
421
|
+
|
|
422
|
+
std::chrono::microseconds offset{};
|
|
423
|
+
if (PyObject* tzinfo = PyDateTime_DATE_GET_TZINFO(o); tzinfo != api->TimeZone_UTC)
|
|
424
|
+
{
|
|
425
|
+
if (tzinfo == Py_None)
|
|
426
|
+
return false;
|
|
427
|
+
PyObject* delta = PyObject_CallMethod(o, "utcoffset", nullptr);
|
|
428
|
+
const bool valid = delta && dataclass_native::python::is_exactly(delta, api->DeltaType) &&
|
|
429
|
+
dataclass_native::python::microseconds(delta, offset);
|
|
430
|
+
Py_XDECREF(delta);
|
|
431
|
+
if (!valid)
|
|
432
|
+
{
|
|
433
|
+
PyErr_Clear();
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
value = Value{dataclass_native::python::date_and_time(o) - offset};
|
|
439
|
+
return true;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
static handle from_cpp(const Value& value, rv_policy, cleanup_list*) noexcept
|
|
443
|
+
{
|
|
444
|
+
return dataclass_native::python::datetime(value.time_since_epoch(),
|
|
445
|
+
dataclass_native::python::datetime_api()->TimeZone_UTC);
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
// A datetime with no timezone
|
|
450
|
+
template <>
|
|
451
|
+
struct type_caster<dataclass_native::NaiveDateTime>
|
|
452
|
+
{
|
|
453
|
+
NB_TYPE_CASTER(dataclass_native::NaiveDateTime, const_name("datetime.datetime"))
|
|
454
|
+
|
|
455
|
+
bool from_python(handle src, uint32_t, cleanup_list*) noexcept
|
|
456
|
+
{
|
|
457
|
+
PyObject* o = src.ptr();
|
|
458
|
+
if (!dataclass_native::python::is_exactly(o, dataclass_native::python::datetime_api()->DateTimeType) ||
|
|
459
|
+
PyDateTime_DATE_GET_TZINFO(o) != Py_None)
|
|
460
|
+
return false;
|
|
461
|
+
value = Value{dataclass_native::python::date_and_time(o)};
|
|
462
|
+
return true;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
static handle from_cpp(const Value& value, rv_policy, cleanup_list*) noexcept
|
|
466
|
+
{
|
|
467
|
+
return dataclass_native::python::datetime(value.time_since_epoch(), Py_None);
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
// The Python object of a Shared<T> is that of its T, and keeps it alive: sharing it shares the object
|
|
472
|
+
template <typename T>
|
|
473
|
+
struct type_caster<dataclass_native::Shared<T>>
|
|
474
|
+
{
|
|
475
|
+
using Caster = make_caster<std::shared_ptr<const T>>;
|
|
476
|
+
NB_TYPE_CASTER(dataclass_native::Shared<T>, Caster::Name)
|
|
477
|
+
|
|
478
|
+
bool from_python(handle src, uint32_t flags, cleanup_list* cleanup) noexcept
|
|
479
|
+
{
|
|
480
|
+
Caster caster;
|
|
481
|
+
if (src.is_none() || !caster.from_python(src, flags, cleanup))
|
|
482
|
+
return false;
|
|
483
|
+
value = Value(std::move(caster.value));
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
static handle from_cpp(const Value& value, rv_policy policy, cleanup_list* cleanup) noexcept
|
|
488
|
+
{
|
|
489
|
+
return Caster::from_cpp(value.ptr(), policy, cleanup);
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
// The Python object of a Mutable<T> is that of its T, which it shares: what is set on either is set on both
|
|
494
|
+
template <typename T>
|
|
495
|
+
struct type_caster<dataclass_native::Mutable<T>>
|
|
496
|
+
{
|
|
497
|
+
using Caster = make_caster<std::shared_ptr<T>>;
|
|
498
|
+
NB_TYPE_CASTER(dataclass_native::Mutable<T>, Caster::Name)
|
|
499
|
+
|
|
500
|
+
bool from_python(handle src, uint32_t flags, cleanup_list* cleanup) noexcept
|
|
501
|
+
{
|
|
502
|
+
Caster caster;
|
|
503
|
+
if (src.is_none() || !caster.from_python(src, flags, cleanup))
|
|
504
|
+
return false;
|
|
505
|
+
value = Value(std::move(caster.value));
|
|
506
|
+
return true;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
static handle from_cpp(const Value& value, rv_policy policy, cleanup_list* cleanup) noexcept
|
|
510
|
+
{
|
|
511
|
+
return Caster::from_cpp(value.ptr(), policy, cleanup);
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
// A numpy array of what the Array holds, which is not copied, and keeps what it holds alive. It cannot be written to,
|
|
516
|
+
// unless the Array may be. Made from an array of exactly its type and number of dimensions, whose numbers are copied
|
|
517
|
+
template <typename T, size_t N, bool W>
|
|
518
|
+
struct type_caster<dataclass_native::Array<T, N, W>>
|
|
519
|
+
{
|
|
520
|
+
using Array = dataclass_native::Array<T, N, W>;
|
|
521
|
+
using Read = ndarray<numpy, typename Array::Element, ndim<N>>;
|
|
522
|
+
using Given = ndarray<const T, ndim<N>, c_contig, device::cpu>;
|
|
523
|
+
using Held = std::shared_ptr<std::vector<T>>;
|
|
524
|
+
NB_TYPE_CASTER(Array, make_caster<Read>::Name)
|
|
525
|
+
|
|
526
|
+
bool from_python(handle src, uint32_t flags, cleanup_list* cleanup) noexcept
|
|
527
|
+
{
|
|
528
|
+
make_caster<Given> caster;
|
|
529
|
+
if (src.is_none() || !caster.from_python(src, flags & ~static_cast<uint32_t>(cast_flags::convert), cleanup))
|
|
530
|
+
return false;
|
|
531
|
+
const Given& given = caster.value;
|
|
532
|
+
typename Array::Shape shape;
|
|
533
|
+
for (size_t i = 0; i != N; ++i)
|
|
534
|
+
shape[i] = given.shape(i);
|
|
535
|
+
value = Value(std::vector<T>(given.data(), given.data() + given.size()), shape);
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
static handle from_cpp(const Value& value, rv_policy, cleanup_list* cleanup) noexcept
|
|
540
|
+
{
|
|
541
|
+
const capsule owner(new Held(value.ptr()), [](void* held) noexcept { delete static_cast<Held*>(held); });
|
|
542
|
+
return make_caster<Read>::from_cpp(Read((*value).data(), N, value.shape().data(), owner), rv_policy::reference,
|
|
543
|
+
cleanup);
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
// As the std::variant it is
|
|
548
|
+
template <typename... Ts>
|
|
549
|
+
struct type_caster<dataclass_native::DiscriminatedUnion<Ts...>>
|
|
550
|
+
{
|
|
551
|
+
using Caster = make_caster<std::variant<Ts...>>;
|
|
552
|
+
NB_TYPE_CASTER(dataclass_native::DiscriminatedUnion<Ts...>, Caster::Name)
|
|
553
|
+
|
|
554
|
+
bool from_python(handle src, uint32_t flags, cleanup_list* cleanup) noexcept
|
|
555
|
+
{
|
|
556
|
+
Caster caster;
|
|
557
|
+
if (!caster.from_python(src, flags, cleanup))
|
|
558
|
+
return false;
|
|
559
|
+
value = Value(caster.operator cast_t<std::variant<Ts...>>());
|
|
560
|
+
return true;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
static handle from_cpp(const Value& value, rv_policy policy, cleanup_list* cleanup) noexcept
|
|
564
|
+
{
|
|
565
|
+
return Caster::from_cpp(value.as_variant(), policy, cleanup);
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
// As its T. An empty Indirect, which Python cannot make, is None
|
|
570
|
+
template <typename T>
|
|
571
|
+
struct type_caster<dataclass_native::Indirect<T>>
|
|
572
|
+
{
|
|
573
|
+
using Caster = make_caster<T>;
|
|
574
|
+
NB_TYPE_CASTER(dataclass_native::Indirect<T>, Caster::Name)
|
|
575
|
+
|
|
576
|
+
bool from_python(handle src, uint32_t flags, cleanup_list* cleanup) noexcept
|
|
577
|
+
{
|
|
578
|
+
Caster caster;
|
|
579
|
+
if (!caster.from_python(src, flags_for_local_caster<T>(flags), cleanup) || !caster.template can_cast<T>())
|
|
580
|
+
return false;
|
|
581
|
+
value = Value(caster.operator cast_t<T>());
|
|
582
|
+
return true;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
static handle from_cpp(const Value& value, rv_policy policy, cleanup_list* cleanup) noexcept
|
|
586
|
+
{
|
|
587
|
+
if (!value)
|
|
588
|
+
return none().release();
|
|
589
|
+
return Caster::from_cpp(*value, policy, cleanup);
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
NAMESPACE_END(detail)
|
|
594
|
+
NAMESPACE_END(NB_NAMESPACE)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// How the generated structs and enums are described to code which is written for any of them, such as msgpack.hpp
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <array>
|
|
5
|
+
#include <string_view>
|
|
6
|
+
#include <tuple>
|
|
7
|
+
|
|
8
|
+
namespace dataclass_native
|
|
9
|
+
{
|
|
10
|
+
// Specialised, in a generated header, for each of its structs and enums:
|
|
11
|
+
//
|
|
12
|
+
// of a struct name The class's name
|
|
13
|
+
// tag What names it when serialised
|
|
14
|
+
// shares Whether one may hold, however deeply, a Shared value
|
|
15
|
+
// shared The classes of those values, as Types
|
|
16
|
+
// arrow Whether it may be held in an Arrow table: not if it, or a class it may share, holds
|
|
17
|
+
// itself
|
|
18
|
+
// fields A std::tuple of Field
|
|
19
|
+
//
|
|
20
|
+
// of an enum name, tag
|
|
21
|
+
// members A std::array of Member
|
|
22
|
+
template <class T>
|
|
23
|
+
struct Reflect;
|
|
24
|
+
|
|
25
|
+
template <class S, class T>
|
|
26
|
+
struct Field
|
|
27
|
+
{
|
|
28
|
+
// As in Python: not the member's name, where that is a C++ keyword
|
|
29
|
+
std::string_view name;
|
|
30
|
+
T S::* member;
|
|
31
|
+
// Else the field must be given, to make one in Python
|
|
32
|
+
bool has_default;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
template <class S, class T>
|
|
36
|
+
Field(std::string_view, T S::*, bool) -> Field<S, T>;
|
|
37
|
+
|
|
38
|
+
// Some types
|
|
39
|
+
template <class... Ts>
|
|
40
|
+
struct Types
|
|
41
|
+
{
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
template <class E>
|
|
45
|
+
struct Member
|
|
46
|
+
{
|
|
47
|
+
E value;
|
|
48
|
+
std::string_view name;
|
|
49
|
+
// What it is written as in JSON, and whether as a string: its value, if every member's is a string or an
|
|
50
|
+
// integer, else its name
|
|
51
|
+
std::string_view text;
|
|
52
|
+
bool quoted;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
template <class E>
|
|
56
|
+
Member(E, std::string_view, std::string_view, bool) -> Member<E>;
|
|
57
|
+
|
|
58
|
+
template <class T>
|
|
59
|
+
concept Struct = requires { Reflect<T>::fields; };
|
|
60
|
+
|
|
61
|
+
template <class T>
|
|
62
|
+
concept Enum = requires { Reflect<T>::members; };
|
|
63
|
+
} // namespace dataclass_native
|