jcmsuite 7.4.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of jcmsuite might be problematic. Click here for more details.

Files changed (119) hide show
  1. jcmsuite/__init__.py +22 -0
  2. jcmsuite/_build_info.py +4 -0
  3. jcmsuite/context/__init__.py +44 -0
  4. jcmsuite/context/interface_metadata.py +158 -0
  5. jcmsuite/context/models.py +213 -0
  6. jcmsuite/context/scope.py +210 -0
  7. jcmsuite/context/solver_context.py +569 -0
  8. jcmsuite/context/subprocess_tools.py +172 -0
  9. jcmsuite/contract.py +47 -0
  10. jcmsuite/daemon/__init__.py +19 -0
  11. jcmsuite/daemon/handler.py +925 -0
  12. jcmsuite/daemon/internal/__init__.py +4 -0
  13. jcmsuite/daemon/internal/bundle_protocol.py +64 -0
  14. jcmsuite/daemon/internal/handler_helpers.py +368 -0
  15. jcmsuite/daemon/internal/handler_resource_jobs.py +227 -0
  16. jcmsuite/daemon/internal/handler_wait.py +564 -0
  17. jcmsuite/daemon/models.py +349 -0
  18. jcmsuite/daemon/namespaces.py +250 -0
  19. jcmsuite/daemon/protocol.py +801 -0
  20. jcmsuite/daemon/resources.py +407 -0
  21. jcmsuite/daemon/scope.py +53 -0
  22. jcmsuite/daemon/transport.py +223 -0
  23. jcmsuite/editing/__init__.py +10 -0
  24. jcmsuite/editing/editor.py +274 -0
  25. jcmsuite/exceptions/__init__.py +32 -0
  26. jcmsuite/exceptions/errors.py +173 -0
  27. jcmsuite/geo/__init__.py +10 -0
  28. jcmsuite/geo/mesh.py +302 -0
  29. jcmsuite/info/__init__.py +10 -0
  30. jcmsuite/info/runtime_info.py +45 -0
  31. jcmsuite/internal/__init__.py +2 -0
  32. jcmsuite/internal/diagnostics.py +74 -0
  33. jcmsuite/internal/progress.py +80 -0
  34. jcmsuite/loaders/__init__.py +18 -0
  35. jcmsuite/loaders/blob_header.py +315 -0
  36. jcmsuite/loaders/cartesian_field_reader.py +249 -0
  37. jcmsuite/loaders/cartesian_fields.py +324 -0
  38. jcmsuite/loaders/nested_mapping.py +69 -0
  39. jcmsuite/loaders/powerflux.py +136 -0
  40. jcmsuite/loaders/table.py +637 -0
  41. jcmsuite/logging/__init__.py +22 -0
  42. jcmsuite/logging/runtime.py +210 -0
  43. jcmsuite/results/__init__.py +126 -0
  44. jcmsuite/results/artifacts.py +806 -0
  45. jcmsuite/results/discovery.py +82 -0
  46. jcmsuite/results/field_exports.py +1062 -0
  47. jcmsuite/results/field_payloads.py +453 -0
  48. jcmsuite/results/internal/__init__.py +4 -0
  49. jcmsuite/results/internal/export/__init__.py +4 -0
  50. jcmsuite/results/internal/export/common.py +56 -0
  51. jcmsuite/results/internal/export/pandas/__init__.py +4 -0
  52. jcmsuite/results/internal/export/pandas/common.py +165 -0
  53. jcmsuite/results/internal/export/pandas/payload.py +480 -0
  54. jcmsuite/results/internal/export/pandas/rows.py +484 -0
  55. jcmsuite/results/internal/export/preview.py +178 -0
  56. jcmsuite/results/internal/export/xarray/__init__.py +4 -0
  57. jcmsuite/results/internal/export/xarray/export.py +688 -0
  58. jcmsuite/results/internal/export/xarray/grouped.py +608 -0
  59. jcmsuite/results/internal/export/xarray/headers.py +290 -0
  60. jcmsuite/results/internal/export/xarray/runtime.py +99 -0
  61. jcmsuite/results/internal/export/xarray/shared.py +246 -0
  62. jcmsuite/results/internal/export/xarray/tabular.py +304 -0
  63. jcmsuite/results/internal/export/xarray/types.py +28 -0
  64. jcmsuite/results/internal/models/__init__.py +4 -0
  65. jcmsuite/results/internal/models/batch.py +1081 -0
  66. jcmsuite/results/internal/models/batch_postprocess.py +1728 -0
  67. jcmsuite/results/internal/models/bundle.py +343 -0
  68. jcmsuite/results/internal/models/core.py +556 -0
  69. jcmsuite/results/internal/models/submission.py +1157 -0
  70. jcmsuite/results/internal/resultbag/__init__.py +4 -0
  71. jcmsuite/results/internal/resultbag/access.py +489 -0
  72. jcmsuite/results/internal/resultbag/iteration.py +199 -0
  73. jcmsuite/results/internal/resultbag/query.py +226 -0
  74. jcmsuite/results/internal/resultbag/raw.py +407 -0
  75. jcmsuite/results/internal/resultbag/source_files.py +142 -0
  76. jcmsuite/results/internal/resultbag/store.py +881 -0
  77. jcmsuite/results/internal/resultbag/utils.py +258 -0
  78. jcmsuite/results/models.py +76 -0
  79. jcmsuite/results/opening.py +55 -0
  80. jcmsuite/results/pandas_export.py +325 -0
  81. jcmsuite/results/payloads.py +478 -0
  82. jcmsuite/results/postprocess.py +97 -0
  83. jcmsuite/results/postprocess_payloads.py +757 -0
  84. jcmsuite/results/postprocess_record.py +418 -0
  85. jcmsuite/results/resultbag.py +279 -0
  86. jcmsuite/results/resultbag_management.py +433 -0
  87. jcmsuite/results/structured_derivatives.py +387 -0
  88. jcmsuite/solve/__init__.py +10 -0
  89. jcmsuite/solve/daemon_bundle_staging.py +100 -0
  90. jcmsuite/solve/daemon_bundle_wait.py +89 -0
  91. jcmsuite/solve/orchestration.py +1576 -0
  92. jcmsuite/solve/orchestration_execution.py +180 -0
  93. jcmsuite/solve/orchestration_routing.py +128 -0
  94. jcmsuite/solve/orchestration_support.py +323 -0
  95. jcmsuite/solve/path_resolution.py +98 -0
  96. jcmsuite/solve/progress.py +20 -0
  97. jcmsuite/solve/solver.py +709 -0
  98. jcmsuite/solve/staging.py +264 -0
  99. jcmsuite/solve/types.py +25 -0
  100. jcmsuite/solve/working_dirs.py +227 -0
  101. jcmsuite/templates/__init__.py +11 -0
  102. jcmsuite/templates/converter.py +71 -0
  103. jcmsuite/templates/formatting.py +335 -0
  104. jcmsuite/templates/formatting_cli.py +47 -0
  105. jcmsuite/templates/rendering.py +666 -0
  106. jcmsuite/torch/__init__.py +25 -0
  107. jcmsuite/torch/autograd.py +488 -0
  108. jcmsuite/torch/coordinate_policy.py +82 -0
  109. jcmsuite/torch/core.py +312 -0
  110. jcmsuite/torch/results/__init__.py +51 -0
  111. jcmsuite/torch/types.py +84 -0
  112. jcmsuite/torch/validation.py +208 -0
  113. jcmsuite/torch/wrappers.py +4332 -0
  114. jcmsuite/view/__init__.py +10 -0
  115. jcmsuite/view/viewer.py +122 -0
  116. jcmsuite-7.4.2.dist-info/METADATA +180 -0
  117. jcmsuite-7.4.2.dist-info/RECORD +119 -0
  118. jcmsuite-7.4.2.dist-info/WHEEL +4 -0
  119. jcmsuite-7.4.2.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,484 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # Copyright (c) 2001-2026 JCMwave GmbH
