Skip to content
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

add support to initialise NTEnum like NTScalar #154

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

m2es3h
Copy link

@m2es3h m2es3h commented Aug 20, 2024

When initialising an NTEnum with a python dict, it is assumed that the dict is meant for the Value.value and contains index and choices keys for the value. This PR adds ability to initialise value and any other fields of the NTEnum, similar to initialising an NTScalar.

pv = SharedPV(
    nt=NTEnum(display=True),
    initial={
        'value.index': 0,
        'value.choices': ['STOP', 'START', 'STANDBY'],
        'display.description': "Pump on/off control word."
    }
)

Copy link
Member

@mdavidsaver mdavidsaver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some coverage would need to be added to TestEnum in src/p4p/test/test_nt.py.

@@ -67,8 +67,12 @@ def wrap(self, value, choices=None, **kws):
V['value.choices'] = choices

if isinstance(value, dict):
# assume dict of index and choices list
V.value = value
if value.keys() == set(['index', 'choices']):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems unnecessarily strict. It is valid to update either 'index' or 'choices' independently, or to add custom fields. Updating only 'index' is likely the most common case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment below about the failing testDictionaryOfValueWrap() test.

src/p4p/nt/enum.py Outdated Show resolved Hide resolved
@@ -267,18 +267,41 @@ def testSubStore(self):
self.assertEqual(V.a.value.index, 1)
self.assertEqual(V.b.value.index, 0)

def testWrap(self):
def testDictionaryOfValueWrap(self):
W = nt.NTEnum()
V = W.wrap({'index':1, 'choices':['X','Y']})
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This existing test fails because it goes through the isinstance(value, dict) branch in NTEnum.wrap(), which now expects 'value.index' and/or 'value.choices' like NTScalar.wrap() does. Should I update this test, or should NTEnum.wrap() be able to handle a dictionary that only contains fields meant for the Value.value?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants