perspective-python 4.5.2__cp311-abi3-pyemscripten_2025_0_wasm32.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.
Files changed (79) hide show
  1. perspective/__init__.py +403 -0
  2. perspective/extension/finos-perspective-nbextension.json +5 -0
  3. perspective/handlers/__init__.py +11 -0
  4. perspective/handlers/aiohttp.py +69 -0
  5. perspective/handlers/starlette.py +63 -0
  6. perspective/handlers/tornado.py +192 -0
  7. perspective/perspective.abi3.so +0 -0
  8. perspective/templates/exported_widget.html.template +35 -0
  9. perspective/tests/__init__.py +11 -0
  10. perspective/tests/async/test_async_client.py +83 -0
  11. perspective/tests/async/test_websocket_client.py +124 -0
  12. perspective/tests/conftest.py +263 -0
  13. perspective/tests/core/__init__.py +11 -0
  14. perspective/tests/core/test_async.py +351 -0
  15. perspective/tests/multi_threaded/__init__.py +11 -0
  16. perspective/tests/multi_threaded/test_multi_threaded.py +201 -0
  17. perspective/tests/server/__init__.py +11 -0
  18. perspective/tests/server/test_server.py +1016 -0
  19. perspective/tests/server/test_session.py +110 -0
  20. perspective/tests/table/__init__.py +11 -0
  21. perspective/tests/table/arrow/date32.arrow +0 -0
  22. perspective/tests/table/arrow/date64.arrow +0 -0
  23. perspective/tests/table/arrow/dict.arrow +0 -0
  24. perspective/tests/table/arrow/dict_update.arrow +0 -0
  25. perspective/tests/table/arrow/int_float_str.arrow +0 -0
  26. perspective/tests/table/arrow/int_float_str_file.arrow +0 -0
  27. perspective/tests/table/arrow/int_float_str_update.arrow +0 -0
  28. perspective/tests/table/object_sequence.py +402 -0
  29. perspective/tests/table/test_column_paths.py +89 -0
  30. perspective/tests/table/test_delete.py +124 -0
  31. perspective/tests/table/test_exception.py +65 -0
  32. perspective/tests/table/test_join.py +115 -0
  33. perspective/tests/table/test_leaks.py +105 -0
  34. perspective/tests/table/test_ports.py +178 -0
  35. perspective/tests/table/test_remove.py +102 -0
  36. perspective/tests/table/test_table.py +641 -0
  37. perspective/tests/table/test_table_arrow.py +500 -0
  38. perspective/tests/table/test_table_datetime.py +2409 -0
  39. perspective/tests/table/test_table_infer.py +201 -0
  40. perspective/tests/table/test_table_limit.py +45 -0
  41. perspective/tests/table/test_table_numpy.py +1022 -0
  42. perspective/tests/table/test_table_page_to_disk.py +304 -0
  43. perspective/tests/table/test_table_pandas.py +1018 -0
  44. perspective/tests/table/test_table_polars.py +251 -0
  45. perspective/tests/table/test_table_view_table.py +130 -0
  46. perspective/tests/table/test_to_arrow.py +417 -0
  47. perspective/tests/table/test_to_arrow_lz4.py +32 -0
  48. perspective/tests/table/test_to_format.py +1024 -0
  49. perspective/tests/table/test_to_polars.py +26 -0
  50. perspective/tests/table/test_update.py +536 -0
  51. perspective/tests/table/test_update_arrow.py +980 -0
  52. perspective/tests/table/test_update_pandas.py +211 -0
  53. perspective/tests/table/test_view.py +2444 -0
  54. perspective/tests/table/test_view_expression.py +1940 -0
  55. perspective/tests/test_dependencies.py +53 -0
  56. perspective/tests/viewer/__init__.py +11 -0
  57. perspective/tests/viewer/test_viewer.py +246 -0
  58. perspective/tests/virtual_servers/__init__.py +12 -0
  59. perspective/tests/virtual_servers/test_coerce_types.py +209 -0
  60. perspective/tests/virtual_servers/test_duckdb.py +908 -0
  61. perspective/tests/virtual_servers/test_polars.py +1032 -0
  62. perspective/tests/widget/__init__.py +11 -0
  63. perspective/tests/widget/test_widget.py +278 -0
  64. perspective/tests/widget/test_widget_pandas.py +453 -0
  65. perspective/virtual_servers/__init__.py +142 -0
  66. perspective/virtual_servers/clickhouse.py +240 -0
  67. perspective/virtual_servers/duckdb.py +236 -0
  68. perspective/virtual_servers/polars.py +710 -0
  69. perspective/widget/__init__.py +349 -0
  70. perspective/widget/viewer/__init__.py +15 -0
  71. perspective/widget/viewer/validate.py +22 -0
  72. perspective/widget/viewer/viewer.py +343 -0
  73. perspective/widget/viewer/viewer_traitlets.py +101 -0
  74. perspective_python-4.5.2.dist-info/METADATA +29 -0
  75. perspective_python-4.5.2.dist-info/RECORD +79 -0
  76. perspective_python-4.5.2.dist-info/WHEEL +4 -0
  77. perspective_python-4.5.2.dist-info/licenses/LICENSE.md +193 -0
  78. perspective_python-4.5.2.dist-info/licenses/LICENSE_THIRDPARTY_cargo.yml +19255 -0
  79. perspective_python-4.5.2.dist-info/sboms/perspective-python.cyclonedx.json +5546 -0