3
+
4
+ """Row-builder helpers for pandas exports."""
5
+
6
+ from __future__ import annotations
7
+
8
+ from collections.abc import Sequence
9
+ from pathlib import Path
10
+ from typing import TYPE_CHECKING
11
+
12
+ from .common import expanded_keys, postprocess_value_mapping
13
+ from .payload import flatten_rows, tabular_rows, value_mapping
14
+
15
+ if TYPE_CHECKING:
16
+ from ....internal.models.bundle import (
17
+ BundledBatchSolveResult,
18
+ BundledSolveEntry,
19
+ BundledSolveResult,
20
+ )
21
+ from ....models import BatchSolveResult, SolveResult
22
+
23
+
24
+ def solve_summary_row(result: "SolveResult") -> dict[str, object]:
25
+ """Build one summary dataframe row for a single solve result."""
26
+
27
+ return {
28
+ "project_file": (
29
+ None if result.project_file is None else str(result.project_file)
30
+ ),
31
+ "backend_used": result.backend_used,
32
+ "daemon_job_id": result.daemon_job_id,
33
+ "file": None if result.file is None else str(result.file),
34
+ "has_costs": result.costs is not None,
35
+ "has_eigenvalues": result.eigenvalues is not None,
36
+ "postprocess_count": len(result.postprocesses),
37
+ "postprocess_titles": result.postprocesses.titles(),
38
+ "template_render_count": len(result.template_renders),
39
+ "log_return_code": None if result.log is None else result.log.return_code,
40
+ "log_streamed_to_console": (
41
+ None if result.log is None else result.log.streamed_to_console
42
+ ),
43
+ }
44
+
45
+
46
+ def solve_long_rows(result: "SolveResult") -> list[dict[str, object]]:
47
+ """Flatten one solve result into long-form ``section/path/value`` rows."""
48
+
49
+ rows: list[dict[str, object]] = []
50
+ summary_mapping: dict[str, object] = solve_summary_row(result)
51
+ rows.extend(
52
+ flatten_rows(
53
+ section="summary",
54
+ payload=summary_mapping,
55
+ source_file=None,
56
+ postprocess_index=None,
57
+ postprocess_title=None,
58
+ )
59
+ )
60
+
61
+ if result.costs is not None:
62
+ rows.extend(
63
+ flatten_rows(
64
+ section="costs",
65
+ payload=value_mapping(result.costs.to_dict()),
66
+ source_file=None,
67
+ postprocess_index=None,
68
+ postprocess_title=None,
69
+ )
70
+ )
71
+
72
+ if result.eigenvalues is not None:
73
+ rows.extend(
74
+ flatten_rows(
75
+ section="eigenvalues",
76
+ payload=value_mapping(result.eigenvalues.to_dict()),
77
+ source_file=None,
78
+ postprocess_index=None,
79
+ postprocess_title=None,
80
+ )
81
+ )
82
+
83
+ postprocess_index: int
84
+ for postprocess_index, postprocess in enumerate(result.postprocesses.records):
85
+ if not postprocess.available:
86
+ continue
87
+ payload_mapping = postprocess_value_mapping(postprocess)
88
+ payload_object: object = (
89
+ payload_mapping if payload_mapping is not None else postprocess._raw
90
+ )
91
+ rows.extend(
92
+ flatten_rows(
93
+ section="postprocess",
94
+ payload=payload_object,
95
+ source_file=postprocess.source_file,
96
+ postprocess_index=postprocess_index,
97
+ postprocess_title=postprocess.title,
98
+ )
99
+ )
100
+ return rows
101
+
102
+
103
+ def solve_value_rows(result: "SolveResult") -> list[dict[str, object]]:
104
+ """Build tabular value rows for a single solve result payload."""
105
+
106
+ rows: list[dict[str, object]] = []
107
+
108
+ if result.costs is not None:
109
+ rows.extend(
110
+ tabular_rows(
111
+ section="costs",
112
+ payload=value_mapping(result.costs.to_dict()),
113
+ source_file=None,
114
+ postprocess_index=None,
115
+ postprocess_title=None,
116
+ )
117
+ )
118
+
119
+ if result.eigenvalues is not None:
120
+ rows.extend(
121
+ tabular_rows(
122
+ section="eigenvalues",
123
+ payload=value_mapping(result.eigenvalues.to_dict()),
124
+ source_file=None,
125
+ postprocess_index=None,
126
+ postprocess_title=None,
127
+ )
128
+ )
129
+
130
+ postprocess_index: int
131
+ for postprocess_index, postprocess in enumerate(result.postprocesses.records):
132
+ if not postprocess.available:
133
+ continue
134
+ payload_mapping = postprocess_value_mapping(postprocess)
135
+ payload_object = (
136
+ payload_mapping if payload_mapping is not None else postprocess._raw
137
+ )
138
+ rows.extend(
139
+ tabular_rows(
140
+ section="postprocess",
141
+ payload=payload_object,
142
+ source_file=postprocess.source_file,
143
+ postprocess_index=postprocess_index,
144
+ postprocess_title=postprocess.title,
145
+ )
146
+ )
147
+
148
+ return rows
149
+
150
+
151
+ def batch_summary_rows(batch_result: "BatchSolveResult") -> list[dict[str, object]]:
152
+ """Build summary dataframe rows for each batch entry."""
153
+
154
+ rows: list[dict[str, object]] = []
155
+ for entry in batch_result:
156
+ summary_row: dict[str, object] = {
157
+ "batch_index": entry.index,
158
+ "success": entry.result is not None,
159
+ "failure_type": (
160
+ None if entry.failure is None else entry.failure.exception_type.__name__
161
+ ),
162
+ "failure_message": None if entry.failure is None else entry.failure.message,
163
+ }
164
+ summary_row.update(expanded_keys(entry.keys))
165
+
166
+ if entry.result is not None:
167
+ solve_summary: dict[str, object] = solve_summary_row(entry.result)
168
+ solve_key: str
169
+ solve_value: object
170
+ for solve_key, solve_value in solve_summary.items():
171
+ summary_row[f"result_{solve_key}"] = solve_value
172
+ rows.append(summary_row)
173
+ return rows
174
+
175
+
176
+ def batch_long_rows(batch_result: "BatchSolveResult") -> list[dict[str, object]]:
177
+ """Build long-form dataframe rows for each batch entry payload."""
178
+
179
+ rows: list[dict[str, object]] = []
180
+ for entry in batch_result:
181
+ prefix: dict[str, object] = {
182
+ "batch_index": entry.index,
183
+ "success": entry.result is not None,
184
+ }
185
+ prefix.update(expanded_keys(entry.keys))
186
+
187
+ if entry.result is None:
188
+ failure_row: dict[str, object] = dict(prefix)
189
+ failure_row.update(
190
+ {
191
+ "section": "failure",
192
+ "source_file": None,
193
+ "postprocess_index": None,
194
+ "postprocess_title": None,
195
+ "path": "message",
196
+ "value": None if entry.failure is None else entry.failure.message,
197
+ }
198
+ )
199
+ rows.append(failure_row)
200
+ continue
201
+
202
+ solve_row: dict[str, object]
203
+ for solve_row in solve_long_rows(entry.result):
204
+ merged_row: dict[str, object] = dict(prefix)
205
+ merged_row.update(solve_row)
206
+ rows.append(merged_row)
207
+ return rows
208
+
209
+
210
+ def batch_value_rows(batch_result: "BatchSolveResult") -> list[dict[str, object]]:
211
+ """Build tabular value rows for each batch entry payload."""
212
+
213
+ rows: list[dict[str, object]] = []
214
+ for entry in batch_result:
215
+ prefix: dict[str, object] = {
216
+ "batch_index": entry.index,
217
+ "success": entry.result is not None,
218
+ }
219
+ prefix.update(expanded_keys(entry.keys))
220
+
221
+ if entry.result is None:
222
+ failure_row: dict[str, object] = dict(prefix)
223
+ failure_row.update(
224
+ {
225
+ "section": "failure",
226
+ "row_index": 0,
227
+ "failure_message": (
228
+ None if entry.failure is None else entry.failure.message
229
+ ),
230
+ }
231
+ )
232
+ rows.append(failure_row)
233
+ continue
234
+
235
+ solve_row: dict[str, object]
236
+ for solve_row in solve_value_rows(entry.result):
237
+ merged_row: dict[str, object] = dict(prefix)
238
+ merged_row.update(solve_row)
239
+ rows.append(merged_row)
240
+ return rows
241
+
242
+
243
+ def bundled_summary_rows(
244
+ bundle_result: "BundledSolveResult",
245
+ ) -> list[dict[str, object]]:
246
+ """Build summary dataframe rows for each project in a bundle."""
247
+
248
+ rows: list[dict[str, object]] = []
249
+ project_index: int
250
+ bundle_entry: "BundledSolveEntry"
251
+ for project_index, bundle_entry in enumerate(bundle_result.entries):
252
+ summary_row: dict[str, object] = {
253
+ "project_index": project_index,
254
+ "project_file": str(bundle_entry.project_file),
255
+ }
256
+ summary_row.update(solve_summary_row(bundle_entry.result))
257
+ summary_row["project_file"] = str(bundle_entry.project_file)
258
+ rows.append(summary_row)
259
+ return rows
260
+
261
+
262
+ def bundled_long_rows(bundle_result: "BundledSolveResult") -> list[dict[str, object]]:
263
+ """Build long-form dataframe rows for each project in a bundle."""
264
+
265
+ rows: list[dict[str, object]] = []
266
+ project_index: int
267
+ bundle_entry: "BundledSolveEntry"
268
+ for project_index, bundle_entry in enumerate(bundle_result.entries):
269
+ project_prefix: dict[str, object] = {
270
+ "project_index": project_index,
271
+ "project_file": str(bundle_entry.project_file),
272
+ }
273
+ solve_row: dict[str, object]
274
+ for solve_row in solve_long_rows(bundle_entry.result):
275
+ merged_row: dict[str, object] = dict(project_prefix)
276
+ merged_row.update(solve_row)
277
+ merged_row["project_file"] = str(bundle_entry.project_file)
278
+ rows.append(merged_row)
279
+ return rows
280
+
281
+
282
+ def bundled_value_rows(bundle_result: "BundledSolveResult") -> list[dict[str, object]]:
283
+ """Build tabular value rows for each project in a bundle."""
284
+
285
+ rows: list[dict[str, object]] = []
286
+ project_index: int
287
+ bundle_entry: "BundledSolveEntry"
288
+ for project_index, bundle_entry in enumerate(bundle_result.entries):
289
+ project_prefix: dict[str, object] = {
290
+ "project_index": project_index,
291
+ "project_file": str(bundle_entry.project_file),
292
+ }
293
+ solve_row: dict[str, object]
294
+ for solve_row in solve_value_rows(bundle_entry.result):
295
+ merged_row: dict[str, object] = dict(project_prefix)
296
+ merged_row.update(solve_row)
297
+ merged_row["project_file"] = str(bundle_entry.project_file)
298
+ rows.append(merged_row)
299
+ return rows
300
+
301
+
302
+ def bundled_batch_summary_rows(
303
+ batch_bundle_result: "BundledBatchSolveResult",
304
+ ) -> list[dict[str, object]]:
305
+ """Build summary dataframe rows for a batch of bundled solves."""
306
+
307
+ rows: list[dict[str, object]] = []
308
+ project_files: tuple[Path | None, ...] = (
309
+ batch_bundle_result._require_project_axis_files()
310
+ )
311
+ project_index: int
312
+ project_file: Path | None
313
+ for project_index, project_file in enumerate(project_files):
314
+ projected_batch: "BatchSolveResult" = batch_bundle_result.project(project_index)
315
+ batch_row: dict[str, object]
316
+ for batch_row in batch_summary_rows(projected_batch):
317
+ merged_row: dict[str, object] = {
318
+ "project_index": project_index,
319
+ "project_file": str(project_file),
320
+ }
321
+ merged_row.update(batch_row)
322
+ rows.append(merged_row)
323
+ return rows
324
+
325
+
326
+ def bundled_batch_long_rows(
327
+ batch_bundle_result: "BundledBatchSolveResult",
328
+ ) -> list[dict[str, object]]:
329
+ """Build long-form dataframe rows for a batch of bundled solves."""
330
+
331
+ rows: list[dict[str, object]] = []
332
+ project_files: tuple[Path | None, ...] = (
333
+ batch_bundle_result._require_project_axis_files()
334
+ )
335
+ project_index: int
336
+ project_file: Path | None
337
+ for project_index, project_file in enumerate(project_files):
338
+ projected_batch: "BatchSolveResult" = batch_bundle_result.project(project_index)
339
+ batch_row: dict[str, object]
340
+ for batch_row in batch_long_rows(projected_batch):
341
+ merged_row: dict[str, object] = {
342
+ "project_index": project_index,
343
+ "project_file": str(project_file),
344
+ }
345
+ merged_row.update(batch_row)
346
+ rows.append(merged_row)
347
+ return rows
348
+
349
+
350
+ def bundled_batch_value_rows(
351
+ batch_bundle_result: "BundledBatchSolveResult",
352
+ ) -> list[dict[str, object]]:
353
+ """Build tabular value rows for a batch of bundled solves."""
354
+
355
+ rows: list[dict[str, object]] = []
356
+ project_files: tuple[Path | None, ...] = (
357
+ batch_bundle_result._require_project_axis_files()
358
+ )
359
+ project_index: int
360
+ project_file: Path | None
361
+ for project_index, project_file in enumerate(project_files):
362
+ projected_batch: "BatchSolveResult" = batch_bundle_result.project(project_index)
363
+ batch_row: dict[str, object]
364
+ for batch_row in batch_value_rows(projected_batch):
365
+ merged_row: dict[str, object] = {
366
+ "project_index": project_index,
367
+ "project_file": str(project_file),
368
+ }
369
+ merged_row.update(batch_row)
370
+ rows.append(merged_row)
371
+ return rows
372
+
373
+
374
+ def resultbag_summary_rows(
375
+ items: Sequence[tuple[dict[str, object], object]],
376
+ ) -> list[dict[str, object]]:
377
+ """Build summary dataframe rows from resultbag key/payload entries."""
378
+
379
+ from ....models import SolveResult
380
+
381
+ rows: list[dict[str, object]] = []
382
+ row_index: int
383
+ keys: dict[str, object]
384
+ result_payload: object
385
+ for row_index, (keys, result_payload) in enumerate(items):
386
+ row: dict[str, object] = {
387
+ "index": row_index,
388
+ "result_type": type(result_payload).__name__,
389
+ }
390
+ row.update(expanded_keys(keys))
391
+
392
+ if isinstance(result_payload, SolveResult):
393
+ solve_summary: dict[str, object] = solve_summary_row(result_payload)
394
+ summary_key: str
395
+ summary_value: object
396
+ for summary_key, summary_value in solve_summary.items():
397
+ row[f"result_{summary_key}"] = summary_value
398
+ rows.append(row)
399
+ return rows
400
+
401
+
402
+ def resultbag_long_rows(
403
+ items: Sequence[tuple[dict[str, object], object]],
404
+ ) -> list[dict[str, object]]:
405
+ """Build long-form dataframe rows from resultbag key/payload entries."""
406
+
407
+ from ....models import SolveResult
408
+
409
+ rows: list[dict[str, object]] = []
410
+ row_index: int
411
+ keys: dict[str, object]
412
+ result_payload: object
413
+ for row_index, (keys, result_payload) in enumerate(items):
414
+ prefix: dict[str, object] = {
415
+ "index": row_index,
416
+ "result_type": type(result_payload).__name__,
417
+ }
418
+ prefix.update(expanded_keys(keys))
419
+
420
+ if isinstance(result_payload, dict):
421
+ payload_rows: list[dict[str, object]] = flatten_rows(
422
+ section="result_payload",
423
+ payload=result_payload,
424
+ source_file=None,
425
+ postprocess_index=None,
426
+ postprocess_title=None,
427
+ )
428
+ elif isinstance(result_payload, SolveResult):
429
+ payload_rows = solve_long_rows(result_payload)
430
+ else:
431
+ payload_rows = [
432
+ {
433
+ "section": "result_payload",
434
+ "source_file": None,
435
+ "postprocess_index": None,
436
+ "postprocess_title": None,
437
+ "path": "value",
438
+ "value": result_payload,
439
+ }
440
+ ]
441
+
442
+ payload_row: dict[str, object]
443
+ for payload_row in payload_rows:
444
+ merged: dict[str, object] = dict(prefix)
445
+ merged.update(payload_row)
446
+ rows.append(merged)
447
+ return rows
448
+
449
+
450
+ def resultbag_value_rows(
451
+ items: Sequence[tuple[dict[str, object], object]],
452
+ ) -> list[dict[str, object]]:
453
+ """Build tabular value rows from resultbag key/payload entries."""
454
+
455
+ from ....models import SolveResult
456
+
457
+ rows: list[dict[str, object]] = []
458
+ row_index: int
459
+ keys: dict[str, object]
460
+ result_payload: object
461
+ for row_index, (keys, result_payload) in enumerate(items):
462
+ prefix: dict[str, object] = {
463
+ "index": row_index,
464
+ "result_type": type(result_payload).__name__,
465
+ }
466
+ prefix.update(expanded_keys(keys))
467
+
468
+ payload_rows: list[dict[str, object]]
469
+ if isinstance(result_payload, SolveResult):
470
+ payload_rows = solve_value_rows(result_payload)
471
+ else:
472
+ payload_rows = tabular_rows(
473
+ section="result_payload",
474
+ payload=result_payload,
475
+ source_file=None,
476
+ postprocess_index=None,
477
+ postprocess_title=None,
478
+ )
479
+ payload_row: dict[str, object]
480
+ for payload_row in payload_rows:
481
+ merged: dict[str, object] = dict(prefix)
482
+ merged.update(payload_row)
483
+ rows.append(merged)
484
+ return rows
@@ -0,0 +1,178 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # Copyright (c) 2001-2026 JCMwave GmbH
3
+
4
+ """Bounded text previews for result payloads."""
5
+
6
+ from __future__ import annotations
7
+
8
+ from collections.abc import Mapping, Sequence
9
+ from typing import TypeVar
10
+
11
+ from .pandas.payload import (
12
+ column_length_of,
13
+ column_value_at,
14
+ flatten_mapping_to_columns,
15
+ )
16
+
17
+ _PREVIEW_ROW_LIMIT = 5
18
+ _PREVIEW_COLUMN_LIMIT = 6
19
+ _PREVIEW_CELL_LIMIT = 40
20
+
21
+ _MappingKey = TypeVar("_MappingKey")
22
+
23
+
24
+ def format_table_preview(
25
+ values: Mapping[object, object],
26
+ *,
27
+ include_row_index: bool = True,
28
+ quote_strings: bool = True,
29
+ compact_values: bool = True,
30
+ ) -> str:
31
+ """Format a small text table without materializing a dataframe."""
32
+
33
+ columns: dict[str, object] = {}
34
+ if not flatten_mapping_to_columns(values, columns, path_prefix=""):
35
+ return " <non-tabular data>"
36
+ if not columns:
37
+ return " <empty>"
38
+
39
+ row_count: int = 1
40
+ column_lengths: dict[str, int] = {}
41
+ column_name: str
42
+ column_value: object
43
+ for column_name, column_value in columns.items():
44
+ column_length: int | None = column_length_of(column_value)
45
+ if column_length is None:
46
+ continue
47
+ column_lengths[column_name] = column_length
48
+ row_count = max(row_count, column_length)
49
+
50
+ if any(length not in {1, row_count} for length in column_lengths.values()):
51
+ return " <non-tabular data>"
52
+
53
+ displayed_names: tuple[str, ...] = tuple(columns)[:_PREVIEW_COLUMN_LIMIT]
54
+ preview_rows: list[tuple[str, ...]] = []
55
+ row_index: int
56
+ for row_index in range(min(row_count, _PREVIEW_ROW_LIMIT)):
57
+ row_values: tuple[str, ...] = tuple(
58
+ _format_preview_value(
59
+ columns[name],
60
+ row_index,
61
+ quote_strings=quote_strings,
62
+ compact_values=compact_values,
63
+ )
64
+ for name in displayed_names
65
+ )
66
+ preview_rows.append(row_values)
67
+
68
+ column_widths: tuple[int, ...] = tuple(
69
+ max(
70
+ len(name),
71
+ *(len(row[column_index]) for row in preview_rows),
72
+ )
73
+ for column_index, name in enumerate(displayed_names)
74
+ )
75
+ index_width: int = max(len(str(len(preview_rows) - 1)), 1)
76
+ header_prefix: str = " " * (index_width + 1) if include_row_index else ""
77
+ header: str = header_prefix + " ".join(
78
+ (
79
+ name
80
+ if column_index == len(displayed_names) - 1
81
+ else name.ljust(column_widths[column_index])
82
+ )
83
+ for column_index, name in enumerate(displayed_names)
84
+ )
85
+ lines: list[str] = [f" {header}"]
86
+ displayed_row_values: tuple[str, ...]
87
+ for row_index, displayed_row_values in enumerate(preview_rows):
88
+ rendered_values: str = " ".join(
89
+ (
90
+ value
91
+ if column_index == len(displayed_row_values) - 1
92
+ else value.ljust(column_widths[column_index])
93
+ )
94
+ for column_index, value in enumerate(displayed_row_values)
95
+ )
96
+ if include_row_index:
97
+ lines.append(f" {row_index:<{index_width}} {rendered_values}")
98
+ else:
99
+ lines.append(f" {rendered_values}")
100
+ if row_count > _PREVIEW_ROW_LIMIT or len(columns) > _PREVIEW_COLUMN_LIMIT:
101
+ lines.append(" ...")
102
+ return "\n".join(lines)
103
+
104
+
105
+ def format_mapping_preview(values: Mapping[_MappingKey, object]) -> str:
106
+ """Format a one-row preview without expanding mapping values."""
107
+
108
+ displayed_items: tuple[tuple[str, object], ...] = tuple(
109
+ (str(key), value) for key, value in values.items()
110
+ )[:_PREVIEW_COLUMN_LIMIT]
111
+ if not displayed_items:
112
+ return " <empty>"
113
+
114
+ displayed_names: tuple[str, ...] = tuple(name for name, _ in displayed_items)
115
+ preview_values: tuple[str, ...] = tuple(
116
+ _format_mapping_preview_value(value) for _, value in displayed_items
117
+ )
118
+ column_widths: tuple[int, ...] = tuple(
119
+ max(len(name), len(preview_values[column_index]))
120
+ for column_index, name in enumerate(displayed_names)
121
+ )
122
+ header: str = " ".join(
123
+ (
124
+ name
125
+ if column_index == len(displayed_names) - 1
126
+ else name.ljust(column_widths[column_index])
127
+ )
128
+ for column_index, name in enumerate(displayed_names)
129
+ )
130
+ values_line: str = " ".join(
131
+ (
132
+ value
133
+ if column_index == len(preview_values) - 1
134
+ else value.ljust(column_widths[column_index])
135
+ )
136
+ for column_index, value in enumerate(preview_values)
137
+ )
138
+ lines: list[str] = [f" {header}", f" {values_line}"]
139
+ if len(values) > _PREVIEW_COLUMN_LIMIT:
140
+ lines.append(" ...")
141
+ return "\n".join(lines)
142
+
143
+
144
+ def _format_preview_value(
145
+ value: object,
146
+ row_index: int,
147
+ *,
148
+ quote_strings: bool,
149
+ compact_values: bool,
150
+ ) -> str:
151
+ """Return one bounded, display-oriented table cell value."""
152
+
153
+ selected_value: object = column_value_at(value, row_index)
154
+ if compact_values and isinstance(selected_value, Mapping):
155
+ return "{...}"
156
+ if (
157
+ compact_values
158
+ and isinstance(selected_value, Sequence)
159
+ and not isinstance(selected_value, (str, bytes, bytearray))
160
+ ):
161
+ return "[...]"
162
+ rendered_value: str = (
163
+ selected_value
164
+ if isinstance(selected_value, str) and not quote_strings
165
+ else repr(selected_value)
166
+ )
167
+ if len(rendered_value) <= _PREVIEW_CELL_LIMIT:
168
+ return rendered_value
169
+ return f"{rendered_value[: _PREVIEW_CELL_LIMIT - 3]}..."
170
+
171
+
172
+ def _format_mapping_preview_value(value: object) -> str:
173
+ """Return one bounded mapping value without changing its cardinality."""
174
+
175
+ rendered_value: str = repr(value)
176
+ if len(rendered_value) <= _PREVIEW_CELL_LIMIT:
177
+ return rendered_value
178
+ return f"{rendered_value[: _PREVIEW_CELL_LIMIT - 3]}..."
@@ -0,0 +1,4 @@
1
+ # SPDX-License-Identifier: MIT
2
+ # Copyright (c) 2001-2026 JCMwave GmbH
3
+
4
+ """Internal xarray export helpers."""