maxframe 0.1.0b5__cp38-cp38-macosx_11_0_arm64.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 maxframe might be problematic. Click here for more details.

Files changed (647) hide show
  1. maxframe/__init__.py +32 -0
  2. maxframe/_utils.cpython-38-darwin.so +0 -0
  3. maxframe/_utils.pxd +33 -0
  4. maxframe/_utils.pyx +547 -0
  5. maxframe/codegen.py +528 -0
  6. maxframe/config/__init__.py +15 -0
  7. maxframe/config/config.py +443 -0
  8. maxframe/config/tests/__init__.py +13 -0
  9. maxframe/config/tests/test_config.py +103 -0
  10. maxframe/config/tests/test_validators.py +34 -0
  11. maxframe/config/validators.py +57 -0
  12. maxframe/conftest.py +139 -0
  13. maxframe/core/__init__.py +65 -0
  14. maxframe/core/base.py +156 -0
  15. maxframe/core/entity/__init__.py +44 -0
  16. maxframe/core/entity/chunks.py +68 -0
  17. maxframe/core/entity/core.py +152 -0
  18. maxframe/core/entity/executable.py +337 -0
  19. maxframe/core/entity/fuse.py +73 -0
  20. maxframe/core/entity/objects.py +100 -0
  21. maxframe/core/entity/output_types.py +90 -0
  22. maxframe/core/entity/tileables.py +438 -0
  23. maxframe/core/entity/utils.py +24 -0
  24. maxframe/core/graph/__init__.py +17 -0
  25. maxframe/core/graph/builder/__init__.py +16 -0
  26. maxframe/core/graph/builder/base.py +86 -0
  27. maxframe/core/graph/builder/chunk.py +430 -0
  28. maxframe/core/graph/builder/tileable.py +34 -0
  29. maxframe/core/graph/builder/utils.py +41 -0
  30. maxframe/core/graph/core.cpython-38-darwin.so +0 -0
  31. maxframe/core/graph/core.pyx +467 -0
  32. maxframe/core/graph/entity.py +171 -0
  33. maxframe/core/graph/tests/__init__.py +13 -0
  34. maxframe/core/graph/tests/test_graph.py +205 -0
  35. maxframe/core/mode.py +96 -0
  36. maxframe/core/operator/__init__.py +34 -0
  37. maxframe/core/operator/base.py +450 -0
  38. maxframe/core/operator/core.py +276 -0
  39. maxframe/core/operator/fetch.py +53 -0
  40. maxframe/core/operator/fuse.py +29 -0
  41. maxframe/core/operator/objects.py +72 -0
  42. maxframe/core/operator/shuffle.py +111 -0
  43. maxframe/core/operator/tests/__init__.py +13 -0
  44. maxframe/core/operator/tests/test_core.py +64 -0
  45. maxframe/core/tests/__init__.py +13 -0
  46. maxframe/core/tests/test_mode.py +75 -0
  47. maxframe/dataframe/__init__.py +81 -0
  48. maxframe/dataframe/arithmetic/__init__.py +359 -0
  49. maxframe/dataframe/arithmetic/abs.py +33 -0
  50. maxframe/dataframe/arithmetic/add.py +60 -0
  51. maxframe/dataframe/arithmetic/arccos.py +28 -0
  52. maxframe/dataframe/arithmetic/arccosh.py +28 -0
  53. maxframe/dataframe/arithmetic/arcsin.py +28 -0
  54. maxframe/dataframe/arithmetic/arcsinh.py +28 -0
  55. maxframe/dataframe/arithmetic/arctan.py +28 -0
  56. maxframe/dataframe/arithmetic/arctanh.py +28 -0
  57. maxframe/dataframe/arithmetic/around.py +152 -0
  58. maxframe/dataframe/arithmetic/bitwise_and.py +46 -0
  59. maxframe/dataframe/arithmetic/bitwise_or.py +50 -0
  60. maxframe/dataframe/arithmetic/bitwise_xor.py +46 -0
  61. maxframe/dataframe/arithmetic/ceil.py +28 -0
  62. maxframe/dataframe/arithmetic/core.py +342 -0
  63. maxframe/dataframe/arithmetic/cos.py +28 -0
  64. maxframe/dataframe/arithmetic/cosh.py +28 -0
  65. maxframe/dataframe/arithmetic/degrees.py +28 -0
  66. maxframe/dataframe/arithmetic/docstring.py +442 -0
  67. maxframe/dataframe/arithmetic/equal.py +56 -0
  68. maxframe/dataframe/arithmetic/exp.py +28 -0
  69. maxframe/dataframe/arithmetic/exp2.py +28 -0
  70. maxframe/dataframe/arithmetic/expm1.py +28 -0
  71. maxframe/dataframe/arithmetic/floor.py +28 -0
  72. maxframe/dataframe/arithmetic/floordiv.py +64 -0
  73. maxframe/dataframe/arithmetic/greater.py +57 -0
  74. maxframe/dataframe/arithmetic/greater_equal.py +57 -0
  75. maxframe/dataframe/arithmetic/invert.py +33 -0
  76. maxframe/dataframe/arithmetic/is_ufuncs.py +62 -0
  77. maxframe/dataframe/arithmetic/less.py +57 -0
  78. maxframe/dataframe/arithmetic/less_equal.py +57 -0
  79. maxframe/dataframe/arithmetic/log.py +28 -0
  80. maxframe/dataframe/arithmetic/log10.py +28 -0
  81. maxframe/dataframe/arithmetic/log2.py +28 -0
  82. maxframe/dataframe/arithmetic/mod.py +60 -0
  83. maxframe/dataframe/arithmetic/multiply.py +60 -0
  84. maxframe/dataframe/arithmetic/negative.py +33 -0
  85. maxframe/dataframe/arithmetic/not_equal.py +56 -0
  86. maxframe/dataframe/arithmetic/power.py +68 -0
  87. maxframe/dataframe/arithmetic/radians.py +28 -0
  88. maxframe/dataframe/arithmetic/sin.py +28 -0
  89. maxframe/dataframe/arithmetic/sinh.py +28 -0
  90. maxframe/dataframe/arithmetic/sqrt.py +28 -0
  91. maxframe/dataframe/arithmetic/subtract.py +64 -0
  92. maxframe/dataframe/arithmetic/tan.py +28 -0
  93. maxframe/dataframe/arithmetic/tanh.py +28 -0
  94. maxframe/dataframe/arithmetic/tests/__init__.py +13 -0
  95. maxframe/dataframe/arithmetic/tests/test_arithmetic.py +695 -0
  96. maxframe/dataframe/arithmetic/truediv.py +64 -0
  97. maxframe/dataframe/arithmetic/trunc.py +28 -0
  98. maxframe/dataframe/arrays.py +864 -0
  99. maxframe/dataframe/core.py +2417 -0
  100. maxframe/dataframe/datasource/__init__.py +15 -0
  101. maxframe/dataframe/datasource/core.py +81 -0
  102. maxframe/dataframe/datasource/dataframe.py +59 -0
  103. maxframe/dataframe/datasource/date_range.py +504 -0
  104. maxframe/dataframe/datasource/from_index.py +54 -0
  105. maxframe/dataframe/datasource/from_records.py +107 -0
  106. maxframe/dataframe/datasource/from_tensor.py +419 -0
  107. maxframe/dataframe/datasource/index.py +117 -0
  108. maxframe/dataframe/datasource/read_csv.py +528 -0
  109. maxframe/dataframe/datasource/read_odps_query.py +299 -0
  110. maxframe/dataframe/datasource/read_odps_table.py +253 -0
  111. maxframe/dataframe/datasource/read_parquet.py +421 -0
  112. maxframe/dataframe/datasource/series.py +55 -0
  113. maxframe/dataframe/datasource/tests/__init__.py +13 -0
  114. maxframe/dataframe/datasource/tests/test_datasource.py +401 -0
  115. maxframe/dataframe/datastore/__init__.py +26 -0
  116. maxframe/dataframe/datastore/core.py +19 -0
  117. maxframe/dataframe/datastore/to_csv.py +227 -0
  118. maxframe/dataframe/datastore/to_odps.py +162 -0
  119. maxframe/dataframe/extensions/__init__.py +41 -0
  120. maxframe/dataframe/extensions/accessor.py +50 -0
  121. maxframe/dataframe/extensions/reshuffle.py +83 -0
  122. maxframe/dataframe/extensions/tests/__init__.py +13 -0
  123. maxframe/dataframe/extensions/tests/test_extensions.py +38 -0
  124. maxframe/dataframe/fetch/__init__.py +15 -0
  125. maxframe/dataframe/fetch/core.py +86 -0
  126. maxframe/dataframe/groupby/__init__.py +82 -0
  127. maxframe/dataframe/groupby/aggregation.py +350 -0
  128. maxframe/dataframe/groupby/apply.py +251 -0
  129. maxframe/dataframe/groupby/core.py +179 -0
  130. maxframe/dataframe/groupby/cum.py +124 -0
  131. maxframe/dataframe/groupby/fill.py +141 -0
  132. maxframe/dataframe/groupby/getitem.py +92 -0
  133. maxframe/dataframe/groupby/head.py +105 -0
  134. maxframe/dataframe/groupby/sample.py +214 -0
  135. maxframe/dataframe/groupby/tests/__init__.py +13 -0
  136. maxframe/dataframe/groupby/tests/test_groupby.py +374 -0
  137. maxframe/dataframe/groupby/transform.py +255 -0
  138. maxframe/dataframe/indexing/__init__.py +84 -0
  139. maxframe/dataframe/indexing/add_prefix_suffix.py +110 -0
  140. maxframe/dataframe/indexing/align.py +349 -0
  141. maxframe/dataframe/indexing/at.py +83 -0
  142. maxframe/dataframe/indexing/getitem.py +204 -0
  143. maxframe/dataframe/indexing/iat.py +37 -0
  144. maxframe/dataframe/indexing/iloc.py +566 -0
  145. maxframe/dataframe/indexing/insert.py +86 -0
  146. maxframe/dataframe/indexing/loc.py +411 -0
  147. maxframe/dataframe/indexing/reindex.py +526 -0
  148. maxframe/dataframe/indexing/rename.py +462 -0
  149. maxframe/dataframe/indexing/rename_axis.py +209 -0
  150. maxframe/dataframe/indexing/reset_index.py +402 -0
  151. maxframe/dataframe/indexing/sample.py +221 -0
  152. maxframe/dataframe/indexing/set_axis.py +194 -0
  153. maxframe/dataframe/indexing/set_index.py +61 -0
  154. maxframe/dataframe/indexing/setitem.py +130 -0
  155. maxframe/dataframe/indexing/tests/__init__.py +13 -0
  156. maxframe/dataframe/indexing/tests/test_indexing.py +488 -0
  157. maxframe/dataframe/indexing/where.py +308 -0
  158. maxframe/dataframe/initializer.py +288 -0
  159. maxframe/dataframe/merge/__init__.py +32 -0
  160. maxframe/dataframe/merge/append.py +121 -0
  161. maxframe/dataframe/merge/concat.py +325 -0
  162. maxframe/dataframe/merge/merge.py +593 -0
  163. maxframe/dataframe/merge/tests/__init__.py +13 -0
  164. maxframe/dataframe/merge/tests/test_merge.py +215 -0
  165. maxframe/dataframe/misc/__init__.py +134 -0
  166. maxframe/dataframe/misc/_duplicate.py +46 -0
  167. maxframe/dataframe/misc/accessor.py +276 -0
  168. maxframe/dataframe/misc/apply.py +692 -0
  169. maxframe/dataframe/misc/astype.py +236 -0
  170. maxframe/dataframe/misc/case_when.py +141 -0
  171. maxframe/dataframe/misc/check_monotonic.py +84 -0
  172. maxframe/dataframe/misc/cut.py +383 -0
  173. maxframe/dataframe/misc/datetimes.py +79 -0
  174. maxframe/dataframe/misc/describe.py +108 -0
  175. maxframe/dataframe/misc/diff.py +210 -0
  176. maxframe/dataframe/misc/drop.py +440 -0
  177. maxframe/dataframe/misc/drop_duplicates.py +248 -0
  178. maxframe/dataframe/misc/duplicated.py +292 -0
  179. maxframe/dataframe/misc/eval.py +728 -0
  180. maxframe/dataframe/misc/explode.py +171 -0
  181. maxframe/dataframe/misc/get_dummies.py +208 -0
  182. maxframe/dataframe/misc/isin.py +217 -0
  183. maxframe/dataframe/misc/map.py +236 -0
  184. maxframe/dataframe/misc/melt.py +162 -0
  185. maxframe/dataframe/misc/memory_usage.py +248 -0
  186. maxframe/dataframe/misc/pct_change.py +150 -0
  187. maxframe/dataframe/misc/pivot_table.py +262 -0
  188. maxframe/dataframe/misc/qcut.py +104 -0
  189. maxframe/dataframe/misc/select_dtypes.py +104 -0
  190. maxframe/dataframe/misc/shift.py +256 -0
  191. maxframe/dataframe/misc/stack.py +238 -0
  192. maxframe/dataframe/misc/string_.py +221 -0
  193. maxframe/dataframe/misc/tests/__init__.py +13 -0
  194. maxframe/dataframe/misc/tests/test_misc.py +468 -0
  195. maxframe/dataframe/misc/to_numeric.py +178 -0
  196. maxframe/dataframe/misc/transform.py +361 -0
  197. maxframe/dataframe/misc/transpose.py +136 -0
  198. maxframe/dataframe/misc/value_counts.py +182 -0
  199. maxframe/dataframe/missing/__init__.py +53 -0
  200. maxframe/dataframe/missing/checkna.py +223 -0
  201. maxframe/dataframe/missing/dropna.py +280 -0
  202. maxframe/dataframe/missing/fillna.py +275 -0
  203. maxframe/dataframe/missing/replace.py +439 -0
  204. maxframe/dataframe/missing/tests/__init__.py +13 -0
  205. maxframe/dataframe/missing/tests/test_missing.py +89 -0
  206. maxframe/dataframe/operators.py +273 -0
  207. maxframe/dataframe/plotting/__init__.py +40 -0
  208. maxframe/dataframe/plotting/core.py +78 -0
  209. maxframe/dataframe/plotting/tests/__init__.py +13 -0
  210. maxframe/dataframe/plotting/tests/test_plotting.py +136 -0
  211. maxframe/dataframe/reduction/__init__.py +107 -0
  212. maxframe/dataframe/reduction/aggregation.py +344 -0
  213. maxframe/dataframe/reduction/all.py +78 -0
  214. maxframe/dataframe/reduction/any.py +78 -0
  215. maxframe/dataframe/reduction/core.py +837 -0
  216. maxframe/dataframe/reduction/count.py +59 -0
  217. maxframe/dataframe/reduction/cummax.py +30 -0
  218. maxframe/dataframe/reduction/cummin.py +30 -0
  219. maxframe/dataframe/reduction/cumprod.py +30 -0
  220. maxframe/dataframe/reduction/cumsum.py +30 -0
  221. maxframe/dataframe/reduction/custom_reduction.py +42 -0
  222. maxframe/dataframe/reduction/kurtosis.py +104 -0
  223. maxframe/dataframe/reduction/max.py +65 -0
  224. maxframe/dataframe/reduction/mean.py +61 -0
  225. maxframe/dataframe/reduction/min.py +65 -0
  226. maxframe/dataframe/reduction/nunique.py +141 -0
  227. maxframe/dataframe/reduction/prod.py +76 -0
  228. maxframe/dataframe/reduction/reduction_size.py +36 -0
  229. maxframe/dataframe/reduction/sem.py +69 -0
  230. maxframe/dataframe/reduction/skew.py +89 -0
  231. maxframe/dataframe/reduction/std.py +53 -0
  232. maxframe/dataframe/reduction/str_concat.py +48 -0
  233. maxframe/dataframe/reduction/sum.py +77 -0
  234. maxframe/dataframe/reduction/tests/__init__.py +13 -0
  235. maxframe/dataframe/reduction/tests/test_reduction.py +486 -0
  236. maxframe/dataframe/reduction/unique.py +90 -0
  237. maxframe/dataframe/reduction/var.py +72 -0
  238. maxframe/dataframe/sort/__init__.py +34 -0
  239. maxframe/dataframe/sort/core.py +36 -0
  240. maxframe/dataframe/sort/sort_index.py +153 -0
  241. maxframe/dataframe/sort/sort_values.py +311 -0
  242. maxframe/dataframe/sort/tests/__init__.py +13 -0
  243. maxframe/dataframe/sort/tests/test_sort.py +81 -0
  244. maxframe/dataframe/statistics/__init__.py +33 -0
  245. maxframe/dataframe/statistics/corr.py +280 -0
  246. maxframe/dataframe/statistics/quantile.py +341 -0
  247. maxframe/dataframe/statistics/tests/__init__.py +13 -0
  248. maxframe/dataframe/statistics/tests/test_statistics.py +82 -0
  249. maxframe/dataframe/tests/__init__.py +13 -0
  250. maxframe/dataframe/tests/test_initializer.py +29 -0
  251. maxframe/dataframe/tseries/__init__.py +13 -0
  252. maxframe/dataframe/tseries/tests/__init__.py +13 -0
  253. maxframe/dataframe/tseries/tests/test_tseries.py +30 -0
  254. maxframe/dataframe/tseries/to_datetime.py +297 -0
  255. maxframe/dataframe/ufunc/__init__.py +27 -0
  256. maxframe/dataframe/ufunc/tensor.py +54 -0
  257. maxframe/dataframe/ufunc/ufunc.py +52 -0
  258. maxframe/dataframe/utils.py +1267 -0
  259. maxframe/dataframe/window/__init__.py +29 -0
  260. maxframe/dataframe/window/aggregation.py +96 -0
  261. maxframe/dataframe/window/core.py +69 -0
  262. maxframe/dataframe/window/ewm.py +249 -0
  263. maxframe/dataframe/window/expanding.py +147 -0
  264. maxframe/dataframe/window/rolling.py +376 -0
  265. maxframe/dataframe/window/tests/__init__.py +13 -0
  266. maxframe/dataframe/window/tests/test_ewm.py +70 -0
  267. maxframe/dataframe/window/tests/test_expanding.py +66 -0
  268. maxframe/dataframe/window/tests/test_rolling.py +57 -0
  269. maxframe/env.py +33 -0
  270. maxframe/errors.py +21 -0
  271. maxframe/extension.py +81 -0
  272. maxframe/learn/__init__.py +17 -0
  273. maxframe/learn/contrib/__init__.py +17 -0
  274. maxframe/learn/contrib/pytorch/__init__.py +16 -0
  275. maxframe/learn/contrib/pytorch/run_function.py +110 -0
  276. maxframe/learn/contrib/pytorch/run_script.py +102 -0
  277. maxframe/learn/contrib/pytorch/tests/__init__.py +13 -0
  278. maxframe/learn/contrib/pytorch/tests/test_pytorch.py +42 -0
  279. maxframe/learn/contrib/utils.py +52 -0
  280. maxframe/learn/contrib/xgboost/__init__.py +26 -0
  281. maxframe/learn/contrib/xgboost/classifier.py +86 -0
  282. maxframe/learn/contrib/xgboost/core.py +156 -0
  283. maxframe/learn/contrib/xgboost/dmatrix.py +150 -0
  284. maxframe/learn/contrib/xgboost/predict.py +138 -0
  285. maxframe/learn/contrib/xgboost/regressor.py +78 -0
  286. maxframe/learn/contrib/xgboost/tests/__init__.py +13 -0
  287. maxframe/learn/contrib/xgboost/tests/test_core.py +43 -0
  288. maxframe/learn/contrib/xgboost/train.py +121 -0
  289. maxframe/learn/utils/__init__.py +15 -0
  290. maxframe/learn/utils/core.py +29 -0
  291. maxframe/lib/__init__.py +15 -0
  292. maxframe/lib/aio/__init__.py +27 -0
  293. maxframe/lib/aio/_runners.py +162 -0
  294. maxframe/lib/aio/_threads.py +35 -0
  295. maxframe/lib/aio/base.py +82 -0
  296. maxframe/lib/aio/file.py +85 -0
  297. maxframe/lib/aio/isolation.py +100 -0
  298. maxframe/lib/aio/lru.py +242 -0
  299. maxframe/lib/aio/parallelism.py +37 -0
  300. maxframe/lib/aio/tests/__init__.py +13 -0
  301. maxframe/lib/aio/tests/test_aio_file.py +55 -0
  302. maxframe/lib/compression.py +55 -0
  303. maxframe/lib/cython/__init__.py +13 -0
  304. maxframe/lib/cython/libcpp.pxd +30 -0
  305. maxframe/lib/filesystem/__init__.py +21 -0
  306. maxframe/lib/filesystem/_glob.py +173 -0
  307. maxframe/lib/filesystem/_oss_lib/__init__.py +13 -0
  308. maxframe/lib/filesystem/_oss_lib/common.py +198 -0
  309. maxframe/lib/filesystem/_oss_lib/glob.py +147 -0
  310. maxframe/lib/filesystem/_oss_lib/handle.py +156 -0
  311. maxframe/lib/filesystem/arrow.py +236 -0
  312. maxframe/lib/filesystem/base.py +263 -0
  313. maxframe/lib/filesystem/core.py +95 -0
  314. maxframe/lib/filesystem/fsmap.py +164 -0
  315. maxframe/lib/filesystem/hdfs.py +31 -0
  316. maxframe/lib/filesystem/local.py +112 -0
  317. maxframe/lib/filesystem/oss.py +157 -0
  318. maxframe/lib/filesystem/tests/__init__.py +13 -0
  319. maxframe/lib/filesystem/tests/test_filesystem.py +223 -0
  320. maxframe/lib/filesystem/tests/test_oss.py +182 -0
  321. maxframe/lib/functools_compat.py +81 -0
  322. maxframe/lib/mmh3.cpython-38-darwin.so +0 -0
  323. maxframe/lib/mmh3_src/MurmurHash3.cpp +339 -0
  324. maxframe/lib/mmh3_src/MurmurHash3.h +43 -0
  325. maxframe/lib/mmh3_src/mmh3module.cpp +387 -0
  326. maxframe/lib/sparse/__init__.py +861 -0
  327. maxframe/lib/sparse/array.py +1604 -0
  328. maxframe/lib/sparse/core.py +92 -0
  329. maxframe/lib/sparse/matrix.py +241 -0
  330. maxframe/lib/sparse/tests/__init__.py +15 -0
  331. maxframe/lib/sparse/tests/test_sparse.py +476 -0
  332. maxframe/lib/sparse/vector.py +150 -0
  333. maxframe/lib/tblib/LICENSE +20 -0
  334. maxframe/lib/tblib/__init__.py +327 -0
  335. maxframe/lib/tblib/cpython.py +83 -0
  336. maxframe/lib/tblib/decorators.py +44 -0
  337. maxframe/lib/tblib/pickling_support.py +90 -0
  338. maxframe/lib/tests/__init__.py +13 -0
  339. maxframe/lib/tests/test_wrapped_pickle.py +51 -0
  340. maxframe/lib/version.py +620 -0
  341. maxframe/lib/wrapped_pickle.py +139 -0
  342. maxframe/mixin.py +100 -0
  343. maxframe/odpsio/__init__.py +21 -0
  344. maxframe/odpsio/arrow.py +91 -0
  345. maxframe/odpsio/schema.py +364 -0
  346. maxframe/odpsio/tableio.py +322 -0
  347. maxframe/odpsio/tests/__init__.py +13 -0
  348. maxframe/odpsio/tests/test_arrow.py +88 -0
  349. maxframe/odpsio/tests/test_schema.py +297 -0
  350. maxframe/odpsio/tests/test_tableio.py +136 -0
  351. maxframe/odpsio/tests/test_volumeio.py +90 -0
  352. maxframe/odpsio/volumeio.py +95 -0
  353. maxframe/opcodes.py +590 -0
  354. maxframe/protocol.py +415 -0
  355. maxframe/remote/__init__.py +18 -0
  356. maxframe/remote/core.py +210 -0
  357. maxframe/remote/run_script.py +121 -0
  358. maxframe/serialization/__init__.py +26 -0
  359. maxframe/serialization/arrow.py +95 -0
  360. maxframe/serialization/core.cpython-38-darwin.so +0 -0
  361. maxframe/serialization/core.pxd +44 -0
  362. maxframe/serialization/core.pyi +61 -0
  363. maxframe/serialization/core.pyx +1094 -0
  364. maxframe/serialization/exception.py +86 -0
  365. maxframe/serialization/maxframe_objects.py +39 -0
  366. maxframe/serialization/numpy.py +91 -0
  367. maxframe/serialization/pandas.py +202 -0
  368. maxframe/serialization/scipy.py +71 -0
  369. maxframe/serialization/serializables/__init__.py +55 -0
  370. maxframe/serialization/serializables/core.py +262 -0
  371. maxframe/serialization/serializables/field.py +624 -0
  372. maxframe/serialization/serializables/field_type.py +589 -0
  373. maxframe/serialization/serializables/tests/__init__.py +13 -0
  374. maxframe/serialization/serializables/tests/test_field_type.py +121 -0
  375. maxframe/serialization/serializables/tests/test_serializable.py +250 -0
  376. maxframe/serialization/tests/__init__.py +13 -0
  377. maxframe/serialization/tests/test_serial.py +412 -0
  378. maxframe/session.py +1310 -0
  379. maxframe/tensor/__init__.py +183 -0
  380. maxframe/tensor/arithmetic/__init__.py +315 -0
  381. maxframe/tensor/arithmetic/abs.py +68 -0
  382. maxframe/tensor/arithmetic/absolute.py +68 -0
  383. maxframe/tensor/arithmetic/add.py +82 -0
  384. maxframe/tensor/arithmetic/angle.py +72 -0
  385. maxframe/tensor/arithmetic/arccos.py +104 -0
  386. maxframe/tensor/arithmetic/arccosh.py +91 -0
  387. maxframe/tensor/arithmetic/arcsin.py +94 -0
  388. maxframe/tensor/arithmetic/arcsinh.py +86 -0
  389. maxframe/tensor/arithmetic/arctan.py +106 -0
  390. maxframe/tensor/arithmetic/arctan2.py +128 -0
  391. maxframe/tensor/arithmetic/arctanh.py +86 -0
  392. maxframe/tensor/arithmetic/around.py +114 -0
  393. maxframe/tensor/arithmetic/bitand.py +95 -0
  394. maxframe/tensor/arithmetic/bitor.py +102 -0
  395. maxframe/tensor/arithmetic/bitxor.py +95 -0
  396. maxframe/tensor/arithmetic/cbrt.py +66 -0
  397. maxframe/tensor/arithmetic/ceil.py +71 -0
  398. maxframe/tensor/arithmetic/clip.py +165 -0
  399. maxframe/tensor/arithmetic/conj.py +74 -0
  400. maxframe/tensor/arithmetic/copysign.py +78 -0
  401. maxframe/tensor/arithmetic/core.py +544 -0
  402. maxframe/tensor/arithmetic/cos.py +85 -0
  403. maxframe/tensor/arithmetic/cosh.py +72 -0
  404. maxframe/tensor/arithmetic/deg2rad.py +72 -0
  405. maxframe/tensor/arithmetic/degrees.py +77 -0
  406. maxframe/tensor/arithmetic/divide.py +114 -0
  407. maxframe/tensor/arithmetic/equal.py +76 -0
  408. maxframe/tensor/arithmetic/exp.py +106 -0
  409. maxframe/tensor/arithmetic/exp2.py +67 -0
  410. maxframe/tensor/arithmetic/expm1.py +79 -0
  411. maxframe/tensor/arithmetic/fabs.py +74 -0
  412. maxframe/tensor/arithmetic/fix.py +69 -0
  413. maxframe/tensor/arithmetic/float_power.py +103 -0
  414. maxframe/tensor/arithmetic/floor.py +77 -0
  415. maxframe/tensor/arithmetic/floordiv.py +94 -0
  416. maxframe/tensor/arithmetic/fmax.py +105 -0
  417. maxframe/tensor/arithmetic/fmin.py +106 -0
  418. maxframe/tensor/arithmetic/fmod.py +99 -0
  419. maxframe/tensor/arithmetic/frexp.py +92 -0
  420. maxframe/tensor/arithmetic/greater.py +77 -0
  421. maxframe/tensor/arithmetic/greater_equal.py +69 -0
  422. maxframe/tensor/arithmetic/hypot.py +77 -0
  423. maxframe/tensor/arithmetic/i0.py +89 -0
  424. maxframe/tensor/arithmetic/imag.py +67 -0
  425. maxframe/tensor/arithmetic/invert.py +110 -0
  426. maxframe/tensor/arithmetic/isclose.py +115 -0
  427. maxframe/tensor/arithmetic/iscomplex.py +64 -0
  428. maxframe/tensor/arithmetic/isfinite.py +106 -0
  429. maxframe/tensor/arithmetic/isinf.py +103 -0
  430. maxframe/tensor/arithmetic/isnan.py +82 -0
  431. maxframe/tensor/arithmetic/isreal.py +63 -0
  432. maxframe/tensor/arithmetic/ldexp.py +99 -0
  433. maxframe/tensor/arithmetic/less.py +69 -0
  434. maxframe/tensor/arithmetic/less_equal.py +69 -0
  435. maxframe/tensor/arithmetic/log.py +92 -0
  436. maxframe/tensor/arithmetic/log10.py +85 -0
  437. maxframe/tensor/arithmetic/log1p.py +95 -0
  438. maxframe/tensor/arithmetic/log2.py +85 -0
  439. maxframe/tensor/arithmetic/logaddexp.py +80 -0
  440. maxframe/tensor/arithmetic/logaddexp2.py +78 -0
  441. maxframe/tensor/arithmetic/logical_and.py +81 -0
  442. maxframe/tensor/arithmetic/logical_not.py +74 -0
  443. maxframe/tensor/arithmetic/logical_or.py +82 -0
  444. maxframe/tensor/arithmetic/logical_xor.py +88 -0
  445. maxframe/tensor/arithmetic/lshift.py +82 -0
  446. maxframe/tensor/arithmetic/maximum.py +108 -0
  447. maxframe/tensor/arithmetic/minimum.py +108 -0
  448. maxframe/tensor/arithmetic/mod.py +104 -0
  449. maxframe/tensor/arithmetic/modf.py +83 -0
  450. maxframe/tensor/arithmetic/multiply.py +81 -0
  451. maxframe/tensor/arithmetic/nan_to_num.py +99 -0
  452. maxframe/tensor/arithmetic/negative.py +65 -0
  453. maxframe/tensor/arithmetic/nextafter.py +68 -0
  454. maxframe/tensor/arithmetic/not_equal.py +72 -0
  455. maxframe/tensor/arithmetic/positive.py +47 -0
  456. maxframe/tensor/arithmetic/power.py +106 -0
  457. maxframe/tensor/arithmetic/rad2deg.py +71 -0
  458. maxframe/tensor/arithmetic/radians.py +77 -0
  459. maxframe/tensor/arithmetic/real.py +70 -0
  460. maxframe/tensor/arithmetic/reciprocal.py +76 -0
  461. maxframe/tensor/arithmetic/rint.py +68 -0
  462. maxframe/tensor/arithmetic/rshift.py +81 -0
  463. maxframe/tensor/arithmetic/setimag.py +29 -0
  464. maxframe/tensor/arithmetic/setreal.py +29 -0
  465. maxframe/tensor/arithmetic/sign.py +81 -0
  466. maxframe/tensor/arithmetic/signbit.py +65 -0
  467. maxframe/tensor/arithmetic/sin.py +98 -0
  468. maxframe/tensor/arithmetic/sinc.py +102 -0
  469. maxframe/tensor/arithmetic/sinh.py +93 -0
  470. maxframe/tensor/arithmetic/spacing.py +72 -0
  471. maxframe/tensor/arithmetic/sqrt.py +81 -0
  472. maxframe/tensor/arithmetic/square.py +69 -0
  473. maxframe/tensor/arithmetic/subtract.py +81 -0
  474. maxframe/tensor/arithmetic/tan.py +88 -0
  475. maxframe/tensor/arithmetic/tanh.py +92 -0
  476. maxframe/tensor/arithmetic/tests/__init__.py +15 -0
  477. maxframe/tensor/arithmetic/tests/test_arithmetic.py +414 -0
  478. maxframe/tensor/arithmetic/truediv.py +104 -0
  479. maxframe/tensor/arithmetic/trunc.py +72 -0
  480. maxframe/tensor/arithmetic/utils.py +65 -0
  481. maxframe/tensor/array_utils.py +186 -0
  482. maxframe/tensor/base/__init__.py +34 -0
  483. maxframe/tensor/base/astype.py +119 -0
  484. maxframe/tensor/base/atleast_1d.py +74 -0
  485. maxframe/tensor/base/broadcast_to.py +89 -0
  486. maxframe/tensor/base/ravel.py +92 -0
  487. maxframe/tensor/base/tests/__init__.py +13 -0
  488. maxframe/tensor/base/tests/test_base.py +114 -0
  489. maxframe/tensor/base/transpose.py +125 -0
  490. maxframe/tensor/base/unique.py +205 -0
  491. maxframe/tensor/base/where.py +127 -0
  492. maxframe/tensor/core.py +724 -0
  493. maxframe/tensor/datasource/__init__.py +32 -0
  494. maxframe/tensor/datasource/arange.py +156 -0
  495. maxframe/tensor/datasource/array.py +415 -0
  496. maxframe/tensor/datasource/core.py +109 -0
  497. maxframe/tensor/datasource/empty.py +169 -0
  498. maxframe/tensor/datasource/from_dataframe.py +70 -0
  499. maxframe/tensor/datasource/from_dense.py +54 -0
  500. maxframe/tensor/datasource/from_sparse.py +47 -0
  501. maxframe/tensor/datasource/full.py +186 -0
  502. maxframe/tensor/datasource/ones.py +173 -0
  503. maxframe/tensor/datasource/scalar.py +40 -0
  504. maxframe/tensor/datasource/tests/__init__.py +13 -0
  505. maxframe/tensor/datasource/tests/test_datasource.py +278 -0
  506. maxframe/tensor/datasource/zeros.py +188 -0
  507. maxframe/tensor/fetch/__init__.py +15 -0
  508. maxframe/tensor/fetch/core.py +54 -0
  509. maxframe/tensor/indexing/__init__.py +47 -0
  510. maxframe/tensor/indexing/choose.py +196 -0
  511. maxframe/tensor/indexing/compress.py +124 -0
  512. maxframe/tensor/indexing/core.py +190 -0
  513. maxframe/tensor/indexing/extract.py +71 -0
  514. maxframe/tensor/indexing/fill_diagonal.py +183 -0
  515. maxframe/tensor/indexing/flatnonzero.py +60 -0
  516. maxframe/tensor/indexing/getitem.py +175 -0
  517. maxframe/tensor/indexing/nonzero.py +120 -0
  518. maxframe/tensor/indexing/setitem.py +132 -0
  519. maxframe/tensor/indexing/slice.py +29 -0
  520. maxframe/tensor/indexing/take.py +130 -0
  521. maxframe/tensor/indexing/tests/__init__.py +15 -0
  522. maxframe/tensor/indexing/tests/test_indexing.py +234 -0
  523. maxframe/tensor/indexing/unravel_index.py +103 -0
  524. maxframe/tensor/merge/__init__.py +15 -0
  525. maxframe/tensor/merge/stack.py +132 -0
  526. maxframe/tensor/merge/tests/__init__.py +13 -0
  527. maxframe/tensor/merge/tests/test_merge.py +52 -0
  528. maxframe/tensor/operators.py +123 -0
  529. maxframe/tensor/random/__init__.py +168 -0
  530. maxframe/tensor/random/beta.py +87 -0
  531. maxframe/tensor/random/binomial.py +137 -0
  532. maxframe/tensor/random/bytes.py +39 -0
  533. maxframe/tensor/random/chisquare.py +110 -0
  534. maxframe/tensor/random/choice.py +186 -0
  535. maxframe/tensor/random/core.py +234 -0
  536. maxframe/tensor/random/dirichlet.py +123 -0
  537. maxframe/tensor/random/exponential.py +94 -0
  538. maxframe/tensor/random/f.py +135 -0
  539. maxframe/tensor/random/gamma.py +128 -0
  540. maxframe/tensor/random/geometric.py +93 -0
  541. maxframe/tensor/random/gumbel.py +167 -0
  542. maxframe/tensor/random/hypergeometric.py +148 -0
  543. maxframe/tensor/random/laplace.py +133 -0
  544. maxframe/tensor/random/logistic.py +129 -0
  545. maxframe/tensor/random/lognormal.py +159 -0
  546. maxframe/tensor/random/logseries.py +122 -0
  547. maxframe/tensor/random/multinomial.py +133 -0
  548. maxframe/tensor/random/multivariate_normal.py +192 -0
  549. maxframe/tensor/random/negative_binomial.py +125 -0
  550. maxframe/tensor/random/noncentral_chisquare.py +132 -0
  551. maxframe/tensor/random/noncentral_f.py +126 -0
  552. maxframe/tensor/random/normal.py +143 -0
  553. maxframe/tensor/random/pareto.py +140 -0
  554. maxframe/tensor/random/permutation.py +104 -0
  555. maxframe/tensor/random/poisson.py +111 -0
  556. maxframe/tensor/random/power.py +142 -0
  557. maxframe/tensor/random/rand.py +82 -0
  558. maxframe/tensor/random/randint.py +121 -0
  559. maxframe/tensor/random/randn.py +96 -0
  560. maxframe/tensor/random/random_integers.py +123 -0
  561. maxframe/tensor/random/random_sample.py +86 -0
  562. maxframe/tensor/random/rayleigh.py +110 -0
  563. maxframe/tensor/random/shuffle.py +61 -0
  564. maxframe/tensor/random/standard_cauchy.py +105 -0
  565. maxframe/tensor/random/standard_exponential.py +72 -0
  566. maxframe/tensor/random/standard_gamma.py +120 -0
  567. maxframe/tensor/random/standard_normal.py +74 -0
  568. maxframe/tensor/random/standard_t.py +135 -0
  569. maxframe/tensor/random/tests/__init__.py +15 -0
  570. maxframe/tensor/random/tests/test_random.py +167 -0
  571. maxframe/tensor/random/triangular.py +119 -0
  572. maxframe/tensor/random/uniform.py +131 -0
  573. maxframe/tensor/random/vonmises.py +131 -0
  574. maxframe/tensor/random/wald.py +114 -0
  575. maxframe/tensor/random/weibull.py +140 -0
  576. maxframe/tensor/random/zipf.py +122 -0
  577. maxframe/tensor/rechunk/__init__.py +26 -0
  578. maxframe/tensor/rechunk/rechunk.py +43 -0
  579. maxframe/tensor/reduction/__init__.py +66 -0
  580. maxframe/tensor/reduction/all.py +103 -0
  581. maxframe/tensor/reduction/allclose.py +88 -0
  582. maxframe/tensor/reduction/any.py +105 -0
  583. maxframe/tensor/reduction/argmax.py +103 -0
  584. maxframe/tensor/reduction/argmin.py +103 -0
  585. maxframe/tensor/reduction/array_equal.py +64 -0
  586. maxframe/tensor/reduction/core.py +168 -0
  587. maxframe/tensor/reduction/count_nonzero.py +81 -0
  588. maxframe/tensor/reduction/cumprod.py +97 -0
  589. maxframe/tensor/reduction/cumsum.py +101 -0
  590. maxframe/tensor/reduction/max.py +120 -0
  591. maxframe/tensor/reduction/mean.py +123 -0
  592. maxframe/tensor/reduction/min.py +120 -0
  593. maxframe/tensor/reduction/nanargmax.py +82 -0
  594. maxframe/tensor/reduction/nanargmin.py +76 -0
  595. maxframe/tensor/reduction/nancumprod.py +91 -0
  596. maxframe/tensor/reduction/nancumsum.py +94 -0
  597. maxframe/tensor/reduction/nanmax.py +111 -0
  598. maxframe/tensor/reduction/nanmean.py +106 -0
  599. maxframe/tensor/reduction/nanmin.py +111 -0
  600. maxframe/tensor/reduction/nanprod.py +94 -0
  601. maxframe/tensor/reduction/nanstd.py +126 -0
  602. maxframe/tensor/reduction/nansum.py +115 -0
  603. maxframe/tensor/reduction/nanvar.py +149 -0
  604. maxframe/tensor/reduction/prod.py +130 -0
  605. maxframe/tensor/reduction/std.py +134 -0
  606. maxframe/tensor/reduction/sum.py +125 -0
  607. maxframe/tensor/reduction/tests/__init__.py +13 -0
  608. maxframe/tensor/reduction/tests/test_reduction.py +181 -0
  609. maxframe/tensor/reduction/var.py +176 -0
  610. maxframe/tensor/reshape/__init__.py +17 -0
  611. maxframe/tensor/reshape/reshape.py +188 -0
  612. maxframe/tensor/reshape/tests/__init__.py +15 -0
  613. maxframe/tensor/reshape/tests/test_reshape.py +37 -0
  614. maxframe/tensor/statistics/__init__.py +13 -0
  615. maxframe/tensor/statistics/percentile.py +175 -0
  616. maxframe/tensor/statistics/quantile.py +288 -0
  617. maxframe/tensor/ufunc/__init__.py +26 -0
  618. maxframe/tensor/ufunc/ufunc.py +200 -0
  619. maxframe/tensor/utils.py +718 -0
  620. maxframe/tests/__init__.py +13 -0
  621. maxframe/tests/test_codegen.py +69 -0
  622. maxframe/tests/test_protocol.py +144 -0
  623. maxframe/tests/test_utils.py +376 -0
  624. maxframe/tests/utils.py +164 -0
  625. maxframe/typing_.py +37 -0
  626. maxframe/udf.py +134 -0
  627. maxframe/utils.py +1114 -0
  628. maxframe-0.1.0b5.dist-info/METADATA +104 -0
  629. maxframe-0.1.0b5.dist-info/RECORD +647 -0
  630. maxframe-0.1.0b5.dist-info/WHEEL +5 -0
  631. maxframe-0.1.0b5.dist-info/top_level.txt +2 -0
  632. maxframe_client/__init__.py +17 -0
  633. maxframe_client/clients/__init__.py +13 -0
  634. maxframe_client/clients/framedriver.py +118 -0
  635. maxframe_client/clients/spe.py +104 -0
  636. maxframe_client/conftest.py +15 -0
  637. maxframe_client/fetcher.py +264 -0
  638. maxframe_client/session/__init__.py +22 -0
  639. maxframe_client/session/consts.py +36 -0
  640. maxframe_client/session/graph.py +119 -0
  641. maxframe_client/session/odps.py +482 -0
  642. maxframe_client/session/task.py +280 -0
  643. maxframe_client/session/tests/__init__.py +13 -0
  644. maxframe_client/session/tests/test_task.py +85 -0
  645. maxframe_client/tests/__init__.py +13 -0
  646. maxframe_client/tests/test_fetcher.py +89 -0
  647. maxframe_client/tests/test_session.py +255 -0
