Skip to content

docs(examples): use the instrument context manager in examples#95

Merged
hoehner merged 1 commit into
mainfrom
issue-51-examples-context-manager
Jun 12, 2026
Merged

docs(examples): use the instrument context manager in examples#95
hoehner merged 1 commit into
mainfrom
issue-51-examples-context-manager

Conversation

@nhschwab

@nhschwab nhschwab commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Closes #51.

What

Migrate the example scripts from manual open() + try/finally/close() to the with form. Every category inherits the context-manager protocol from Instrument (open on entry, close on exit including on exception), so the with block is shorter, exception-safe, and showcases the idiomatic API.

The mirrored example pages under docs/guides/instrumentation/examples/ are auto-generated from the scripts (just gen-examples); regenerated in the same commit.

Migrated (24 scripts)

All examples/daq/*.py, dmm/dmm_basic.py, eload/eload.py, i2c/i2c_basic.py, i2c/i2c_command.py, modbus/minimal_example.py, modbus/labjack_t4_loopback_test.py, psu/psu.py, psu/psu_background_daemon.py, all publishers/*.py, test_rack_example/test_rack.py.

Scope notes (reconciled against recent commits)

  • psu_background_worker.py (named in the issue) is now psu_background_daemon.py — migrated.
  • examples/custom/simple_temp_controller.py already uses with — no change.
  • examples/modbus/modbus_example.py and programmatic_config_example.py use ModbusDevice(..., autostart=True), which opens + starts polling in __init__. They never use the manual pattern, so wrapping them in with would re-open an already-open device. Out of scope, left as-is.
  • Prose HAL guides (psu.mdx, daq.mdx, etc.) were intentionally not mass-converted: quickstart.mdx deliberately documents both explicit open()/close() and the with block as first-class, and many .open()/.close() occurrences there are transport-level (visa.open()) or driver method definitions, not instrument-lifecycle usage.

Behavior preserved where finally did more than close

  • test_rack.py: the safe-state shutdown (disable outputs + settle) is kept in an inner try/finally inside the with block, so it still runs before the instruments close.
  • daq_sine_wave_dac0_t4.py: the KeyboardInterrupt handler that parks the DAC at 0 V is kept as an inner try/except inside the with block.

Verification

just check passes (ruff format, mypy, ruff lint all clean); all example scripts byte-compile.

Migrate the example scripts from manual open() + try/finally/close() to
the `with` form. Every category inherits the context-manager protocol from
Instrument (open on entry, close on exit including on exception), so the
`with` block is shorter, exception-safe, and showcases the idiomatic API.

Regenerate the mirrored example pages under
docs/guides/instrumentation/examples/ via `just gen-examples`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@mintlify

mintlify Bot commented Jun 12, 2026

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
instro 🟢 Ready View Preview Jun 12, 2026, 12:47 AM

💡 Tip: Enable Workflows to automatically generate PRs for you.

@hoehner hoehner merged commit 15c6d9a into main Jun 12, 2026
11 checks passed
@hoehner hoehner deleted the issue-51-examples-context-manager branch June 12, 2026 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs(examples): use the instrument context manager in examples

2 participants