-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Bunch.ts
368 lines (275 loc) · 10.3 KB
/
Bunch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* eslint-disable */
/* NOTE: This file is auto-generated. Do not edit it directly. */
import crypto from 'node:crypto'
import { PythonBridge, NDArray, ArrayLike, SparseMatrix } from '@/sklearn/types'
/**
Container object exposing keys as attributes.
Bunch objects are sometimes used as an output for functions and methods. They extend dictionaries by enabling values to be accessed by key, `bunch\["value\_key"\]`, or by an attribute, `bunch.value\_key`.
Examples
[Python Reference](https://scikit-learn.org/stable/modules/generated/sklearn.utils.Bunch.html)
*/
export class Bunch {
id: string
opts: any
_py: PythonBridge
_isInitialized: boolean = false
_isDisposed: boolean = false
constructor(opts?: {}) {
this.id = `Bunch${crypto.randomUUID().split('-')[0]}`
this.opts = opts || {}
}
get py(): PythonBridge {
return this._py
}
set py(pythonBridge: PythonBridge) {
this._py = pythonBridge
}
/**
Initializes the underlying Python resources.
This instance is not usable until the `Promise` returned by `init()` resolves.
*/
async init(py: PythonBridge): Promise<void> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (this._isInitialized) {
return
}
if (!py) {
throw new Error('Bunch.init requires a PythonBridge instance')
}
this._py = py
await this._py.ex`
import numpy as np
from sklearn.utils import Bunch
try: bridgeBunch
except NameError: bridgeBunch = {}
`
// set up constructor params
await this._py.ex`ctor_Bunch = {}
ctor_Bunch = {k: v for k, v in ctor_Bunch.items() if v is not None}`
await this._py.ex`bridgeBunch[${this.id}] = Bunch(**ctor_Bunch)`
this._isInitialized = true
}
/**
Disposes of the underlying Python resources.
Once `dispose()` is called, the instance is no longer usable.
*/
async dispose() {
if (this._isDisposed) {
return
}
if (!this._isInitialized) {
return
}
await this._py.ex`del bridgeBunch[${this.id}]`
this._isDisposed = true
}
async clear(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before clear()')
}
// set up method params
await this._py.ex`pms_Bunch_clear = {}
pms_Bunch_clear = {k: v for k, v in pms_Bunch_clear.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_clear = bridgeBunch[${this.id}].clear(**pms_Bunch_clear)`
// convert the result from python to node.js
return this
._py`res_Bunch_clear.tolist() if hasattr(res_Bunch_clear, 'tolist') else res_Bunch_clear`
}
async copy(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before copy()')
}
// set up method params
await this._py.ex`pms_Bunch_copy = {}
pms_Bunch_copy = {k: v for k, v in pms_Bunch_copy.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_copy = bridgeBunch[${this.id}].copy(**pms_Bunch_copy)`
// convert the result from python to node.js
return this
._py`res_Bunch_copy.tolist() if hasattr(res_Bunch_copy, 'tolist') else res_Bunch_copy`
}
/**
Create a new dictionary with keys from iterable and values set to value.
*/
async fromkeys(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before fromkeys()')
}
// set up method params
await this._py.ex`pms_Bunch_fromkeys = {}
pms_Bunch_fromkeys = {k: v for k, v in pms_Bunch_fromkeys.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_fromkeys = bridgeBunch[${this.id}].fromkeys(**pms_Bunch_fromkeys)`
// convert the result from python to node.js
return this
._py`res_Bunch_fromkeys.tolist() if hasattr(res_Bunch_fromkeys, 'tolist') else res_Bunch_fromkeys`
}
/**
Return the value for key if key is in the dictionary, else default.
*/
async get(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before get()')
}
// set up method params
await this._py.ex`pms_Bunch_get = {}
pms_Bunch_get = {k: v for k, v in pms_Bunch_get.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_get = bridgeBunch[${this.id}].get(**pms_Bunch_get)`
// convert the result from python to node.js
return this
._py`res_Bunch_get.tolist() if hasattr(res_Bunch_get, 'tolist') else res_Bunch_get`
}
async items(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before items()')
}
// set up method params
await this._py.ex`pms_Bunch_items = {}
pms_Bunch_items = {k: v for k, v in pms_Bunch_items.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_items = bridgeBunch[${this.id}].items(**pms_Bunch_items)`
// convert the result from python to node.js
return this
._py`res_Bunch_items.tolist() if hasattr(res_Bunch_items, 'tolist') else res_Bunch_items`
}
async keys(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before keys()')
}
// set up method params
await this._py.ex`pms_Bunch_keys = {}
pms_Bunch_keys = {k: v for k, v in pms_Bunch_keys.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_keys = bridgeBunch[${this.id}].keys(**pms_Bunch_keys)`
// convert the result from python to node.js
return this
._py`res_Bunch_keys.tolist() if hasattr(res_Bunch_keys, 'tolist') else res_Bunch_keys`
}
/**
If key is not found, default is returned if given, otherwise KeyError is raised
*/
async pop(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before pop()')
}
// set up method params
await this._py.ex`pms_Bunch_pop = {}
pms_Bunch_pop = {k: v for k, v in pms_Bunch_pop.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_pop = bridgeBunch[${this.id}].pop(**pms_Bunch_pop)`
// convert the result from python to node.js
return this
._py`res_Bunch_pop.tolist() if hasattr(res_Bunch_pop, 'tolist') else res_Bunch_pop`
}
/**
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
*/
async popitem(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before popitem()')
}
// set up method params
await this._py.ex`pms_Bunch_popitem = {}
pms_Bunch_popitem = {k: v for k, v in pms_Bunch_popitem.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_popitem = bridgeBunch[${this.id}].popitem(**pms_Bunch_popitem)`
// convert the result from python to node.js
return this
._py`res_Bunch_popitem.tolist() if hasattr(res_Bunch_popitem, 'tolist') else res_Bunch_popitem`
}
/**
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
*/
async setdefault(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before setdefault()')
}
// set up method params
await this._py.ex`pms_Bunch_setdefault = {}
pms_Bunch_setdefault = {k: v for k, v in pms_Bunch_setdefault.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_setdefault = bridgeBunch[${this.id}].setdefault(**pms_Bunch_setdefault)`
// convert the result from python to node.js
return this
._py`res_Bunch_setdefault.tolist() if hasattr(res_Bunch_setdefault, 'tolist') else res_Bunch_setdefault`
}
/**
If E is present and has a .keys() method, then does: for k in E: D\[k\] = E\[k\] If E is present and lacks a .keys() method, then does: for k, v in E: D\[k\] = v In either case, this is followed by: for k in F: D\[k\] = F\[k\]
*/
async update(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before update()')
}
// set up method params
await this._py.ex`pms_Bunch_update = {}
pms_Bunch_update = {k: v for k, v in pms_Bunch_update.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_update = bridgeBunch[${this.id}].update(**pms_Bunch_update)`
// convert the result from python to node.js
return this
._py`res_Bunch_update.tolist() if hasattr(res_Bunch_update, 'tolist') else res_Bunch_update`
}
async values(opts: {}): Promise<any> {
if (this._isDisposed) {
throw new Error('This Bunch instance has already been disposed')
}
if (!this._isInitialized) {
throw new Error('Bunch must call init() before values()')
}
// set up method params
await this._py.ex`pms_Bunch_values = {}
pms_Bunch_values = {k: v for k, v in pms_Bunch_values.items() if v is not None}`
// invoke method
await this._py
.ex`res_Bunch_values = bridgeBunch[${this.id}].values(**pms_Bunch_values)`
// convert the result from python to node.js
return this
._py`res_Bunch_values.tolist() if hasattr(res_Bunch_values, 'tolist') else res_Bunch_values`
}
}