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 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/p4p/nt/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def __repr__(self):
class NTEnum(NTBase):
"""Describes a string selected from among a list of possible choices. Stored internally as an integer
"""
Value = Value

@staticmethod
def buildType(extra=[], display=False, control=False, valueAlarm=False):
F = [
Expand All @@ -61,19 +63,28 @@ def __init__(self, **kws):

def wrap(self, value, choices=None, **kws):
"""Pack python value into Value

Accepts dict to explicitly initialize fields by name.
Any other type is assigned to the 'value' field via
the self.assign() method.
"""
V = self.type()
if choices is not None:
V['value.choices'] = choices

if isinstance(value, dict):
# assume dict of index and choices list
V.value = value
self._choices = V['value.choices']
if isinstance(value, Value):
pass
elif isinstance(value, ntwrappercommon):
kws.setdefault('timestamp', value.timestamp)
value = value.raw
elif isinstance(value, dict):
value = self.Value(self.type, value)
else:
# index or string
V = self.type()
if choices is not None:
V['value.choices'] = choices
self.assign(V, value)
return self._annotate(V, **kws)
value = V

self._choices = value['value.choices'] or self._choices
return self._annotate(value, **kws)

def unwrap(self, value):
"""Unpack a Value into an augmented python type (selected from the 'value' field)
Expand Down
25 changes: 24 additions & 1 deletion src/p4p/test/test_nt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?


self.assertEqual(V.value.index, 1)
self.assertEqual(V.value.choices, ['X','Y'])

def testDictionaryWrap(self):
W = nt.NTEnum()
V = W.wrap({'value.index': 0,
'value.choices': ['X','Y'],
'alarm.severity': 1})

self.assertEqual(V.value.index, 0)
self.assertEqual(V.value.choices, ['X','Y'])
self.assertEqual(V.alarm.severity, 1)

def testWrap(self):
W = nt.NTEnum()
V = W.wrap(0)

self.assertEqual(V.value.index, 0)
self.assertEqual(V.value.choices, [])
self.assertTrue(V.changed('value'))

V = W.wrap(1, choices=['X', 'Y'])

self.assertEqual(V.value.index, 1)
self.assertEqual(V.value.choices, ['X', 'Y'])
self.assertTrue(V.changed('value'))

V = W.wrap('X')

self.assertEqual(V.value.index, 0)
self.assertTrue(V.changed('value.index'))

def testAssign(self):
W = nt.NTEnum()
Expand Down