Closed markcampanelli closed 4 years ago
I took a quick look at this, and adding a context manager is straightforward. Adding a config is also easy, but I would want it to take a config string with a mode rather than detailed parameter setttings. The parameters can vary from device to device (they don't currently), so the driver should abstract the details, at least at this level. How about:
:param config: The initial default configuration following device open.
Choices are ['auto', 'off', 'ignore', None].
* 'auto': enable the sensor and start collecting data with
current sensor autoranging.
* 'ignore' or None: Leave the device in its existing state.
* 'off': Turn the sensor off and disable data collection.
Then the use case becomes:
import joulescope
with joulescope.scan_require_one(config='auto') as js:
data = js.read(contiguous_duration=0.25)
I went ahead an implemented this in commit fa8f3ded. Let me know if you have feedback!
Available in 0.5.0.
Consider using a Pythonic context manager for scan operations.
Using the context manager pattern,
becomes the more streamlined
where the context manager handles opening and closing the device, doing so even when an exception is raised.
Optionally, one could also pass a configuration
dict
tojoulescope.scan_require_one()
, such as