scout-rig 0.2.5 → 0.2.7

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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/.vimproject +31 -13
  3. data/README.md +27 -4
  4. data/Rakefile +1 -0
  5. data/VERSION +1 -1
  6. data/doc/Improvements.md +396 -0
  7. data/doc/StartHere.md +95 -0
  8. data/doc/developer/Architecture.md +232 -0
  9. data/doc/developer/DataConversionInternals.md +407 -0
  10. data/doc/developer/DesignPrinciples.md +254 -0
  11. data/doc/developer/PythonTaskRunnerInternals.md +371 -0
  12. data/doc/developer/PythonWorkflowInternals.md +539 -0
  13. data/doc/developer/ScoutPythonInternals.md +504 -0
  14. data/doc/user/Cookbook.md +337 -0
  15. data/doc/user/DefiningPythonTasks.md +708 -0
  16. data/doc/user/DrivingWorkflowsFromPython.md +418 -0
  17. data/doc/user/IteratingPythonData.md +142 -0
  18. data/doc/user/PassingDataToPython.md +223 -0
  19. data/doc/user/PythonEnvironments.md +197 -0
  20. data/doc/user/RunningPythonFromRuby.md +267 -0
  21. data/doc/user/ScriptingPython.md +281 -0
  22. data/doc/user/TestingAndPackaging.md +487 -0
  23. data/lib/scout/python/script.rb +11 -2
  24. data/lib/scout/python/util.rb +9 -0
  25. data/lib/scout/workflow/python/inputs.rb +33 -23
  26. data/lib/scout/workflow/python/task.rb +219 -18
  27. data/lib/scout/workflow/python.rb +236 -9
  28. data/lib/scout-rig/testing/availability.rb +608 -0
  29. data/python/README.md +51 -0
  30. data/python/scout/__init__.py +2 -0
  31. data/python/scout/runner.py +193 -29
  32. data/python/scout/task_errors.py +6 -0
  33. data/research/00_scope_and_themes.md +131 -0
  34. data/research/data-conversion-and-scripting-analysis.md +209 -0
  35. data/research/design-philosophy-analysis.md +246 -0
  36. data/research/python-helper-package-analysis.md +272 -0
  37. data/research/pythonworkflow-analysis.md +199 -0
  38. data/research/scoutpython-bridge-analysis.md +185 -0
  39. data/research/synthesis-report.md +109 -0
  40. data/research/testing-availability-baseline.md +1625 -0
  41. data/research/testing-availability-final-report.md +500 -0
  42. data/research/validation-report.md +149 -0
  43. data/scout-rig.gemspec +35 -5
  44. data/test/scout/python/test_util.rb +13 -0
  45. data/test/scout/test_python.rb +10 -0
  46. data/test/scout/workflow/python/test_task.rb +1119 -1
  47. data/test/scout-rig/testing/test_availability.rb +495 -0
  48. data/test/scout-rig/testing/test_test_support.rb +166 -0
  49. data/test/test_helper.rb +96 -19
  50. metadata +47 -4
  51. data/doc/Python.md +0 -482
  52. data/doc/PythonWorkflow.md +0 -163
