|
3 | 3 | #include "DolphinDB.h" |
4 | 4 | #include "Concurrent.h" |
5 | 5 | #include "MultithreadedTableWriter.h" |
| 6 | + |
6 | 7 | #include "pybind11/numpy.h" |
7 | | -#include "pybind11/embed.h" |
8 | | -#include "pybind11/stl.h" |
9 | | -#include "Wrappers.h" |
10 | 8 |
|
11 | 9 | #ifdef _MSC_VER |
12 | 10 | #ifdef _USRDLL |
|
20 | 18 |
|
21 | 19 | namespace dolphindb { |
22 | 20 |
|
23 | | - |
24 | | - |
25 | | -#define EXPARAM_DEFAULT INT_MIN |
26 | | - |
27 | | - |
28 | | -struct HIDEVISIBILITY Preserved { |
29 | | - // instantiation only once for frequently use |
30 | | - bool np_above_1_20_; |
31 | | - bool pd_above_2_0_; |
32 | | - bool pd_above_1_2_; |
33 | | - bool pyarrow_import_; |
34 | | - bool has_arrow_; |
35 | | - // modules and methods |
36 | | - py::object numpy_; // module |
37 | | - //static py::object isnan_; // func |
38 | | - //static py::object sum_; // func |
39 | | - py::object datetime64_; // type, equal to np.datetime64 |
40 | | - py::object nan_; |
41 | | - py::object pandas_; // module |
42 | | - py::object pyarrow_; // pyarrow |
43 | | - |
44 | | - // pandas types (use py::isinstance) |
45 | | - py::object pdseries_; |
46 | | - py::object pddataframe_; |
47 | | - py::object pdNaT_; |
48 | | - py::object pdNA_;//np.nan=pd.NA |
49 | | - py::object pdtimestamp_; |
50 | | - py::object pdindex_; |
51 | | - py::object pdextensiondtype_; |
52 | | - |
53 | | - // pandas extension dtypes (use equal) |
54 | | - py::object pdBooleanDtype_; |
55 | | - py::object pdFloat32Dtype_; |
56 | | - py::object pdFloat64Dtype_; |
57 | | - py::object pdInt8Dtype_; |
58 | | - py::object pdInt16Dtype_; |
59 | | - py::object pdInt32Dtype_; |
60 | | - py::object pdInt64Dtype_; |
61 | | - py::object pdStringDtype_; |
62 | | - |
63 | | - // numpy dtypes (instances of dtypes, use equal) |
64 | | - py::object nparray_; |
65 | | - py::object npmatrix_; |
66 | | - py::object npbool_; |
67 | | - py::object npint8_; |
68 | | - py::object npint16_; |
69 | | - py::object npint32_; |
70 | | - py::object npint64_; |
71 | | - py::object npfloat32_; |
72 | | - py::object npfloat64_; |
73 | | - //npstr has many format, like U4,U8..., they have same type |
74 | | - py::object npstrType_; |
75 | | - |
76 | | - py::object npdatetime64M_ins_; |
77 | | - py::object npdatetime64D_ins_; |
78 | | - py::object npdatetime64m_ins_; |
79 | | - py::object npdatetime64s_ins_; |
80 | | - py::object npdatetime64h_ins_; |
81 | | - py::object npdatetime64ms_ins_; |
82 | | - py::object npdatetime64us_ins_; |
83 | | - py::object npdatetime64ns_ins_; |
84 | | - py::object npdatetime64_ins_; |
85 | | - |
86 | | - py::object npobject_; |
87 | | - |
88 | | - py::object pynone_; |
89 | | - py::object pybool_; |
90 | | - py::object pyint_; |
91 | | - py::object pyfloat_; |
92 | | - py::object pystr_; |
93 | | - py::object pybytes_; |
94 | | - py::object pyset_; |
95 | | - py::object pytuple_; |
96 | | - py::object pylist_; |
97 | | - py::object pydict_; |
98 | | - |
99 | | - py::object m_decimal_; |
100 | | - py::object decimal_; |
101 | | - |
102 | | - // pyarrow types |
103 | | - py::object pdarrowdtype_; |
104 | | - py::object paboolean_; |
105 | | - py::object paint8_; |
106 | | - py::object paint16_; |
107 | | - py::object paint32_; |
108 | | - py::object paint64_; |
109 | | - py::object padate32_; |
110 | | - py::object patime32_ms_; |
111 | | - py::object patime32_s_; |
112 | | - py::object patime64_ns_; |
113 | | - py::object patimestamp_ns_; |
114 | | - py::object patimestamp_ms_; |
115 | | - py::object patimestamp_s_; |
116 | | - py::object pafloat32_; |
117 | | - py::object pafloat64_; |
118 | | - py::object padictionary_int32_utf8_; |
119 | | - py::object paDictionaryType_; |
120 | | - py::object pautf8_; |
121 | | - py::object pafixed_size_binary_16_; |
122 | | - py::object palarge_binary_; |
123 | | - py::object padecimal128_; |
124 | | - py::object palist_; |
125 | | - py::object pachunkedarray_; |
126 | | - |
127 | | - py::object npdatetime64M_(){ |
128 | | - if(np_above_1_20_) return npdatetime64M_ins_; |
129 | | - else return name2dtype("datetime64[M]"); |
130 | | - } |
131 | | - py::object npdatetime64D_(){ |
132 | | - if(np_above_1_20_) return npdatetime64D_ins_; |
133 | | - else return name2dtype("datetime64[D]"); |
134 | | - } |
135 | | - py::object npdatetime64m_(){ |
136 | | - if(np_above_1_20_) return npdatetime64m_ins_; |
137 | | - else return name2dtype("datetime64[m]"); |
138 | | - } |
139 | | - py::object npdatetime64s_(){ |
140 | | - if(np_above_1_20_) return npdatetime64s_ins_; |
141 | | - else return name2dtype("datetime64[s]"); |
142 | | - } |
143 | | - py::object npdatetime64h_(){ |
144 | | - if(np_above_1_20_) return npdatetime64h_ins_; |
145 | | - else return name2dtype("datetime64[h]"); |
146 | | - } |
147 | | - py::object npdatetime64ms_(){ |
148 | | - if(np_above_1_20_) return npdatetime64ms_ins_; |
149 | | - else return name2dtype("datetime64[ms]"); |
150 | | - } |
151 | | - py::object npdatetime64us_(){ |
152 | | - if(np_above_1_20_) return npdatetime64us_ins_; |
153 | | - else return name2dtype("datetime64[us]"); |
154 | | - } |
155 | | - py::object npdatetime64ns_(){ |
156 | | - if(np_above_1_20_) return npdatetime64ns_ins_; |
157 | | - else return name2dtype("datetime64[ns]"); |
158 | | - } |
159 | | - py::object npdatetime64_(){ |
160 | | - if(np_above_1_20_) return npdatetime64_ins_; |
161 | | - else return name2dtype("datetime64"); |
162 | | - } |
163 | | - |
164 | | - Preserved(); |
165 | | - |
166 | | - py::object name2dtype(const char *pname){ |
167 | | - py::object type = py::reinterpret_borrow<py::object>(numpy_.attr("dtype")(pname)); |
168 | | - // py::dtype type(pname); |
169 | | - //py::object dtname=py::getattr(type, "name"); |
170 | | - //std::string name=py::str(dtname); |
171 | | - //printf("DType: %s.",name.data()); |
172 | | - return type; |
173 | | - } |
174 | | - static py::object getType(py::object obj){ |
175 | | - //std::string text=py::str(obj.get_type()); |
176 | | - //printf("Type: %s.",text.data()); |
177 | | - return py::reinterpret_borrow<py::object>(obj.get_type()); |
178 | | - } |
179 | | -}; |
180 | | - |
181 | | -enum CHILD_VECTOR_OPTION { |
182 | | - DISABLE, |
183 | | - ANY_VECTOR, |
184 | | - ARRAY_VECTOR, |
185 | | -}; |
186 | | - |
187 | | -struct TableVectorInfo { |
188 | | - bool is_empty; |
189 | | - std::string colName; |
190 | | -}; |
191 | | - |
192 | | -class EXPORT_DECL TableChecker: public std::map<std::string, Type>{ |
193 | | -public: |
194 | | - TableChecker() {} |
195 | | - TableChecker(const py::object &pydict); |
196 | | -}; |
197 | | - |
198 | 21 | class EXPORT_DECL DdbPythonUtil{ |
199 | 22 | public: |
200 | | - struct ToPythonOption; |
201 | | - struct ToPythonOption{ |
202 | | - bool table2List;//if object is table, false: convert to pandas, true: convert to list |
203 | | - ToPythonOption(){ |
204 | | - table2List = false; |
205 | | - } |
206 | | - ToPythonOption(bool table2Lista){ |
207 | | - table2List = table2Lista; |
208 | | - } |
209 | | - }; |
210 | | - |
211 | | - static ConstantSP toDolphinDB(py::object obj, Type typeIndicator = {DT_UNK, EXPARAM_DEFAULT}, TableChecker checker = TableChecker()); |
212 | | - static py::object toPython(ConstantSP obj, bool tableFlag=false, ToPythonOption *poption = NULL); |
213 | | - static ConstantSP toDolphinDB_Scalar(py::object obj, Type typeIndicator = {DT_UNK, EXPARAM_DEFAULT}); |
214 | | - static ConstantSP toDolphinDB_Vector(py::object obj, Type typeIndicator = {DT_UNK, EXPARAM_DEFAULT}, CHILD_VECTOR_OPTION option = CHILD_VECTOR_OPTION::DISABLE); |
215 | 23 | static py::object loadPickleFile(const std::string &filepath); |
216 | | - static void createPyVector(const ConstantSP &obj,py::object &pyObject,bool tableFlag,ToPythonOption *poption); |
217 | | - static Preserved *preserved_; |
218 | | - static void preservedinit() { if(preserved_ == nullptr) preserved_ = new Preserved(); } |
219 | 24 |
|
220 | 25 | protected: |
221 | 26 | friend class PytoDdbRowPool; |
|
0 commit comments