maxframe/session.py ADDED
@@ -0,0 +1,1310 @@
1
+ # Copyright 1999-2024 Alibaba Group Holding Ltd.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import asyncio
16
+ import concurrent.futures
17
+ import logging
18
+ import random
19
+ import string
20
+ import threading
21
+ import warnings
22
+ from abc import ABC, ABCMeta, abstractmethod
23
+ from concurrent.futures import Future as SyncFuture
24
+ from dataclasses import dataclass
25
+ from functools import wraps
26
+ from typing import Callable, Coroutine, Dict, List, Optional, Tuple, Type, Union
27
+ from urllib.parse import urlparse
28
+ from weakref import ref
29
+
30
+ from odps import ODPS
31
+
32
+ from maxframe.core import TileableType
33
+ from maxframe.lib.aio import Isolation, get_isolation, new_isolation, stop_isolation
34
+ from maxframe.typing_ import ClientType
35
+ from maxframe.utils import classproperty, implements, relay_future
36
+
37
+ from .config import options
38
+
39
+ logger = logging.getLogger(__name__)
40
+
41
+
42
+ @dataclass
43
+ class Progress:
44
+ value: float = 0.0
45
+
46
+
47
+ @dataclass
48
+ class Profiling:
49
+ result: dict = None
50
+
51
+
52
+ class ExecutionInfo:
53
+ def __init__(
54
+ self,
55
+ aio_task: asyncio.Task,
56
+ progress: Progress,
57
+ profiling: Profiling,
58
+ loop: asyncio.AbstractEventLoop,
59
+ to_execute_tileables: List[TileableType],
60
+ ):
61
+ self._aio_task = aio_task
62
+ self._progress = progress
63
+ self._profiling = profiling
64
+ self._loop = loop
65
+ self._to_execute_tileables = [ref(t) for t in to_execute_tileables]
66
+
67
+ self._future_local = threading.local()
68
+
69
+ def _ensure_future(self):
70
+ try:
71
+ self._future_local.future
72
+ except AttributeError:
73
+
74
+ async def wait():
75
+ return await self._aio_task
76
+
77
+ self._future_local.future = fut = asyncio.run_coroutine_threadsafe(
78
+ wait(), self._loop
79
+ )
80
+ self._future_local.aio_future = asyncio.wrap_future(fut)
81
+
82
+ @property
83
+ def loop(self):
84
+ return self._loop
85
+
86
+ @property
87
+ def aio_task(self):
88
+ return self._aio_task
89
+
90
+ def progress(self) -> float:
91
+ return self._progress.value
92
+
93
+ @property
94
+ def to_execute_tileables(self) -> List[TileableType]:
95
+ return [t() for t in self._to_execute_tileables]
96
+
97
+ def profiling_result(self) -> dict:
98
+ return self._profiling.result
99
+
100
+ def result(self, timeout=None):
101
+ self._ensure_future()
102
+ return self._future_local.future.result(timeout=timeout)
103
+
104
+ def cancel(self):
105
+ self._aio_task.cancel()
106
+
107
+ def __getattr__(self, attr):
108
+ self._ensure_future()
109
+ return getattr(self._future_local.aio_future, attr)
110
+
111
+ def __await__(self):
112
+ self._ensure_future()
113
+ return self._future_local.aio_future.__await__()
114
+
115
+ def get_future(self):
116
+ self._ensure_future()
117
+ return self._future_local.aio_future
118
+
119
+
120
+ warning_msg = """
121
+ No session found, local session \
122
+ will be created in background, \
123
+ it may take a while before execution. \
124
+ If you want to new a local session by yourself, \
125
+ run code below:
126
+
127
+ ```
128
+ import maxframe
129
+
130
+ maxframe.new_session()
131
+ ```
132
+ """
133
+
134
+
135
+ class AbstractSession(ABC):
136
+ name = None
137
+ _default = None
138
+ _lock = threading.Lock()
139
+
140
+ def __init__(self, address: str, session_id: str):
141
+ self._address = address
142
+ self._session_id = session_id
143
+ self._closed = False
144
+
145
+ @property
146
+ def address(self):
147
+ return self._address
148
+
149
+ @property
150
+ def session_id(self):
151
+ return self._session_id
152
+
153
+ def __eq__(self, other):
154
+ return (
155
+ isinstance(other, AbstractSession)
156
+ and self._address == other.address
157
+ and self._session_id == other.session_id
158
+ )
159
+
160
+ def __hash__(self):
161
+ return hash((AbstractSession, self._address, self._session_id))
162
+
163
+ def as_default(self) -> "AbstractSession":
164
+ """
165
+ Mark current session as default session.
166
+ """
167
+ AbstractSession._default = self
168
+ return self
169
+
170
+ @classmethod
171
+ def reset_default(cls):
172
+ AbstractSession._default = None
173
+
174
+ @classproperty
175
+ def default(self):
176
+ return AbstractSession._default
177
+
178
+
179
+ class AbstractAsyncSession(AbstractSession, metaclass=ABCMeta):
180
+ @classmethod
181
+ @abstractmethod
182
+ async def init(
183
+ cls, address: str, session_id: str, new: bool = True, **kwargs
184
+ ) -> "AbstractSession":
185
+ """
186
+ Init a new session.
187
+
188
+ Parameters
189
+ ----------
190
+ address : str
191
+ Address.
192
+ session_id : str
193
+ Session ID.
194
+ new : bool
195
+ New a session.
196
+ kwargs
197
+
198
+ Returns
199
+ -------
200
+ session
201
+ """
202
+
203
+ async def destroy(self):
204
+ """
205
+ Destroy a session.
206
+ """
207
+ self.reset_default()
208
+ self._closed = True
209
+
210
+ @abstractmethod
211
+ async def execute(self, *tileables, **kwargs) -> ExecutionInfo:
212
+ """
213
+ Execute tileables.
214
+
215
+ Parameters
216
+ ----------
217
+ tileables
218
+ Tileables.
219
+ kwargs
220
+ """
221
+
222
+ @abstractmethod
223
+ async def fetch(self, *tileables, **kwargs) -> list:
224
+ """
225
+ Fetch tileables' data.
226
+
227
+ Parameters
228
+ ----------
229
+ tileables
230
+ Tileables.
231
+
232
+ Returns
233
+ -------
234
+ data
235
+ """
236
+
237
+ @abstractmethod
238
+ async def decref(self, *tileables_keys):
239
+ """
240
+ Decref tileables.
241
+
242
+ Parameters
243
+ ----------
244
+ tileables_keys : list
245
+ Tileables' keys
246
+ """
247
+
248
+ @abstractmethod
249
+ async def _get_ref_counts(self) -> Dict[str, int]:
250
+ """
251
+ Get all ref counts
252
+
253
+ Returns
254
+ -------
255
+ ref_counts
256
+ """
257
+
258
+ @abstractmethod
259
+ async def fetch_tileable_op_logs(
260
+ self,
261
+ tileable_op_key: str,
262
+ offsets: Union[Dict[str, List[int]], str, int],
263
+ sizes: Union[Dict[str, List[int]], str, int],
264
+ ) -> Dict:
265
+ """
266
+ Fetch logs given tileable op key.
267
+
268
+ Parameters
269
+ ----------
270
+ tileable_op_key : str
271
+ Tileable op key.
272
+ offsets
273
+ Chunk op key to offsets.
274
+ sizes
275
+ Chunk op key to sizes.
276
+
277
+ Returns
278
+ -------
279
+ chunk_key_to_logs
280
+ """
281
+
282
+ @abstractmethod
283
+ async def get_total_n_cpu(self):
284
+ """
285
+ Get number of cluster cpus.
286
+
287
+ Returns
288
+ -------
289
+ number_of_cpu: int
290
+ """
291
+
292
+ @abstractmethod
293
+ async def get_cluster_versions(self) -> List[str]:
294
+ """
295
+ Get versions used in current MaxFrame cluster
296
+
297
+ Returns
298
+ -------
299
+ version_list : list
300
+ List of versions
301
+ """
302
+
303
+ @abstractmethod
304
+ async def get_web_endpoint(self) -> Optional[str]:
305
+ """
306
+ Get web endpoint of current session
307
+
308
+ Returns
309
+ -------
310
+ web_endpoint : str
311
+ web endpoint
312
+ """
313
+
314
+ @abstractmethod
315
+ async def create_remote_object(
316
+ self, session_id: str, name: str, object_cls, *args, **kwargs
317
+ ):
318
+ """
319
+ Create remote object
320
+
321
+ Parameters
322
+ ----------
323
+ session_id : str
324
+ Session ID.
325
+ name : str
326
+ object_cls
327
+ args
328
+ kwargs
329
+
330
+ Returns
331
+ -------
332
+ actor_ref
333
+ """
334
+
335
+ @abstractmethod
336
+ async def get_remote_object(self, session_id: str, name: str):
337
+ """
338
+ Get remote object.
339
+
340
+ Parameters
341
+ ----------
342
+ session_id : str
343
+ Session ID.
344
+ name : str
345
+
346
+ Returns
347
+ -------
348
+ actor_ref
349
+ """
350
+
351
+ @abstractmethod
352
+ async def destroy_remote_object(self, session_id: str, name: str):
353
+ """
354
+ Destroy remote object.
355
+
356
+ Parameters
357
+ ----------
358
+ session_id : str
359
+ Session ID.
360
+ name : str
361
+ """
362
+
363
+ async def stop_server(self):
364
+ """
365
+ Stop server.
366
+ """
367
+
368
+ @abstractmethod
369
+ async def get_logview_address(self, hours=None) -> Optional[str]:
370
+ """
371
+ Get Logview address
372
+ Returns
373
+ -------
374
+ Logview address
375
+ """
376
+
377
+ def close(self):
378
+ asyncio.run(self.destroy())
379
+
380
+ def __enter__(self):
381
+ return self
382
+
383
+ def __exit__(self, *_):
384
+ self.close()
385
+
386
+
387
+ class AbstractSyncSession(AbstractSession, metaclass=ABCMeta):
388
+ @classmethod
389
+ @abstractmethod
390
+ def init(
391
+ cls,
392
+ address: str,
393
+ session_id: str,
394
+ backend: str = "maxframe",
395
+ new: bool = True,
396
+ **kwargs,
397
+ ) -> "AbstractSession":
398
+ """
399
+ Init a new session.
400
+
401
+ Parameters
402
+ ----------
403
+ address : str
404
+ Address.
405
+ session_id : str
406
+ Session ID.
407
+ backend : str
408
+ Backend.
409
+ new : bool
410
+ New a session.
411
+ kwargs
412
+
413
+ Returns
414
+ -------
415
+ session
416
+ """
417
+
418
+ @abstractmethod
419
+ def execute(
420
+ self, tileable, *tileables, show_progress: Union[bool, str] = None, **kwargs
421
+ ) -> Union[List[TileableType], TileableType, ExecutionInfo]:
422
+ """
423
+ Execute tileables.
424
+
425
+ Parameters
426
+ ----------
427
+ tileable
428
+ Tileable.
429
+ tileables
430
+ Tileables.
431
+ show_progress
432
+ If show progress.
433
+ kwargs
434
+
435
+ Returns
436
+ -------
437
+ result
438
+ """
439
+
440
+ @abstractmethod
441
+ def fetch(self, *tileables, **kwargs) -> list:
442
+ """
443
+ Fetch tileables.
444
+
445
+ Parameters
446
+ ----------
447
+ tileables
448
+ Tileables.
449
+ kwargs
450
+
451
+ Returns
452
+ -------
453
+ fetched_data : list
454
+ """
455
+
456
+ @abstractmethod
457
+ def fetch_infos(self, *tileables, fields, **kwargs) -> list:
458
+ """
459
+ Fetch infos of tileables.
460
+
461
+ Parameters
462
+ ----------
463
+ tileables
464
+ Tileables.
465
+ fields
466
+ List of fields
467
+ kwargs
468
+
469
+ Returns
470
+ -------
471
+ fetched_infos : list
472
+ """
473
+
474
+ @abstractmethod
475
+ def decref(self, *tileables_keys):
476
+ """
477
+ Decref tileables.
478
+
479
+ Parameters
480
+ ----------
481
+ tileables_keys : list
482
+ Tileables' keys
483
+ """
484
+
485
+ @abstractmethod
486
+ def _get_ref_counts(self) -> Dict[str, int]:
487
+ """
488
+ Get all ref counts
489
+
490
+ Returns
491
+ -------
492
+ ref_counts
493
+ """
494
+
495
+ @abstractmethod
496
+ def fetch_tileable_op_logs(
497
+ self,
498
+ tileable_op_key: str,
499
+ offsets: Union[Dict[str, List[int]], str, int],
500
+ sizes: Union[Dict[str, List[int]], str, int],
501
+ ) -> Dict:
502
+ """
503
+ Fetch logs given tileable op key.
504
+
505
+ Parameters
506
+ ----------
507
+ tileable_op_key : str
508
+ Tileable op key.
509
+ offsets
510
+ Chunk op key to offsets.
511
+ sizes
512
+ Chunk op key to sizes.
513
+
514
+ Returns
515
+ -------
516
+ chunk_key_to_logs
517
+ """
518
+
519
+ @abstractmethod
520
+ def get_total_n_cpu(self):
521
+ """
522
+ Get number of cluster cpus.
523
+
524
+ Returns
525
+ -------
526
+ number_of_cpu: int
527
+ """
528
+
529
+ @abstractmethod
530
+ def get_cluster_versions(self) -> List[str]:
531
+ """
532
+ Get versions used in current MaxFrame cluster
533
+
534
+ Returns
535
+ -------
536
+ version_list : list
537
+ List of versions
538
+ """
539
+
540
+ @abstractmethod
541
+ def get_web_endpoint(self) -> Optional[str]:
542
+ """
543
+ Get web endpoint of current session
544
+
545
+ Returns
546
+ -------
547
+ web_endpoint : str
548
+ web endpoint
549
+ """
550
+
551
+ def fetch_log(
552
+ self,
553
+ tileables: List[TileableType],
554
+ offsets: List[int] = None,
555
+ sizes: List[int] = None,
556
+ ):
557
+ from .core.custom_log import fetch
558
+
559
+ return fetch(tileables, self, offsets=offsets, sizes=sizes)
560
+
561
+ @abstractmethod
562
+ def get_logview_address(self, hours=None) -> Optional[str]:
563
+ """
564
+ Get logview address
565
+ Returns
566
+ -------
567
+ logview address
568
+ """
569
+
570
+
571
+ def _delegate_to_isolated_session(func: Union[Callable, Coroutine]):
572
+ if asyncio.iscoroutinefunction(func):
573
+
574
+ @wraps(func)
575
+ async def inner(session: "AsyncSession", *args, **kwargs):
576
+ coro = getattr(session._isolated_session, func.__name__)(*args, **kwargs)
577
+ fut = asyncio.run_coroutine_threadsafe(coro, session._loop)
578
+ return await asyncio.wrap_future(fut)
579
+
580
+ else:
581
+
582
+ @wraps(func)
583
+ def inner(session: "SyncSession", *args, **kwargs):
584
+ coro = getattr(session._isolated_session, func.__name__)(*args, **kwargs)
585
+ fut = asyncio.run_coroutine_threadsafe(coro, session._loop)
586
+ return fut.result()
587
+
588
+ return inner
589
+
590
+
591
+ _schemes_to_isolated_session_cls: Dict[
592
+ Optional[str], Type["IsolatedAsyncSession"]
593
+ ] = dict()
594
+
595
+
596
+ class IsolatedAsyncSession(AbstractAsyncSession):
597
+ """
598
+ Abstract class of isolated session which can be registered
599
+ to different schemes
600
+ """
601
+
602
+ schemes = None
603
+
604
+ @classmethod
605
+ def register_schemes(cls, overwrite: bool = True):
606
+ assert isinstance(cls.schemes, list)
607
+ for scheme in cls.schemes:
608
+ if overwrite or scheme not in _schemes_to_isolated_session_cls:
609
+ _schemes_to_isolated_session_cls[scheme] = cls
610
+
611
+
612
+ def _get_isolated_session_cls(address: str) -> Type[IsolatedAsyncSession]:
613
+ if ":/" not in address:
614
+ url_scheme = None
615
+ else:
616
+ url_scheme = urlparse(address).scheme or None
617
+ scheme_cls = _schemes_to_isolated_session_cls.get(url_scheme)
618
+ if scheme_cls is None:
619
+ raise ValueError(f"Address scheme {url_scheme} not supported")
620
+ return scheme_cls
621
+
622
+
623
+ class AsyncSession(AbstractAsyncSession):
624
+ def __init__(
625
+ self,
626
+ address: str,
627
+ session_id: str,
628
+ isolated_session: IsolatedAsyncSession,
629
+ isolation: Isolation,
630
+ ):
631
+ super().__init__(address, session_id)
632
+
633
+ self._isolated_session = _get_isolated_session(isolated_session)
634
+ self._isolation = isolation
635
+ self._loop = isolation.loop
636
+
637
+ @classmethod
638
+ def from_isolated_session(
639
+ cls, isolated_session: IsolatedAsyncSession
640
+ ) -> "AsyncSession":
641
+ return cls(
642
+ isolated_session.address,
643
+ isolated_session.session_id,
644
+ isolated_session,
645
+ get_isolation(),
646
+ )
647
+
648
+ @property
649
+ def client(self):
650
+ return self._isolated_session.client
651
+
652
+ @client.setter
653
+ def client(self, client: ClientType):
654
+ self._isolated_session.client = client
655
+
656
+ @classmethod
657
+ @implements(AbstractAsyncSession.init)
658
+ async def init(
659
+ cls,
660
+ address: str,
661
+ session_id: str,
662
+ backend: str = "maxframe",
663
+ new: bool = True,
664
+ **kwargs,
665
+ ) -> "AbstractSession":
666
+ isolation = ensure_isolation_created(kwargs)
667
+ coro = _get_isolated_session_cls(address).init(
668
+ address, session_id, backend, new=new, **kwargs
669
+ )
670
+ fut = asyncio.run_coroutine_threadsafe(coro, isolation.loop)
671
+ isolated_session = await asyncio.wrap_future(fut)
672
+ return AsyncSession(address, session_id, isolated_session, isolation)
673
+
674
+ def as_default(self) -> AbstractSession:
675
+ AbstractSession._default = self._isolated_session
676
+ return self
677
+
678
+ @implements(AbstractAsyncSession.destroy)
679
+ async def destroy(self):
680
+ coro = self._isolated_session.destroy()
681
+ await asyncio.wrap_future(asyncio.run_coroutine_threadsafe(coro, self._loop))
682
+ self.reset_default()
683
+
684
+ @implements(AbstractAsyncSession.execute)
685
+ @_delegate_to_isolated_session
686
+ async def execute(self, *tileables, **kwargs) -> ExecutionInfo:
687
+ pass # pragma: no cover
688
+
689
+ @implements(AbstractAsyncSession.fetch)
690
+ async def fetch(self, *tileables, **kwargs) -> list:
691
+ coro = _fetch(*tileables, session=self._isolated_session, **kwargs)
692
+ return await asyncio.wrap_future(
693
+ asyncio.run_coroutine_threadsafe(coro, self._loop)
694
+ )
695
+
696
+ @implements(AbstractAsyncSession._get_ref_counts)
697
+ @_delegate_to_isolated_session
698
+ async def _get_ref_counts(self) -> Dict[str, int]:
699
+ pass # pragma: no cover
700
+
701
+ @implements(AbstractAsyncSession.fetch_tileable_op_logs)
702
+ @_delegate_to_isolated_session
703
+ async def fetch_tileable_op_logs(
704
+ self,
705
+ tileable_op_key: str,
706
+ offsets: Union[Dict[str, List[int]], str, int],
707
+ sizes: Union[Dict[str, List[int]], str, int],
708
+ ) -> Dict:
709
+ pass # pragma: no cover
710
+
711
+ @implements(AbstractAsyncSession.get_total_n_cpu)
712
+ @_delegate_to_isolated_session
713
+ async def get_total_n_cpu(self):
714
+ pass # pragma: no cover
715
+
716
+ @implements(AbstractAsyncSession.get_cluster_versions)
717
+ @_delegate_to_isolated_session
718
+ async def get_cluster_versions(self) -> List[str]:
719
+ pass # pragma: no cover
720
+
721
+ @implements(AbstractAsyncSession.create_remote_object)
722
+ @_delegate_to_isolated_session
723
+ async def create_remote_object(
724
+ self, session_id: str, name: str, object_cls, *args, **kwargs
725
+ ):
726
+ pass # pragma: no cover
727
+
728
+ @implements(AbstractAsyncSession.get_remote_object)
729
+ @_delegate_to_isolated_session
730
+ async def get_remote_object(self, session_id: str, name: str):
731
+ pass # pragma: no cover
732
+
733
+ @implements(AbstractAsyncSession.destroy_remote_object)
734
+ @_delegate_to_isolated_session
735
+ async def destroy_remote_object(self, session_id: str, name: str):
736
+ pass # pragma: no cover
737
+
738
+ @implements(AbstractAsyncSession.get_web_endpoint)
739
+ @_delegate_to_isolated_session
740
+ async def get_web_endpoint(self) -> Optional[str]:
741
+ pass # pragma: no cover
742
+
743
+ @implements(AbstractAsyncSession.stop_server)
744
+ async def stop_server(self):
745
+ coro = self._isolated_session.stop_server()
746
+ await asyncio.wrap_future(asyncio.run_coroutine_threadsafe(coro, self._loop))
747
+ stop_isolation()
748
+
749
+ @implements(AbstractAsyncSession.get_logview_address)
750
+ @_delegate_to_isolated_session
751
+ async def get_logview_address(self, hours=None) -> Optional[str]:
752
+ pass # pragma: no cover
753
+
754
+
755
+ class ProgressBar:
756
+ def __init__(self, show_progress):
757
+ if not show_progress:
758
+ self.progress_bar = None
759
+ else:
760
+ try:
761
+ from tqdm.auto import tqdm
762
+ except ImportError:
763
+ if show_progress != "auto": # pragma: no cover
764
+ raise ImportError("tqdm is required to show progress")
765
+ else:
766
+ self.progress_bar = None
767
+ else:
768
+ self.progress_bar = tqdm(
769
+ total=100,
770
+ bar_format="{l_bar}{bar}| {n:6.2f}/{total_fmt} "
771
+ "[{elapsed}<{remaining}, {rate_fmt}{postfix}]",
772
+ )
773
+
774
+ self.last_progress: float = 0.0
775
+
776
+ @property
777
+ def show_progress(self) -> bool:
778
+ return self.progress_bar is not None
779
+
780
+ def __enter__(self):
781
+ self.progress_bar.__enter__()
782
+
783
+ def __exit__(self, *_):
784
+ self.progress_bar.__exit__(*_)
785
+
786
+ def update(self, progress: float):
787
+ progress = min(progress, 100)
788
+ last_progress = self.last_progress
789
+ if self.progress_bar:
790
+ incr = max(progress - last_progress, 0)
791
+ self.progress_bar.update(incr)
792
+ self.last_progress = max(last_progress, progress)
793
+
794
+
795
+ class SyncSession(AbstractSyncSession):
796
+ _execution_pool = concurrent.futures.ThreadPoolExecutor(1)
797
+
798
+ def __init__(
799
+ self,
800
+ address: str,
801
+ session_id: str,
802
+ isolated_session: IsolatedAsyncSession,
803
+ isolation: Isolation,
804
+ ):
805
+ super().__init__(address, session_id)
806
+
807
+ self._isolated_session = _get_isolated_session(isolated_session)
808
+ self._isolation = isolation
809
+ self._loop = isolation.loop
810
+
811
+ @classmethod
812
+ def from_isolated_session(
813
+ cls, isolated_session: IsolatedAsyncSession
814
+ ) -> "SyncSession":
815
+ return cls(
816
+ isolated_session.address,
817
+ isolated_session.session_id,
818
+ isolated_session,
819
+ get_isolation(),
820
+ )
821
+
822
+ @classmethod
823
+ def init(
824
+ cls,
825
+ address: str,
826
+ session_id: str,
827
+ backend: str = "maxframe",
828
+ new: bool = True,
829
+ **kwargs,
830
+ ) -> "AbstractSession":
831
+ isolation = ensure_isolation_created(kwargs)
832
+ coro = _get_isolated_session_cls(address).init(
833
+ address, session_id, backend, new=new, **kwargs
834
+ )
835
+ fut = asyncio.run_coroutine_threadsafe(coro, isolation.loop)
836
+ isolated_session = fut.result()
837
+ return SyncSession(address, session_id, isolated_session, isolation)
838
+
839
+ def as_default(self) -> AbstractSession:
840
+ AbstractSession._default = self._isolated_session
841
+ return self
842
+
843
+ @property
844
+ def _session(self):
845
+ return self._isolated_session
846
+
847
+ @property
848
+ def session_id(self):
849
+ try:
850
+ return self._session.session_id or self._session_id
851
+ except AttributeError:
852
+ return self._session_id
853
+
854
+ def _new_cancel_event(self):
855
+ async def new_event():
856
+ return asyncio.Event()
857
+
858
+ return asyncio.run_coroutine_threadsafe(new_event(), self._loop).result()
859
+
860
+ @implements(AbstractSyncSession.execute)
861
+ def execute(
862
+ self,
863
+ tileable,
864
+ *tileables,
865
+ show_progress: Union[bool, str] = None,
866
+ warn_duplicated_execution: bool = None,
867
+ **kwargs,
868
+ ) -> Union[List[TileableType], TileableType, ExecutionInfo]:
869
+ wait = kwargs.get("wait", True)
870
+ # add an intermediate future for cancel tests
871
+ result_future = kwargs.pop("result_future", None) or SyncFuture()
872
+
873
+ if show_progress is None:
874
+ show_progress = options.show_progress
875
+ if warn_duplicated_execution is None:
876
+ warn_duplicated_execution = options.warn_duplicated_execution
877
+ to_execute_tileables = []
878
+ for t in (tileable,) + tileables:
879
+ to_execute_tileables.extend(t.op.outputs)
880
+
881
+ cancelled = kwargs.get("cancelled")
882
+ if cancelled is None:
883
+ cancelled = kwargs["cancelled"] = self._new_cancel_event()
884
+
885
+ coro = _execute(
886
+ *set(to_execute_tileables),
887
+ session=self._isolated_session,
888
+ show_progress=show_progress,
889
+ warn_duplicated_execution=warn_duplicated_execution,
890
+ **kwargs,
891
+ )
892
+ fut = asyncio.run_coroutine_threadsafe(coro, self._loop)
893
+ relay_future(result_future, fut)
894
+ try:
895
+ execution_info: ExecutionInfo = result_future.result(
896
+ timeout=self._isolated_session.timeout
897
+ )
898
+ except KeyboardInterrupt: # pragma: no cover
899
+ logger.warning("Cancelling running task")
900
+ cancelled.set()
901
+ fut.result()
902
+ logger.warning("Cancel finished")
903
+
904
+ if wait:
905
+ return tileable if len(tileables) == 0 else [tileable] + list(tileables)
906
+ else:
907
+ aio_task = execution_info.aio_task
908
+
909
+ async def run():
910
+ await aio_task
911
+ return tileable if len(tileables) == 0 else [tileable] + list(tileables)
912
+
913
+ async def driver():
914
+ return asyncio.create_task(run())
915
+
916
+ new_aio_task = asyncio.run_coroutine_threadsafe(
917
+ driver(), execution_info.loop
918
+ ).result()
919
+ new_execution_info = ExecutionInfo(
920
+ new_aio_task,
921
+ execution_info._progress,
922
+ execution_info._profiling,
923
+ execution_info.loop,
924
+ to_execute_tileables,
925
+ )
926
+ return new_execution_info
927
+
928
+ @implements(AbstractSyncSession.fetch)
929
+ def fetch(self, *tileables, **kwargs) -> list:
930
+ coro = _fetch(*tileables, session=self._isolated_session, **kwargs)
931
+ return asyncio.run_coroutine_threadsafe(coro, self._loop).result()
932
+
933
+ @implements(AbstractSyncSession.fetch_infos)
934
+ def fetch_infos(self, *tileables, fields, **kwargs) -> list:
935
+ coro = _fetch_infos(
936
+ *tileables, fields=fields, session=self._isolated_session, **kwargs
937
+ )
938
+ return asyncio.run_coroutine_threadsafe(coro, self._loop).result()
939
+
940
+ @implements(AbstractSyncSession.decref)
941
+ def decref(self, *tileable_keys):
942
+ coro = _decref(*tileable_keys, session=self._isolated_session)
943
+ return asyncio.run_coroutine_threadsafe(coro, self._loop).result()
944
+
945
+ @implements(AbstractSyncSession._get_ref_counts)
946
+ @_delegate_to_isolated_session
947
+ def _get_ref_counts(self) -> Dict[str, int]:
948
+ pass # pragma: no cover
949
+
950
+ @implements(AbstractSyncSession.fetch_tileable_op_logs)
951
+ @_delegate_to_isolated_session
952
+ def fetch_tileable_op_logs(
953
+ self,
954
+ tileable_op_key: str,
955
+ offsets: Union[Dict[str, List[int]], str, int],
956
+ sizes: Union[Dict[str, List[int]], str, int],
957
+ ) -> Dict:
958
+ pass # pragma: no cover
959
+
960
+ @implements(AbstractSyncSession.get_total_n_cpu)
961
+ @_delegate_to_isolated_session
962
+ def get_total_n_cpu(self):
963
+ pass # pragma: no cover
964
+
965
+ @implements(AbstractSyncSession.get_web_endpoint)
966
+ @_delegate_to_isolated_session
967
+ def get_web_endpoint(self) -> Optional[str]:
968
+ pass # pragma: no cover
969
+
970
+ @implements(AbstractSyncSession.get_cluster_versions)
971
+ @_delegate_to_isolated_session
972
+ def get_cluster_versions(self) -> List[str]:
973
+ pass # pragma: no cover
974
+
975
+ @implements(AbstractSyncSession.get_logview_address)
976
+ @_delegate_to_isolated_session
977
+ def get_logview_address(self, hours=None) -> Optional[str]:
978
+ pass # pragma: no cover
979
+
980
+ def destroy(self):
981
+ coro = self._isolated_session.destroy()
982
+ asyncio.run_coroutine_threadsafe(coro, self._loop).result()
983
+ self.reset_default()
984
+
985
+ def stop_server(self, isolation=True):
986
+ try:
987
+ coro = self._isolated_session.stop_server()
988
+ future = asyncio.run_coroutine_threadsafe(coro, self._loop)
989
+ future.result(timeout=5)
990
+ finally:
991
+ self.reset_default()
992
+ if isolation:
993
+ stop_isolation()
994
+
995
+ def close(self):
996
+ self.destroy()
997
+
998
+ def __enter__(self):
999
+ return self
1000
+
1001
+ def __exit__(self, *_):
1002
+ self.close()
1003
+
1004
+
1005
+ async def _execute_with_progress(
1006
+ execution_info: ExecutionInfo,
1007
+ progress_bar: ProgressBar,
1008
+ progress_update_interval: Union[int, float],
1009
+ cancelled: asyncio.Event,
1010
+ ):
1011
+ with progress_bar:
1012
+ while not cancelled.is_set():
1013
+ done, _pending = await asyncio.wait(
1014
+ [execution_info.get_future()], timeout=progress_update_interval
1015
+ )
1016
+ if not done:
1017
+ if not cancelled.is_set() and execution_info.progress() is not None:
1018
+ progress_bar.update(execution_info.progress() * 100)
1019
+ else:
1020
+ # done
1021
+ if not cancelled.is_set():
1022
+ progress_bar.update(100)
1023
+ break
1024
+
1025
+
1026
+ async def _execute(
1027
+ *tileables: Tuple[TileableType, ...],
1028
+ session: IsolatedAsyncSession = None,
1029
+ wait: bool = True,
1030
+ show_progress: Union[bool, str] = "auto",
1031
+ progress_update_interval: Union[int, float] = 1,
1032
+ cancelled: asyncio.Event = None,
1033
+ **kwargs,
1034
+ ):
1035
+ execution_info = await session.execute(*tileables, **kwargs)
1036
+
1037
+ def _attach_session(future: asyncio.Future):
1038
+ if future.exception() is None:
1039
+ for t in execution_info.to_execute_tileables:
1040
+ t._attach_session(session)
1041
+
1042
+ execution_info.add_done_callback(_attach_session)
1043
+ cancelled = cancelled or asyncio.Event()
1044
+
1045
+ if wait:
1046
+ progress_bar = ProgressBar(show_progress)
1047
+ if progress_bar.show_progress:
1048
+ await _execute_with_progress(
1049
+ execution_info, progress_bar, progress_update_interval, cancelled
1050
+ )
1051
+ else:
1052
+ exec_task = asyncio.ensure_future(execution_info)
1053
+ cancel_task = asyncio.ensure_future(cancelled.wait())
1054
+ await asyncio.wait(
1055
+ [exec_task, cancel_task], return_when=asyncio.FIRST_COMPLETED
1056
+ )
1057
+ if cancelled.is_set():
1058
+ execution_info.remove_done_callback(_attach_session)
1059
+ execution_info.cancel()
1060
+ else:
1061
+ # set cancelled to avoid wait task leak
1062
+ cancelled.set()
1063
+ await execution_info
1064
+ else:
1065
+ return execution_info
1066
+
1067
+
1068
+ def execute(
1069
+ tileable: TileableType,
1070
+ *tileables: Tuple[TileableType, ...],
1071
+ session: SyncSession = None,
1072
+ wait: bool = True,
1073
+ new_session_kwargs: dict = None,
1074
+ show_progress: Union[bool, str] = None,
1075
+ progress_update_interval=1,
1076
+ **kwargs,
1077
+ ):
1078
+ if isinstance(tileable, (tuple, list)) and len(tileables) == 0:
1079
+ tileable, tileables = tileable[0], tileable[1:]
1080
+ if session is None:
1081
+ session = get_default_or_create(**(new_session_kwargs or dict()))
1082
+ session = _ensure_sync(session)
1083
+ return session.execute(
1084
+ tileable,
1085
+ *tileables,
1086
+ wait=wait,
1087
+ show_progress=show_progress,
1088
+ progress_update_interval=progress_update_interval,
1089
+ **kwargs,
1090
+ )
1091
+
1092
+
1093
+ async def _fetch(
1094
+ tileable: TileableType,
1095
+ *tileables: Tuple[TileableType, ...],
1096
+ session: IsolatedAsyncSession = None,
1097
+ **kwargs,
1098
+ ):
1099
+ if isinstance(tileable, tuple) and len(tileables) == 0:
1100
+ tileable, tileables = tileable[0], tileable[1:]
1101
+ session = _get_isolated_session(session)
1102
+ data = await session.fetch(tileable, *tileables, **kwargs)
1103
+ return data[0] if len(tileables) == 0 else data
1104
+
1105
+
1106
+ async def _fetch_infos(
1107
+ tileable: TileableType,
1108
+ *tileables: Tuple[TileableType, ...],
1109
+ session: IsolatedAsyncSession = None,
1110
+ fields: List[str] = None,
1111
+ **kwargs,
1112
+ ):
1113
+ if isinstance(tileable, tuple) and len(tileables) == 0:
1114
+ tileable, tileables = tileable[0], tileable[1:]
1115
+ session = _get_isolated_session(session)
1116
+ data = await session.fetch_infos(tileable, *tileables, fields=fields, **kwargs)
1117
+ return data[0] if len(tileables) == 0 else data
1118
+
1119
+
1120
+ async def _decref(
1121
+ tileable_key: str,
1122
+ *tileable_keys: Tuple[str, ...],
1123
+ session: IsolatedAsyncSession = None,
1124
+ ):
1125
+ if isinstance(tileable_key, tuple) and len(tileable_keys) == 0:
1126
+ tileable_key, tileable_keys = tileable_key[0], tileable_key[1:]
1127
+ session = _get_isolated_session(session)
1128
+ await session.decref(tileable_key, *tileable_keys)
1129
+
1130
+
1131
+ def fetch(
1132
+ tileable: TileableType,
1133
+ *tileables: Tuple[TileableType],
1134
+ session: SyncSession = None,
1135
+ **kwargs,
1136
+ ):
1137
+ if isinstance(tileable, (tuple, list)) and len(tileables) == 0:
1138
+ tileable, tileables = tileable[0], tileable[1:]
1139
+ if session is None:
1140
+ session = get_default_session()
1141
+ if session is None: # pragma: no cover
1142
+ raise ValueError("No session found")
1143
+
1144
+ session = _ensure_sync(session)
1145
+ return session.fetch(tileable, *tileables, **kwargs)
1146
+
1147
+
1148
+ def fetch_infos(
1149
+ tileable: TileableType,
1150
+ *tileables: Tuple[TileableType],
1151
+ fields: List[str],
1152
+ session: SyncSession = None,
1153
+ **kwargs,
1154
+ ):
1155
+ if isinstance(tileable, tuple) and len(tileables) == 0:
1156
+ tileable, tileables = tileable[0], tileable[1:]
1157
+ if session is None:
1158
+ session = get_default_session()
1159
+ if session is None: # pragma: no cover
1160
+ raise ValueError("No session found")
1161
+ session = _ensure_sync(session)
1162
+ return session.fetch_infos(tileable, *tileables, fields=fields, **kwargs)
1163
+
1164
+
1165
+ def fetch_log(*tileables: TileableType, session: SyncSession = None, **kwargs):
1166
+ if len(tileables) == 1 and isinstance(tileables[0], (list, tuple)):
1167
+ tileables = tileables[0]
1168
+ if session is None:
1169
+ session = get_default_session()
1170
+ if session is None: # pragma: no cover
1171
+ raise ValueError("No session found")
1172
+ session = _ensure_sync(session)
1173
+ return session.fetch_log(list(tileables), **kwargs)
1174
+
1175
+
1176
+ def ensure_isolation_created(kwargs):
1177
+ loop = kwargs.pop("loop", None)
1178
+ use_uvloop = kwargs.pop("use_uvloop", "auto")
1179
+
1180
+ try:
1181
+ return get_isolation()
1182
+ except KeyError:
1183
+ if loop is None:
1184
+ if not use_uvloop:
1185
+ loop = asyncio.new_event_loop()
1186
+ else:
1187
+ try:
1188
+ import uvloop
1189
+
1190
+ loop = uvloop.new_event_loop()
1191
+ except ImportError:
1192
+ if use_uvloop == "auto":
1193
+ loop = asyncio.new_event_loop()
1194
+ else: # pragma: no cover
1195
+ raise
1196
+ return new_isolation(loop=loop)
1197
+
1198
+
1199
+ def _new_session_id():
1200
+ return "".join(
1201
+ random.choice(string.ascii_letters + string.digits) for _ in range(24)
1202
+ )
1203
+
1204
+
1205
+ async def _new_session(
1206
+ address: str,
1207
+ session_id: str = None,
1208
+ backend: str = "maxframe",
1209
+ default: bool = False,
1210
+ **kwargs,
1211
+ ) -> AbstractSession:
1212
+ if session_id is None:
1213
+ session_id = _new_session_id()
1214
+
1215
+ session = await AsyncSession.init(
1216
+ address, session_id=session_id, backend=backend, new=True, **kwargs
1217
+ )
1218
+ if default:
1219
+ session.as_default()
1220
+ return session
1221
+
1222
+
1223
+ def new_session(
1224
+ address: Union[str, ODPS] = None,
1225
+ session_id: str = None,
1226
+ backend: str = "maxframe",
1227
+ default: bool = True,
1228
+ new: bool = True,
1229
+ odps_entry: Optional[ODPS] = None,
1230
+ **kwargs,
1231
+ ) -> AbstractSession:
1232
+ from maxframe_client.session import register_session_schemes
1233
+
1234
+ register_session_schemes()
1235
+
1236
+ if isinstance(address, ODPS):
1237
+ address, odps_entry = None, address
1238
+
1239
+ # load third party extensions.
1240
+ ensure_isolation_created(kwargs)
1241
+
1242
+ odps_entry = odps_entry or ODPS.from_global() or ODPS.from_environments()
1243
+ if address is None:
1244
+ from maxframe_client.session.consts import ODPS_SESSION_INSECURE_SCHEME
1245
+
1246
+ address = f"{ODPS_SESSION_INSECURE_SCHEME}://"
1247
+
1248
+ if session_id is None:
1249
+ session_id = _new_session_id()
1250
+
1251
+ session = SyncSession.init(
1252
+ address,
1253
+ session_id=session_id,
1254
+ backend=backend,
1255
+ new=new,
1256
+ odps_entry=odps_entry,
1257
+ **kwargs,
1258
+ )
1259
+ if default:
1260
+ session.as_default()
1261
+ return session
1262
+
1263
+
1264
+ def get_default_session() -> Optional[SyncSession]:
1265
+ if AbstractSession.default is None:
1266
+ return
1267
+ return SyncSession.from_isolated_session(AbstractSession.default)
1268
+
1269
+
1270
+ def clear_default_session():
1271
+ AbstractSession.reset_default()
1272
+
1273
+
1274
+ def get_default_async_session() -> Optional[AsyncSession]:
1275
+ if AbstractSession.default is None:
1276
+ return
1277
+ return AsyncSession.from_isolated_session(AbstractSession.default)
1278
+
1279
+
1280
+ def get_default_or_create(**kwargs):
1281
+ with AbstractSession._lock:
1282
+ session = AbstractSession.default
1283
+ if session is None:
1284
+ # no session attached, try to create one
1285
+ warnings.warn(warning_msg)
1286
+ session = new_session(
1287
+ ODPS.from_global() or ODPS.from_environments(), **kwargs
1288
+ )
1289
+ session.as_default()
1290
+ if isinstance(session, IsolatedAsyncSession):
1291
+ session = SyncSession.from_isolated_session(session)
1292
+ return _ensure_sync(session)
1293
+
1294
+
1295
+ def stop_server():
1296
+ if AbstractSession.default:
1297
+ SyncSession.from_isolated_session(AbstractSession.default).stop_server()
1298
+
1299
+
1300
+ def _get_isolated_session(session: AbstractSession) -> IsolatedAsyncSession:
1301
+ if hasattr(session, "_isolated_session"):
1302
+ return session._isolated_session
1303
+ return session
1304
+
1305
+
1306
+ def _ensure_sync(session: AbstractSession) -> SyncSession:
1307
+ if isinstance(session, SyncSession):
1308
+ return session
1309
+ isolated_session = _get_isolated_session(session)
1310
+ return SyncSession.from_isolated_session(isolated_session)