-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supporting with-statements #128
Comments
It seems like this is currently implemented: Lines 1409 to 1432 in ac2671e
This should support something like: with spidev.SpiDev() as spi:
... However, with my level of understanding Python C modules, it seems like you would still need to call the How I think it would currently work: with spidev.SpiDev() as spi:
spi.open(0, 0)
spi.max_speed_hz = 4_000_000
spi.writebyte([0x00]) I am currently also not able to test this. How I wish it would work: with spidev.SpiDev(bus=0, device=0, max_speed_hz=4_000_000) as spi:
spi.writebyte([0x00]) Or maybe even better, keeping the SPI object initialized but not opened: spi_device = spidev.SpiDev(bus=0, device=0, max_speed_hz=4_000_000)
with spi_device.open() as conn:
conn.writebyte([0x00])
# do some other stuff here...
with spi_device.open() as conn:
conn.writebyte([0xFF]) @doceme In general, would it be an idea to document the interface for this package? Maybe autogenerate API documentation? I also feel the current readme is not sufficient, as it for example lacks documentation about this feature. If you would be interested, I could investigate the possibilities. |
As the library already has a class, an
open()
method and aclose()
method, it would make sense to add the small steps needed to make it callable in awith
statement. However, as it's a c-Extension I currently don't feel confident that I could write the code alone.In Python I would add this:
When the
with
statement is entered, the connection is opened. When it is left, the connection gets closed. Even in case of an exception. And just in case, the connection gets closed on object deletion as well.More explanation regarding the value of supporting
with
.Is there someone who has experience in transferring that idea to the C codebase here?
PS: Locally, I might simply add a wrapper class that gets the new methods added.
The text was updated successfully, but these errors were encountered: