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
Harmonise instrument name definition pattern, consistently name the instrument connection argument "adapter" #659
Conversation
I'm actually not sure if we should always put the default name into the constructor signature, as it pollutes that (and displayed docstrings, presumably) with a pretty long entry that is not that central. def test_name_setting():
class NamedInstrument(Instrument):
def __init__(self, adapter, **kwargs):
kwargs.setdefault('name', 'Default Instrument')
super().__init__(adapter, **kwargs)
adapter = FakeAdapter()
instr = NamedInstrument(adapter)
assert instr.name == 'Default Instrument'
# Let's use a user-specific name (e.g. to disambiguate multiple identical devices)
instr2 = NamedInstrument(adapter, name='Vacuum Frobnicator')
assert instr2.name == 'Vacuum Frobnicator' This way the signature of |
Normally I prefer to know, what values I might change and what the default is. setdefault hides the possibility to rename the device. One caveat though: The documentation does not hand the kwargs to the instrument, but to the adapter in case of a custom adapter! Once I'm remodeling all instruments. Should we name the adapter consistently adapter? In some instruments it is called "resourceName", which is taken from the documentation. |
I'm not sure what you mean?
I think this comes from pyvisa nomenclature, where it's "resource name". I'm not sure. I guess in 95% of cases people supply a resource name string, so that would be a more fitting name. However, if people pass an
|
I agree, in my opinion |
I made the talked about changes (to adapter and using kwargs). There are some doctest errors in the documentation though. |
Please also add that test to test_instrument.py, it's good to have tests that define best practices, so they constantly get checked/confirmed. |
I don't know what's up with the doctest, first I thought that it's because we swap in FakeInstrument in adding_instruments.rst L89 (to enable doctesting), but I can't seem to reproduce this locally, and the error message does not make sense to me. Let's see if it's a transient or appears again. |
Thanks for your reviews. I implemented the changes. I also implemented a test whether a named argument removes the corresponding entry from kwargs. |
I think we're good now! Only the doctests are still broken (and a small linting issue remains). 😿 I'll pull and see if I can figure it out locally. Afterwards, we should check/update all open PRs for the new usage ( |
Testing with something like
This test verifies, whether the first argname is adapter, because if it is not adapter, it won't receive a value and will raise an error. |
Sorry, I was unclear. Yeah such a test tests what we need, but it does not do that (dynamically) for any instruments changed in a PR. Adding this same test to all instruments individually seems wasteful. I think we could enumerate all instruments, and feed those to that test, though! (or one test for the adapter, one for the name) :-) |
I wrote a test, which searches for all instruments in pymeasure and does this test. |
I'd say push the test, if there are obvious fixes make them, if we need to add something to FakeAdapter (if only that some instrument has something to call) we can do that. |
In general, that list of all instruments (module-level |
Awesome addition with that test, we can work with that! |
(This comment is related to |
For a user yes, |
One use case for users giving custom names is when you have an experiment with several identical devices, e.g. several power supplies controlling coil current, bias voltage, excitation voltage (random example), then you would name the instances as such so that it's clearer/more convenient to see further down (e.g. in logging, GUI use, etc). The other use case is when subclassing, as already mentioned. |
With that, I think, we're good to go. |
Me too. |
In order to ensure more easy subclassing, I'm for enabling all instruments to allow a Is that PR #698 enough for you, to see If not, we can close the PR (the code still remains and could be used later on). |
Yes, even though I would do as class attribute as I mentioned previously. On the other hand, pretty much all users prefer to have it as parameter, so for me it's ok to have as parameter. |
Now remains the question of the documentation: Do we want only one style (init parameter or |
Something I remember from #719 is that if |
I have a slight preference for the "init parameter" way (as Casper said we don't have that much clutter in the signature especially when includeSCPI should disappear, and it's a bit clearer that this option is there). |
I prefer the init parameter as well, but I'm reluctant to touch all the instruments. What do you think:
EDIT: |
Excellent suggestion, agreed, that's better! |
Divide and conquer 😁 I have learnt my lesson of large PRs with too little talk about the principles first. |
…ents. This catches new instrument PRs.
f411648
to
7f884fb
Compare
As discussed, I stored the changes in a local branch (if we should need them) and started from scratch:
Should we add a change note? It only affects new instrument creators. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, with some restructuring suggestions for the tests.
Why not. A hint about the new best practice could be useful. When I compile the changelog, I mostly go by PR titles and what I remember from the PRs. If there are details that should or must be in the changelog, it's good to add them with the PR, at least for us maintainers -- there's less potential that something is missed. This primarily concerns "core"-touching PRs, for instruments it's straightforward enough most of the time. |
Thanks for the suggestions. |
Rewording of variable names. Change note added. ATSBase has a default name.
9450fe6
to
796cd23
Compare
As you both had only these minor (unrelated) changes, we can merge, I think. |
I agree |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good for me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Fixes #658 and adjustes the documentation accordingly.