Modify GPIB code to use symbolic constants#601
Conversation
Create a file of symbolic constants (IntEnum classes) for the various libgpib interface calls. Use these symbols rather than hard-coded integers. Remove now-extraneous commentary about what the hard-coded integers mean.
for more information, see https://pre-commit.ci
…nto gpib-enums-clean
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #601 +/- ##
==========================================
+ Coverage 39.18% 41.03% +1.84%
==========================================
Files 27 28 +1
Lines 5022 5176 +154
Branches 518 517 -1
==========================================
+ Hits 1968 2124 +156
+ Misses 3028 3026 -2
Partials 26 26
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| if ifc.ask(2): | ||
| ifc.config(2, attribute_state + 96) | ||
| if ifc.ask(gpib_constants.ask.IbaSAD): | ||
| ifc.config(gpib_constants.config.IbcSAD, attribute_state + 96) |
There was a problem hiding this comment.
I wish my comment about the 96 was more explicit...
There was a problem hiding this comment.
Ach. I didn't even consider those NI-specific SAD masks/constants.
I'll add another commit to the PR for these. Shouldn't take long.
There was a problem hiding this comment.
Things being as they are, these days, I'm not absolutely certain that saying "96 86'ed" is entirely safe, but what the hell. Done.
There was a problem hiding this comment.
Hah! Just realized there was an off-by-one error in the old code - range(96,126) should have been range(96,127). As it was, subaddress 30 wouldn't have been iterated. Fixed now.
Turn 0, 96, and 126 into symbolic constants for the SAD.
for more information, see https://pre-commit.ci
The range() operator which allows iteration through the allowed GPIB sub-addresses (0 through 30, mapped up to 96 through 126) needs to have its stop value set to 127 (LAST_SAD + 1) rather than 126 (LAST_SAD). Otherwise it will look for subaddresses 0 through 29, and not see a device at subaddress 30.
for more information, see https://pre-commit.ci
MatthieuDartiailh
left a comment
There was a problem hiding this comment.
LGTM (minus the changelog formatting)
Co-authored-by: Matthieu Dartiailh <marul@laposte.net>
|
Can you address the linting/typin failure ? (mostly a bad import I think) |
Oops... missed one set of cases where I had initially used GPIBwhatever and now (with imported symbols) need to use gpib_constants.whatever Also, sort the gpib_constants import statement
for more information, see https://pre-commit.ci
Another one I missed: GPIBconfig -> gpib_constants.config
Apparently, in this case, an IntEnum is not sufficiently like an int to be type-matched.
for more information, see https://pre-commit.ci
…nto gpib-enums-clean
|
OK, this looks better. I fixed the two enum-name-change sets (GPIBconfig and GPIBerror) I missed during my original restructuring. I've moved the import into the proper sorted position. There's one other complaint about an old import, but I don't know if I should touch it now (I didn't, before). I fixed the type mismatch involving the SAD, although it took a couple of tries for me to realize what was actually going on. Apparently IntEnums don't always behave precisely like integers, from a type-checking perspective. Please take another look. |
|
Looks good. One more import sorting you can use ruff to fix. |
Run ruff on gpib.py, merging two import statements into one.
|
Done. |
Create a file of symbolic constants (IntEnum classes) for the various libgpib interface calls. Use these symbols rather than hard-coded integers. Remove
now-extraneous commentary about what the hard-coded integers mean.
The new file gpib_constants.py is derived from the constants file in gpib_ctypes (which isn't a strict dependency of this project). I've reorganized the values as an individual IntEnum class per API or purpose, with the class name based on the API or purpose, and the member names matching those used in gpib_ctypes. This provides a clean visual match between the ib*() call, class name, and member name each time one of the symbols is used, and should help prevent usage mismatches. In the gpib.py code I modified, I have removed the old commentary which explained the intent of each bare integer as being unnecessary.
This change deserves careful eyeball checking before being merged, to make sure I've gotten the translations between bare-integer and symbol correct in every case. I've reviewed it all two or three times, but you know how much author code reviews are worth! :-)
black . && isort -c . && flake8with no errors