docs(examples): use the instrument context manager in examples#95
Merged
Conversation
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>
|
Preview deployment for your docs. Learn more about Mintlify Previews.
💡 Tip: Enable Workflows to automatically generate PRs for you. |
hoehner
approved these changes
Jun 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #51.
What
Migrate the example scripts from manual
open()+try/finally/close()to thewithform. Every category inherits the context-manager protocol fromInstrument(open on entry, close on exit including on exception), so thewithblock 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, allpublishers/*.py,test_rack_example/test_rack.py.Scope notes (reconciled against recent commits)
psu_background_worker.py(named in the issue) is nowpsu_background_daemon.py— migrated.examples/custom/simple_temp_controller.pyalready useswith— no change.examples/modbus/modbus_example.pyandprogrammatic_config_example.pyuseModbusDevice(..., autostart=True), which opens + starts polling in__init__. They never use the manual pattern, so wrapping them inwithwould re-open an already-open device. Out of scope, left as-is.psu.mdx,daq.mdx, etc.) were intentionally not mass-converted:quickstart.mdxdeliberately documents both explicitopen()/close()and thewithblock 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
finallydid more than closetest_rack.py: the safe-state shutdown (disable outputs + settle) is kept in an innertry/finallyinside thewithblock, so it still runs before the instruments close.daq_sine_wave_dac0_t4.py: theKeyboardInterrupthandler that parks the DAC at 0 V is kept as an innertry/exceptinside thewithblock.Verification
just checkpasses (ruff format, mypy, ruff lint all clean); all example scripts byte-compile.