@@ -0,0 +1,304 @@
1
+ # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2
+ # ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3
+ # ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4
+ # ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5
+ # ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6
+ # ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7
+ # ┃ Copyright (c) 2017, the Perspective Authors. ┃
8
+ # ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9
+ # ┃ This file is part of the Perspective library, distributed under the terms ┃
10
+ # ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11
+ # ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
+
13
+ import os
14
+ import tempfile
15
+
16
+ import perspective as psp
17
+ from pytest import mark
18
+
19
+ client = psp.Server().new_local_client()
20
+ Table = client.table
21
+
22
+
23
+ def _perspective_temp_dirs():
24
+ """All `perspective_*` directories currently in the OS temp directory.
25
+
26
+ `BACKING_STORE_DISK` columns are written to a unique
27
+ `<tempdir>/perspective_<uuid>/` directory.
28
+ """
29
+ tmp = tempfile.gettempdir()
30
+ try:
31
+ entries = os.listdir(tmp)
32
+ except OSError:
33
+ return set()
34
+
35
+ print(tmp)
36
+ return {
37
+ os.path.join(tmp, e)
38
+ for e in entries
39
+ if e.startswith("perspective_") and os.path.isdir(os.path.join(tmp, e))
40
+ }
41
+
42
+
43
+ class TestTableOnDisk:
44
+ def test_page_to_disk_schema_and_view(self):
45
+ data = {
46
+ "x": [1, 2, 3, 4],
47
+ "y": ["a", "b", "c", "d"],
48
+ "z": [True, False, True, False],
49
+ }
50
+
51
+ mem = Table(data)
52
+ disk = Table(data, page_to_disk=True)
53
+ assert disk.schema() == mem.schema()
54
+ assert disk.view().to_columns() == mem.view().to_columns()
55
+
56
+ def test_page_to_disk_csv(self):
57
+ data = "x,y,z\n1,a,true\n2,b,false\n3,c,true\n4,d,false"
58
+ tbl = Table(data, page_to_disk=True)
59
+ assert tbl.schema() == {"x": "integer", "y": "string", "z": "boolean"}
60
+ assert tbl.view().to_columns() == {
61
+ "x": [1, 2, 3, 4],
62
+ "y": ["a", "b", "c", "d"],
63
+ "z": [True, False, True, False],
64
+ }
65
+
66
+ def test_page_to_disk_update_indexed(self):
67
+ mem = Table({"x": [1, 2, 3], "y": [1.0, 2.0, 3.0]}, index="x")
68
+ disk = Table(
69
+ {"x": [1, 2, 3], "y": [1.0, 2.0, 3.0]}, index="x", page_to_disk=True
70
+ )
71
+ update = {"x": [2, 4], "y": [20.0, 40.0]}
72
+ mem.update(update)
73
+ disk.update(update)
74
+ assert disk.view().to_columns() == mem.view().to_columns()
75
+ assert disk.size() == mem.size()
76
+
77
+ def test_page_to_disk_group_by_aggregation(self):
78
+ data = {"g": ["a", "b", "a", "b", "a"], "v": [1, 2, 3, 4, 5]}
79
+ mem = Table(data)
80
+ disk = Table(data, page_to_disk=True)
81
+ config = {"group_by": ["g"], "columns": ["v"], "aggregates": {"v": "sum"}}
82
+ assert disk.view(**config).to_columns() == mem.view(**config).to_columns()
83
+
84
+ def test_page_to_disk_arrow_roundtrip(self):
85
+ data = {"x": list(range(100)), "y": [float(i) / 2 for i in range(100)]}
86
+ mem = Table(data)
87
+ disk = Table(data, page_to_disk=True)
88
+ assert disk.view().to_arrow() == mem.view().to_arrow()
89
+
90
+ def test_page_to_disk_creates_backing_files(self):
91
+ before = _perspective_temp_dirs()
92
+ tbl = Table({"x": [1, 2, 3], "y": ["a", "b", "c"]}, page_to_disk=True)
93
+
94
+ # Touch the table so it is not optimized away before we inspect the FS.
95
+ assert tbl.size() == 3
96
+ after = _perspective_temp_dirs()
97
+ new_dirs = after - before
98
+ assert new_dirs, "expected a new perspective_<uuid> directory on disk"
99
+ assert any(os.listdir(d) for d in new_dirs), "expected column files on disk"
100
+
101
+ def test_memory_table_creates_no_backing_files(self):
102
+ before = _perspective_temp_dirs()
103
+ tbl = Table({"x": [1, 2, 3]})
104
+ assert tbl.size() == 3
105
+ after = _perspective_temp_dirs()
106
+ assert after == before, "in-memory table must not write to disk"
107
+
108
+ def test_page_to_disk_growth_forces_resize(self):
109
+ tbl = Table({"x": [0], "y": [0.0]}, index="x", page_to_disk=True)
110
+ n = 50000
111
+ tbl.update({"x": list(range(n)), "y": [float(i) for i in range(n)]})
112
+ assert tbl.size() == n
113
+ cols = tbl.view().to_columns()
114
+ assert cols["x"][0] == 0
115
+ assert cols["x"][-1] == n - 1
116
+ assert cols["y"][-1] == float(n - 1)
117
+
118
+ def test_page_to_disk_clone_keeps_master_files(self):
119
+ # Cloning a disk-backed column (e.g. the masked clone that serializing a
120
+ # table with removed rows performs) must give the clone its OWN backing
121
+ # file. Otherwise the clone aliases — and on teardown `rmfile`s — the
122
+ # master's file, silently unlinking the master's named backing store.
123
+ before = _perspective_temp_dirs()
124
+ tbl = Table(
125
+ {"x": [1, 2, 3, 4], "y": [10.0, 20.0, 30.0, 40.0]},
126
+ index="x",
127
+ page_to_disk=True,
128
+ )
129
+ view = tbl.view()
130
+ new_dirs = _perspective_temp_dirs() - before
131
+ master_dirs = [
132
+ d
133
+ for d in new_dirs
134
+ if not os.path.basename(d).startswith("perspective_expr_")
135
+ ]
136
+ assert len(master_dirs) == 1
137
+ master_dir = master_dirs[0]
138
+ master_files = set(os.listdir(master_dir))
139
+ assert master_files, "expected master backing files on disk"
140
+
141
+ # Removing rows triggers a masked clone of the disk master columns.
142
+ tbl.remove([2, 3])
143
+ assert view.to_columns()["x"] == [1, 4]
144
+ # The master's own backing files must survive the clone's teardown.
145
+ survived = set(os.listdir(master_dir))
146
+ assert master_files <= survived, (
147
+ "clone teardown unlinked master backing files: {}".format(
148
+ master_files - survived
149
+ )
150
+ )
151
+
152
+ # And the master must remain usable for subsequent updates.
153
+ tbl.update({"x": [5, 6], "y": [50.0, 60.0]})
154
+ assert sorted(view.to_columns()["x"]) == [1, 4, 5, 6]
155
+
156
+ def test_page_to_disk_larger_dataset_matches_memory(self):
157
+ n = 20000
158
+ data = {
159
+ "i": list(range(n)),
160
+ "f": [float(i) * 1.5 for i in range(n)],
161
+ "s": ["row_{}".format(i % 97) for i in range(n)],
162
+ }
163
+
164
+ mem = Table(data)
165
+ disk = Table(data, page_to_disk=True)
166
+ assert disk.view().to_arrow() == mem.view().to_arrow()
167
+
168
+
169
+ def _perspective_expr_dirs():
170
+ """`perspective_expr_*` directories — the on-disk expression `m_master`."""
171
+ tmp = tempfile.gettempdir()
172
+ try:
173
+ entries = os.listdir(tmp)
174
+ except OSError:
175
+ return set()
176
+
177
+ return {
178
+ os.path.join(tmp, e)
179
+ for e in entries
180
+ if e.startswith("perspective_expr_") and os.path.isdir(os.path.join(tmp, e))
181
+ }
182
+
183
+
184
+ class TestTableOnDiskExpressions:
185
+ def test_expression_numeric_equivalence(self):
186
+ data = {"x": [1, 2, 3, 4], "y": [10.0, 20.0, 30.0, 40.0]}
187
+ mem = Table(data)
188
+ disk = Table(data, page_to_disk=True)
189
+ exprs = {"sum": '"x" + "y"', "prod": '"x" * "y"'}
190
+ assert (
191
+ disk.view(expressions=exprs).to_columns()
192
+ == mem.view(expressions=exprs).to_columns()
193
+ )
194
+
195
+ def test_expression_string_equivalence(self):
196
+ data = {"a": ["foo", "bar", "baz"], "b": ["AA", "BB", "CC"]}
197
+ mem = Table(data)
198
+ disk = Table(data, page_to_disk=True)
199
+ exprs = {"up": 'upper("a")', "lo": 'lower("b")'}
200
+ assert (
201
+ disk.view(expressions=exprs).to_columns()
202
+ == mem.view(expressions=exprs).to_columns()
203
+ )
204
+
205
+ def test_expression_with_group_by_equivalence(self):
206
+ data = {"g": ["a", "b", "a", "b"], "v": [1, 2, 3, 4]}
207
+ mem = Table(data)
208
+ disk = Table(data, page_to_disk=True)
209
+ config = {
210
+ "expressions": {"v2": '"v" * 2'},
211
+ "group_by": ["g"],
212
+ "columns": ["v2"],
213
+ "aggregates": {"v2": "sum"},
214
+ }
215
+
216
+ assert disk.view(**config).to_columns() == mem.view(**config).to_columns()
217
+
218
+ def test_expression_master_is_page_to_disk(self):
219
+ before = _perspective_expr_dirs()
220
+ tbl = Table({"x": [1, 2, 3], "y": [10.0, 20.0, 30.0]}, page_to_disk=True)
221
+ view = tbl.view(expressions={"e": '"x" + "y"'})
222
+ assert view.to_columns()["e"] == [11.0, 22.0, 33.0]
223
+ new_dirs = _perspective_expr_dirs() - before
224
+ assert new_dirs, (
225
+ "expected a perspective_expr_<uuid> dir for the expression master"
226
+ )
227
+
228
+ assert any(os.listdir(d) for d in new_dirs), (
229
+ "expected expression backing files on disk"
230
+ )
231
+
232
+ def test_memory_expression_creates_no_expr_dir(self):
233
+ before = _perspective_expr_dirs()
234
+ tbl = Table({"x": [1, 2, 3], "y": [10.0, 20.0, 30.0]})
235
+ view = tbl.view(expressions={"e": '"x" + "y"'})
236
+ assert view.to_columns()["e"] == [11.0, 22.0, 33.0]
237
+ assert _perspective_expr_dirs() == before, (
238
+ "in-memory table must not write expression data to disk"
239
+ )
240
+
241
+ def test_expression_page_to_disk_update_and_larger(self):
242
+ n = 10000
243
+ tbl = Table({"x": [0], "y": [0.0]}, index="x", page_to_disk=True)
244
+ tbl.update({"x": list(range(n)), "y": [float(i) for i in range(n)]})
245
+ cols = tbl.view(expressions={"e": '"x" + "y"'}).to_columns()
246
+ assert cols["e"][0] == 0.0
247
+ assert cols["e"][-1] == float((n - 1) + (n - 1))
248
+
249
+
250
+ class TestResidency:
251
+ # The residency manager evicts disk-backed column buffers to their files
252
+ # when over the `PSP_MEMORY_BUDGET` and restores them transparently on
253
+ # access. Under a tiny budget, eviction fires aggressively and data must
254
+ # still round-trip identically to an in-memory table.
255
+
256
+ @mark.skip(reason="No secret hooks in the engine")
257
+ def test_residency_evicts_and_data_is_correct(self):
258
+ stats_fd, stats_path = tempfile.mkstemp(prefix="psp_residency_")
259
+ os.close(stats_fd)
260
+ os.environ["PSP_MEMORY_BUDGET"] = "1024"
261
+ os.environ["PSP_RESIDENCY_STATS_FILE"] = stats_path
262
+ try:
263
+ n = 5000
264
+ data = {
265
+ "x": list(range(n)),
266
+ "y": [float(i) * 1.5 for i in range(n)],
267
+ "s": ["row_{}".format(i % 50) for i in range(n)],
268
+ }
269
+ mem = Table(data)
270
+ disk = Table(data, page_to_disk=True)
271
+
272
+ # Each request is a safepoint that trims to budget; data read back
273
+ # must be correct (round-tripped through evict -> restore).
274
+ assert disk.view().to_columns() == mem.view().to_columns()
275
+ assert disk.view().to_arrow() == mem.view().to_arrow()
276
+
277
+ upd = {
278
+ "x": list(range(n, n + 200)),
279
+ "y": [float(i) for i in range(n, n + 200)],
280
+ "s": ["upd_{}".format(i) for i in range(200)],
281
+ }
282
+ mem.update(upd)
283
+ disk.update(upd)
284
+ assert disk.view().to_columns() == mem.view().to_columns()
285
+
286
+ # An expression view on an evicted disk table must still compute.
287
+ exprs = {"e": '"x" + "y"'}
288
+ assert (
289
+ disk.view(expressions=exprs).to_columns()
290
+ == mem.view(expressions=exprs).to_columns()
291
+ )
292
+
293
+ # Confirm eviction actually occurred (otherwise the test is vacuous).
294
+ with open(stats_path) as f:
295
+ stats = f.read()
296
+ evictions = int(stats.split("evictions=")[1].split()[0])
297
+ assert evictions > 0, "expected evictions under a tiny budget: " + stats
298
+ finally:
299
+ os.environ.pop("PSP_MEMORY_BUDGET", None)
300
+ os.environ.pop("PSP_RESIDENCY_STATS_FILE", None)
301
+ os.remove(stats_path)
302
+ # Drain a safepoint with residency disabled so any evicted stores
303
+ # from other live tables are restored before subsequent tests.
304
+ Table({"_": [0]}).view().to_columns()