data/doc/Python.md DELETED
@@ -1,482 +0,0 @@
1
- # Python (ScoutPython)
2
-
3
- ScoutPython is the bridge between Ruby (Scout) and Python. It provides:
4
-
5
- - A thin, ergonomic layer on top of PyCall to execute Python code from Ruby (direct or in a dedicated thread).
6
- - Import helpers and a safe “binding” scope that keeps Python names local to a call site.
7
- - A small scripting facility to run ad‑hoc Python text, pass Ruby variables (including TSV) into Python, and return results.
8
- - Data conversion helpers for numpy/list and pandas DataFrame to/from TSV.
9
- - Path management to expose package Python modules to sys.path.
10
- - A companion Python package (scout) with utilities for TSV IO and Workflow execution from Python, and a small remote Workflow client.
11
-
12
- Sections:
13
- - Requirements
14
- - Path management
15
- - Running Python code from Ruby
16
- - Binding scopes and imports
17
- - Scripting: run ad‑hoc Python text
18
- - Iteration utilities
19
- - Data conversion and pandas helpers
20
- - Python-side helper package (scout)
21
- - Workflow wrapper (local)
22
- - Remote workflows over HTTP
23
- - Error handling, logging, and threading
24
- - API quick reference
25
- - Examples
26
-
27
- ---
28
-
29
- ## Requirements
30
-
31
- - Ruby gem: pycall (PyCall).
32
- - Python 3 with:
33
- - pandas (for DataFrame helpers),
34
- - numpy (for numeric conversions),
35
- - Ruby gems used in certain paths: python/pickle (for reading pickle files back into Ruby), json.
36
-
37
- Make sure a Python3 interpreter is available in PATH.
38
-
39
- ---
40
-
41
- ## Path management
42
-
43
- ScoutPython manages Python import paths and pre-populates them with known package Python dirs.
44
-
45
- - Sources of Python paths:
46
- - Scout.python.find_all returns Python directories registered via the Path subsystem; these are added automatically at load time.
47
- - You can add more at runtime.
48
-
49
- API:
50
- ```ruby
51
- # Add one or many paths for Python imports
52
- ScoutPython.add_path("/path/to/python/package")
53
- ScoutPython.add_paths(%w[/opt/mypkg/py /usr/local/mytools/py])
54
-
55
- # Apply registered paths to sys.path (idempotent; called on init and before run_simple)
56
- ScoutPython.process_paths
57
- ```
58
-
59
- process_paths executes in a Python context:
60
- ```ruby
61
- ScoutPython.run_direct 'sys' do
62
- ScoutPython.paths.each { |p| sys.path.append p }
63
- end
64
- ```
65
-
66
- ---
67
-
68
- ## Running Python code from Ruby
69
-
70
- ScoutPython wraps PyCall to simplify imports and execution. The central methods accept optional import directives and a block of Python-interacting Ruby.
71
-
72
- Imports argument forms:
73
- - String/Symbol module only: pyimport "numpy"
74
- - Array of names to import from module: pyfrom "module.submodule", import: [:Class, :func]
75
- - Hash passed to pyimport (e.g., aliases): pyimport "numpy", as: :np
76
-
77
- Methods:
78
-
79
- - run(mod = nil, imports = nil) { ... }
80
- - Initialize PyCall once (init_scout), ensure paths, run block. Garbage collects after run.
81
- - run_simple(mod = nil, imports = nil) { ... }
82
- - Same as run but without init_scout and extra GC. Synchronizes and calls process_paths.
83
- - run_direct(mod = nil, imports = nil) { ... }
84
- - Run without synchronization; perform a single pyimport/pyfrom (if provided) and module_eval the block.
85
- - run_threaded(mod = nil, imports = nil) { ... }
86
- - Execute in a dedicated background thread (see Threading below). Optional import executed in thread context.
87
- - run_log(mod = nil, imports = nil, severity = 0, severity_err = nil) { ... }
88
- - Wrap the execution with Log.trap_std to capture/route Python stdout/stderr.
89
- - run_log_stderr(mod = nil, imports = nil, severity = 0) { ... }
90
- - Capture only Python stderr via Log.trap_stderr.
91
- - stop_thread
92
- - Stop the background Python thread, join/kill, GC, and PyCall.finalize (if available).
93
-
94
- Examples:
95
- ```ruby
96
- # Simple import and call
97
- ScoutPython.run 'numpy', as: :np do
98
- a = np.array([1,2,3])
99
- a.sum # => Py object; can call PyCall on it
100
- end
101
-
102
- # From submodule and alias
103
- ScoutPython.run "tensorflow.keras.models", import: :Sequential do
104
- defined?(self::Sequential) # => true in this scope
105
- end
106
-
107
- # Threaded execution
108
- arr = ScoutPython.run_threaded :numpy, as: :np do
109
- np.array([1,2])
110
- end
111
- ScoutPython.stop_thread
112
- ```
113
-
114
- ---
115
-
116
- ## Binding scopes and imports
117
-
118
- Use a dedicated Binding (includes PyCall::Import) to localize imported names so they don’t leak into other scopes.
119
-
120
- - new_binding → returns a Binding instance with PyCall::Import mixed in.
121
- - binding_run(binding = nil, *args) { ... }
122
- - Create a new Binding, instance_exec the block, then discard.
123
-
124
- Example (from tests):
125
- ```ruby
126
- raised = false
127
- ScoutPython.binding_run do
128
- pyimport :torch
129
- pyfrom :torch, import: ["nn"]
130
- begin
131
- torch
132
- rescue
133
- raised = true
134
- end
135
- end
136
- raised # => false (torch available inside)
137
-
138
- raised = false
139
- ScoutPython.binding_run do
140
- begin
141
- torch # not defined in this fresh binding
142
- rescue
143
- raised = true
144
- end
145
- end
146
- raised # => true
147
- ```
148
-
149
- Import helpers:
150
- - import_method(module_name, method_name, as=nil) → Method
151
- - call_method(module_name, method_name, *args)
152
- - get_module(module_name) → imported module object (aliased safely as name with dots replaced by underscores)
153
- - get_class(module_name, class_name)
154
- - class_new_obj(module_name, class_name, args={}) → instantiate class with keyword args
155
- - exec(script) → PyCall.exec(script) one-liner
156
-
157
- ---
158
-
159
- ## Scripting: run ad‑hoc Python text
160
-
161
- script(text, variables = {}) runs a Python script in a subprocess and returns the value of the Python variable result. It:
162
-
163
- - Serializes provided variables into Python assignments at the top of the script:
164
- - Ruby primitives (String, Numeric, true/false/nil) mapped to Python literals.
165
- - Arrays and Hashes transformed recursively.
166
- - TSV values are written to a temporary file and loaded using the companion Python function scout.tsv(file), yielding a pandas DataFrame.
167
- - Appends a “save result” snippet to persist the result to a temporary file (default: Pickle).
168
- - Executes the script with python3 via CMD.cmd_log, wiring PYTHONPATH from ScoutPython.paths.
169
- - Loads the result back (default: pickled result via python/pickle gem) and returns it.
170
-
171
- By default, pickle is used:
172
- - save_script_result_pickle(file) → Python code to pickle.dump(result, file)
173
- - load_pickle(file) → Ruby loads via Python::Pickle
174
-
175
- Alternatively, you can alias to JSON-based persistence:
176
- - save_script_result_json(file)
177
- - load_json(file)
178
- - Overwrite aliases if desired:
179
- ```ruby
180
- class << ScoutPython
181
- alias save_script_result save_script_result_json
182
- alias load_result load_json
183
- end
184
- ```
185
-
186
- Examples (from tests):
187
- ```ruby
188
- # Simple arithmetic
189
- res = ScoutPython.script <<~PY, value: 2
190
- result = value * 3
191
- PY
192
- res # => 6
193
-
194
- # Using pandas via the 'scout' Python helper
195
- tsv = TSV.setup({}, "Key~ValueA,ValueB#:type=:list")
196
- tsv["k1"] = %w[a1 b1]; tsv["k2"] = %w[a2 b2]
197
-
198
- TmpFile.with_file(tsv.to_s) do |tsv_file|
199
- TmpFile.with_file do |target|
200
- res = ScoutPython.script <<~PY, file: tsv_file, target: target
201
- import scout
202
- df = scout.tsv(file)
203
- result = df.loc["k2", "ValueB"]
204
- scout.save_tsv(target, df)
205
- PY
206
- res # => "b2"
207
- TSV.open(target, type: :list)["k2"]["ValueB"] # => "b2"
208
- end
209
- end
210
-
211
- # Pass a TSV directly; script receives a pandas DataFrame
212
- res = ScoutPython.script <<~PY, df: tsv, target: target
213
- result = df.loc["k2", "ValueB"]
214
- scout.save_tsv(target, df)
215
- PY
216
- ```
217
-
218
- Errors:
219
- - If the Python subprocess fails (syntax error, exception), script raises ConcurrentStreamProcessFailed (via CMD and ConcurrentStream), with stderr logged if logging enabled.
220
-
221
- ---
222
-
223
- ## Iteration utilities
224
-
225
- Helpers to traverse Python iterables from Ruby, with optional progress bars:
226
-
227
- - iterate(iterator, bar: nil|true|String) { |elem| ... } → nil
228
- - Accepts PyCall iterable objects (with __iter__/__next__) or indexable sequences.
229
- - bar: true creates a default bar; String sets desc.
230
- - iterate_index(sequence, bar: ...) { |elem| ... } → nil
231
- - Index-based loop using len and [i].
232
- - collect(iterator, bar: ...) { |elem| block.call(elem) } → Array
233
- - Convenience to map Python iterables to Ruby arrays.
234
-
235
- StopIteration is respected and terminates traversal. Errors mark the bar as error and re-raise.
236
-
237
- ---
238
-
239
- ## Data conversion and pandas helpers
240
-
241
- Low-level converters:
242
- - py2ruby_a(listlike) / to_a → convert PyCall::List-like into a Ruby Array.
243
- - list2ruby(list) → deep-convert nested Py lists to Ruby arrays.
244
- - numpy2ruby(numpy_array) → numpy.ndarray.tolist to Ruby arrays.
245
- - obj2hash(py_mapping) → build a Ruby Hash by iterating keys and indexing values.
246
-
247
- pandas ↔ TSV:
248
- - tsv2df(tsv) → pandas.DataFrame with:
249
- - index: tsv.keys, columns: tsv.fields, df.columns.name = tsv.key_field.
250
- - df2tsv(df, options = {})
251
- - Default options[:type] = :list
252
- - Builds a TSV with key_field = df.columns.name (or provided), fields from df.columns, and values from df rows.
253
-
254
- Example (from tests):
255
- ```ruby
256
- tsv = TSV.setup([], key_field: "Key", fields: %w(Value1 Value2), type: :list)
257
- tsv["k1"] = %w(V1_1 V2_1)
258
- tsv["k2"] = %w(V1_2 V2_2)
259
-
260
- df = ScoutPython.tsv2df(tsv)
261
- new_tsv = ScoutPython.df2tsv(df)
262
- new_tsv == tsv # => true
263
- ```
264
-
265
- ---
266
-
267
- ## Python-side helper package (scout)
268
-
269
- The repository also ships a small Python package named scout (python/scout), intended to be importable from Python code run by ScoutPython or independently.
270
-
271
- Top-level utilities (scout/__init__.py):
272
- - cmd(ruby_string=nil) → execute Ruby via rbbt_exec.rb; returns stdout.
273
- - libdir() → resolve Ruby lib directory via cmd.
274
- - add_libdir() → prepend libdir/python to sys.path.
275
- - path(subdir=None, base_dir=None) → convenience location helper ("base" uses ~/.rbbt, "lib" uses libdir).
276
- - read(subdir, base_dir=None) → read a file via path.
277
- - inspect(obj) / rich(obj) → inspection helpers (rich requires rich).
278
- - tsv_header/tsv_preamble/tsv_pandas(tsv_path, ...) → read TSV respecting Scout headers (:sep, :type, headers).
279
- - tsv(*args, **kwargs) → alias to tsv_pandas.
280
- - save_tsv(filename, df, key=None) → write pandas DataFrame to TSV with header and key (index_label).
281
- - save_job_inputs(data: dict) → materialize Python values into files for Workflow inputs.
282
- - run_job(workflow, task, name='Default', fork=False, clean=False, **kwargs) → shell out to CLI (`rbbt workflow task`) to execute or fork a job and return path or value.
283
-
284
- Notes:
285
- - The CLI used in run_job is named rbbt in this package; in Scout-based installs, a compatible executable should be available (often named scout). Setup a symlink or adjust if needed.
286
-
287
- ### Workflow wrapper (local)
288
-
289
- Pythonic interface to run workflows and get results (python/scout/workflow.py):
290
-
291
- - Workflow(name)
292
- - tasks() → list of task names.
293
- - task_info(name) → JSON string from Workflow.task_info.
294
- - run(task, **kwargs) → execute and return stdout (uses run_job).
295
- - fork(task, **kwargs) → submit with fork=True, return a Step(path).
296
-
297
- - Step(path)
298
- - info() → job info (JSON parsed via Ruby Step.load(path).info).
299
- - status(), done(), error(), aborted()
300
- - join() → poll until completion.
301
- - load() → load job result via Ruby Step.load(path).load.
302
-
303
- Example (python/test.py):
304
- ```python
305
- if __name__ == "__main__":
306
- import sys
307
- sys.path.append('python')
308
- import scout
309
- import scout.workflow
310
- wf = scout.workflow.Workflow('Baking')
311
- step = wf.fork('bake_muffin_tray', add_blueberries=True, clean='recursive')
312
- step.join()
313
- print(step.load())
314
- ```
315
-
316
- ### Remote workflows over HTTP
317
-
318
- A minimal client for remote Workflow services (python/scout/workflow/remote.py):
319
-
320
- - RemoteWorkflow(url)
321
- - init_remote_tasks() → populate available tasks.
322
- - task_info(name) → JSON
323
- - job(task, **kwargs) → start a job, returns RemoteStep.
324
-
325
- - RemoteStep(url)
326
- - info(), status()
327
- - done(), error(), running()
328
- - wait(time=1)
329
- - raw() → GET bytes of result
330
- - json() → GET JSON result
331
-
332
- Requests are made via requests; results are obtained by GET/POST using a _format parameter (raw/json).
333
-
334
- ---
335
-
336
- ## Error handling, logging, and threading
337
-
338
- - Logging:
339
- - run_log and run_log_stderr wrap execution in Log.trap_std/stderr; stdout/stderr lines are routed to the Scout Log with chosen severity.
340
- - script:
341
- - Uses CMD.cmd_log to launch python3; stderr is logged; non-zero exit raises ConcurrentStreamProcessFailed on join (see CMD and ConcurrentStream docs).
342
- - Threaded execution:
343
- - A dedicated thread processes queued blocks (Queue IN/OUT).
344
- - stop_thread sends a :stop sentinel, joins or kills the thread, triggers GC, and calls PyCall.finalize if available.
345
- - At exit, ScoutPython attempts to stop non-main threads, run GC while Python is still initialized, and touch PyCall.builtins.object to validate GIL access.
346
-
347
- ---
348
-
349
- ## API quick reference
350
-
351
- Path management:
352
- - ScoutPython.paths → Array of Python paths
353
- - ScoutPython.add_path(path)
354
- - ScoutPython.add_paths(paths)
355
- - ScoutPython.process_paths
356
-
357
- Execution:
358
- - ScoutPython.run(mod=nil, imports=nil) { ... }
359
- - ScoutPython.run_simple(mod=nil, imports=nil) { ... }
360
- - ScoutPython.run_direct(mod=nil, imports=nil) { ... }
361
- - ScoutPython.run_threaded(mod=nil, imports=nil) { ... }
362
- - ScoutPython.stop_thread
363
- - ScoutPython.run_log(mod=nil, imports=nil, severity=0, severity_err=nil) { ... }
364
- - ScoutPython.run_log_stderr(mod=nil, imports=nil, severity=0) { ... }
365
-
366
- Binding/import helpers:
367
- - ScoutPython.new_binding
368
- - ScoutPython.binding_run(binding=nil, *args) { ... }
369
- - ScoutPython.import_method(module_name, method_name, as=nil)
370
- - ScoutPython.call_method(module_name, method_name, *args)
371
- - ScoutPython.get_module(module_name)
372
- - ScoutPython.get_class(module_name, class_name)
373
- - ScoutPython.class_new_obj(module_name, class_name, args={})
374
- - ScoutPython.exec(script)
375
-
376
- Iteration:
377
- - ScoutPython.iterate(iterator, bar: nil|true|String) { |elem| ... }
378
- - ScoutPython.iterate_index(sequence, bar: ...) { |elem| ... }
379
- - ScoutPython.collect(iterator, bar: ...) { |elem| ... } → Array
380
-
381
- Scripting:
382
- - ScoutPython.script(text, variables={}) → result
383
- - ScoutPython.save_script_result_pickle(file) / load_pickle(file)
384
- - ScoutPython.save_script_result_json(file) / load_json(file)
385
- - Aliases (defaults): save_script_result → pickle; load_result → load_pickle
386
-
387
- Data conversion:
388
- - ScoutPython.py2ruby_a(obj) / ScoutPython.to_a(obj)
389
- - ScoutPython.list2ruby(list)
390
- - ScoutPython.numpy2ruby(numpy_array)
391
- - ScoutPython.obj2hash(py_mapping)
392
- - ScoutPython.tsv2df(tsv) → pandas.DataFrame
393
- - ScoutPython.df2tsv(df, options={}) → TSV
394
-
395
- Python package (scout):
396
- - scout.tsv(path, ...) → pandas DataFrame (header-aware)
397
- - scout.save_tsv(path, df, key=None)
398
- - scout.run_job(workflow, task, name='Default', fork=False, clean=False, **inputs) → job path or stdout
399
- - scout.workflow.Workflow(name).run/fork/tasks/task_info
400
- - scout.workflow.Step(path).info/status/join/load
401
- - scout.workflow.remote.RemoteWorkflow/RemoteStep for HTTP services
402
-
403
- ---
404
-
405
- ## Examples
406
-
407
- Direct PyCall use with imports:
408
- ```ruby
409
- # Print sys.path in a background Python thread
410
- ScoutPython.run_threaded :sys do
411
- paths = sys.path()
412
- puts paths
413
- end
414
- ScoutPython.stop_thread
415
- ```
416
-
417
- Script with result:
418
- ```ruby
419
- res = ScoutPython.script <<~PY, value: 2
420
- result = value * 3
421
- PY
422
- # => 6
423
- ```
424
-
425
- Script with TSV and pandas:
426
- ```ruby
427
- tsv = TSV.setup({}, "Key~ValueA,ValueB#:type=:list")
428
- tsv["k1"] = ["a1", "b1"]; tsv["k2"] = ["a2", "b2"]
429
-
430
- TmpFile.with_file do |target|
431
- res = ScoutPython.script <<~PY, df: tsv, target: target
432
- result = df.loc["k2", "ValueB"]
433
- scout.save_tsv(target, df)
434
- PY
435
- res # => "b2"
436
- TSV.open(target, type: :list)["k2"]["ValueB"] # => "b2"
437
- end
438
- ```
439
-
440
- Numpy conversion:
441
- ```ruby
442
- ra = ScoutPython.run :numpy, as: :np do
443
- na = np.array([[[1,2,3], [4,5,6]]])
444
- ScoutPython.numpy2ruby(na)
445
- end
446
- ra[0][1][2] # => 6
447
- ```
448
-
449
- pandas ↔ TSV:
450
- ```ruby
451
- tsv = TSV.setup([], key_field: "Key", fields: %w(Value1 Value2), type: :list)
452
- tsv["k1"] = %w(V1_1 V2_1)
453
- tsv["k2"] = %w(V1_2 V2_2)
454
-
455
- df = ScoutPython.tsv2df(tsv)
456
- new_tsv = ScoutPython.df2tsv(df)
457
- new_tsv == tsv # => true
458
- ```
459
-
460
- Binding-local imports:
461
- ```ruby
462
- ScoutPython.binding_run do
463
- pyimport :torch
464
- # torch is available here
465
- end
466
- # torch is not defined here
467
- ```
468
-
469
- Python-side Workflow usage:
470
- ```python
471
- import scout.workflow as sw
472
-
473
- wf = sw.Workflow('Baking')
474
- print(wf.tasks())
475
- step = wf.fork('bake_muffin_tray', add_blueberries=True, clean='recursive')
476
- step.join()
477
- print(step.load())
478
- ```
479
-
480
- ---
481
-
482
- ScoutPython gives you a compact, production-friendly toolbox to interoperate with Python: safe imports, threaded or synchronous execution, TSV/pandas integration, and Workflow orchestration from both Ruby and Python. Use run/script for quick integrations, the conversion helpers to pass data efficiently, and the Python scout package to drive Scout Workflows from Python environments.
@@ -1,163 +0,0 @@
1
- PythonWorkflow lets you define Ruby Scout workflows whose tasks are implemented as standalone Python functions.
2
-
3
- This module is meant for workflow authors who prefer to write task logic in Python, while still benefiting from Scout/Rbbt features such as dependency management, persistence, provenance, and CLI integration.
4
-
5
- A Python-backed task is defined in a `.py` script (typically under a `python/task` directory). The script registers one or more functions using `scout.task(...)`. The Ruby workflow then calls `python_task` to import those function definitions as regular Scout tasks.
6
-
7
- Key ideas
8
-
9
- - Python tasks are ordinary Python functions with type hints, defaults, and a docstring.
10
- - The Python script can be run on its own:
11
- - `--scout-metadata` prints machine-readable JSON task metadata (consumed by Ruby to define inputs and return types).
12
- - without `--scout-metadata` it behaves like a CLI that runs the function.
13
- - Ruby side (`PythonWorkflow`) reads metadata and auto-creates Workflow inputs and tasks.
14
- - At execution time, the Ruby task block runs the Python script in a subprocess using `ScoutPython.run_file`.
15
-
16
- Minimal directory layout
17
-
18
- - `workflow.rb` defines the Ruby workflow.
19
- - `python/task/<name>.py` defines one or more Python functions and registers them.
20
-
21
- Example Python task
22
-
23
- ```python
24
- import scout
25
-
26
- def hello(name: str, excited: bool = False) -> str:
27
- """
28
- Generate a greeting.
29
-
30
- Args:
31
- name: Name of the person to greet.
32
- excited: Whether to add an exclamation mark.
33
-
34
- Returns:
35
- Greeting text.
36
- """
37
- return f"Hello, {name}{'!' if excited else ''}"
38
-
39
- scout.task(hello)
40
- ```
41
-
42
- Example Ruby workflow
43
-
44
- ```ruby
45
- require 'scout'
46
-
47
- module TestPythonWF
48
- extend Workflow
49
- extend PythonWorkflow
50
-
51
- self.name = 'TestPythonWF'
52
-
53
- python_task :hello
54
- end
55
- ```
56
-
57
- Type mapping (Python metadata to Scout inputs/returns)
58
-
59
- PythonWorkflow relies on `scout.task` metadata type strings (produced by `python/scout/runner.py`) and maps them to standard Workflow types.
60
-
61
- - Scalars
62
- - `string` -> `:string`
63
- - `integer` -> `:integer`
64
- - `float` -> `:float`
65
- - `boolean` -> `:boolean`
66
- - `binary` -> `:binary`
67
- - `path` -> `:file` for inputs (passed as a path string on the CLI)
68
- - Lists
69
- - `list[string]`, `list[integer]`, `list[float]` -> `:array`
70
- - `list[path]` -> `:file_array`
71
-
72
- List inputs in Ruby
73
-
74
- When building the Python command line, list parameters accept several Ruby-side formats:
75
-
76
- - A Ruby Array (`['a', 'b', 'c']`)
77
- - A comma-separated String (`"a,b,c"`)
78
- - A path to an existing file (the file is read line-by-line and passed as items)
79
-
80
- Return value decoding
81
-
82
- The Python runner prints function results to stdout, and Ruby tries to interpret them as follows:
83
-
84
- - If stdout is valid JSON, it is parsed with `JSON.parse` and returned.
85
- - Otherwise, if the declared Scout return type is `:array` or `:file_array`, stdout is split on newlines.
86
- - Otherwise, stdout is returned as a stripped string.
87
-
88
- This means you can return complex objects from Python, as long as the runner prints JSON and your declared return type can sensibly persist that Ruby value.
89
-
90
- Python CLI behavior (standalone execution)
91
-
92
- A Python task file registered via `scout.task(...)` can be used directly as a command-line tool.
93
-
94
- - Metadata:
95
- - `python hello.py --scout-metadata`
96
- - For files that register multiple functions, `--scout-metadata` prints a JSON array of metadata objects.
97
- - Run:
98
- - `python hello.py --name Alice --excited`
99
- - If multiple functions are registered in the same file, you can select one by passing its name as the first positional argument:
100
- - `python tasks.py hello --name Alice`
101
-
102
- Python import paths
103
-
104
- Python tasks are executed as subprocesses with a `PYTHONPATH` composed from `ScoutPython.paths`. These are initialized from `Scout.python.find_all` and can be extended at runtime using `ScoutPython.add_path` or `ScoutPython.add_paths`.
105
-
106
- # Tasks
107
-
108
- ## python_task
109
- Register one or more Python functions as Workflow tasks.
110
-
111
- `python_task` discovers and reads metadata from a Python script (by running it with `--scout-metadata`) and then defines one Workflow task per function found.
112
-
113
- Inputs and return type are inferred from the Python function signature and type hints.
114
-
115
- If the Python script registers multiple functions, multiple Workflow tasks are created (one per registered function). The `task_sym` argument selects the default filename to locate, but does not limit how many functions will be imported from that file.
116
-
117
- The task execution runs the Python script as a subprocess and passes CLI options that correspond to the declared inputs.
118
-
119
- ## python_task_dir
120
- Configure where Python task scripts are discovered.
121
-
122
- By default, `python_task_dir` is taken from `Scout.python.task.find(:lib)`, so tasks can be shipped as part of a Scout package and located via the Path subsystem.
123
-
124
- You can override it by setting `self.python_task_dir` in your workflow module to a different Path or directory-like object that supports `[]` indexing and `find_with_extension('py')`.
125
-
126
- ## scout.task
127
- Register a Python function as a Scout-compatible task and enable metadata/CLI execution.
128
-
129
- A Python task script should end with one or more `scout.task(function)` calls. This:
130
-
131
- - Captures signature, type hints, and docstring to build a metadata object.
132
- - Parses per-argument documentation primarily from Google-style `Args:` sections.
133
- - Enables `--scout-metadata` output for Ruby to consume.
134
- - Enables standalone CLI execution using argparse, including support for list and boolean arguments.
135
-
136
- For scripts that register multiple functions, `scout.task` defers CLI dispatch until interpreter shutdown so all functions are registered before selecting a target function.
137
-
138
- Docstring format for parameter descriptions
139
-
140
- To maximize interoperability with agent/tool frameworks and to provide good CLI help text, write docstrings in Google style.
141
-
142
- Recommended pattern:
143
-
144
- ```python
145
- def my_task(query: str, max_results: int = 10) -> str:
146
- """
147
- Search items.
148
-
149
- Args:
150
- query: Natural language query describing what to search.
151
- max_results: Maximum number of results to return.
152
-
153
- Returns:
154
- A newline-delimited or JSON-encoded result.
155
- """
156
- ```
157
-
158
- Notes:
159
-
160
- - The task description is taken from the docstring preamble: everything up to the first `Args:`/`Arguments:` or `Returns:` section.
161
- - The `Args:` section is used to populate each parameter `help` field in `--scout-metadata`.
162
- - Multi-line argument descriptions are supported as long as continuation lines stay indented.
163
- - A NumPy-style `Parameters` section is still accepted as a legacy fallback, but `Args:` is preferred.