Skip to content

Commit

Permalink
NTScalar add form=False
Browse files Browse the repository at this point in the history
Another step on the slow migration from display.format
to display.form/.precision
  • Loading branch information
mdavidsaver committed Aug 9, 2023
1 parent e2eb24e commit 360c131
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
24 changes: 21 additions & 3 deletions src/p4p/nt/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,21 @@ class ntstringarray(ntwrappercommon, list):
* .raw - The underlying :py:class:`p4p.Value`.
"""

def _metaHelper(F, valtype, display=False, control=False, valueAlarm=False):
def _metaHelper(F, valtype, display=False, control=False, valueAlarm=False, form=False):
isnumeric = valtype[-1:] not in '?su'
if display and isnumeric:
F.extend([
('display', ('S', None, [
('limitLow', valtype[-1:]),
('limitHigh', valtype[-1:]),
('description', 's'),
('precision', 'i'),
('form', ('S', 'enum_t', [
('index', 'i'),
('choices', 'as'),
])),
('units', 's'),
] if form else [
('limitLow', valtype[-1:]),
('limitHigh', valtype[-1:]),
('description', 's'),
Expand Down Expand Up @@ -175,18 +185,26 @@ class NTScalar(NTBase):
* .raw_stamp - A tuple of (seconds, nanoseconds)
* .severity - An integer in the range [0, 3]
* .raw - The complete underlying :class:`~p4p.Value`
:param str valtype: A type code to be used with the 'value' field. See :ref:`valuecodes`
:param list extra: A list of tuples describing additional non-standard fields
:param bool display: Include optional fields for display meta-data
:param bool control: Include optional fields for control meta-data
:param bool valueAlarm: Include optional fields for alarm level meta-data
:param bool form: Include ``display.form`` instead of the deprecated ``display.format``.
"""
Value = Value

@staticmethod
def buildType(valtype, extra=[], display=False, control=False, valueAlarm=False):
def buildType(valtype, extra=[], *args, **kws):
"""Build a Type
:param str valtype: A type code to be used with the 'value' field. See :ref:`valuecodes`
:param list extra: A list of tuples describing additional non-standard fields
:param bool display: Include optional fields for display meta-data
:param bool control: Include optional fields for control meta-data
:param bool valueAlarm: Include optional fields for alarm level meta-data
:param bool form: Include ``display.form`` instead of the deprecated ``display.format``.
:returns: A :py:class:`Type`
"""
isarray = valtype[:1] == 'a'
Expand All @@ -195,7 +213,7 @@ def buildType(valtype, extra=[], display=False, control=False, valueAlarm=False)
('alarm', alarm),
('timeStamp', timeStamp),
]
_metaHelper(F, valtype, display=display, control=control, valueAlarm=valueAlarm)
_metaHelper(F, valtype, *args, **kws)
F.extend(extra)
return Type(id="epics:nt/NTScalarArray:1.0" if isarray else "epics:nt/NTScalar:1.0",
spec=F)
Expand Down
18 changes: 15 additions & 3 deletions src/p4p/test/test_nt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

class TestScalar(RefTestCase):

def test_float_wrap(self, code='d', value=5.0):
def test_float_wrap(self, code='d', value=5.0, form=False):
NT = nt.NTScalar(code)

V = NT.wrap(value, timestamp=None)
self.assertEqual(V.value, value)
self.assertEqual(V.alarm.severity, 0)
self.assertIsNone(V.get('display'))

NT = nt.NTScalar(code, display=True)
NT = nt.NTScalar(code, display=True, form=form)
V = NT.wrap({
'value': value,
'alarm': {
Expand All @@ -35,7 +35,16 @@ def test_float_wrap(self, code='d', value=5.0):

self.assertEqual(V.value, value)
self.assertEqual(V.alarm.severity, 1)
if code!='s':
if code!='s' and form:
self.assertEqual(V.display.tolist(), [
('limitLow', 0.0),
('limitHigh', 0.0),
('description', u''),
('precision', 0),
('form', [('index', 0), ('choices', [])]),
('units', u'')
])
if code!='s' and not form:
self.assertEqual(V.display.tolist(), [
('limitLow', 0.0),
('limitHigh', 0.0),
Expand All @@ -44,6 +53,9 @@ def test_float_wrap(self, code='d', value=5.0):
('units', u'')
])

def test_float_wrap_form(self):
self.test_float_wrap(form=True)

def test_time_wrap(self):
NT = nt.NTScalar('d')
V = NT.wrap(42, timestamp=None) # no timestamp
Expand Down

0 comments on commit 360c131

Please sign in to comment.