environment: make NumberValue return integers when appropriate. Closes #397

This commit is contained in:
Sebastien Bourdeauducq 2016-04-16 14:20:45 +08:00
parent d4ba525c9d
commit dc44aad411
3 changed files with 37 additions and 4 deletions

View File

@ -48,6 +48,9 @@ unreleased [1.0rc3]
from ARTIQ, trusting that h5py maps and converts types between HDF5 and from ARTIQ, trusting that h5py maps and converts types between HDF5 and
python/numpy "as expected". python/numpy "as expected".
* NumberValue now returns an integer if ``ndecimals`` = 0, ``scale`` = 1 and
``step`` is integer.
1.0rc2 1.0rc2
------ ------

View File

@ -44,6 +44,8 @@ class ArgumentsDemo(EnvExperiment):
self.setattr_argument("number", NumberValue(42e-6, self.setattr_argument("number", NumberValue(42e-6,
unit="us", scale=1e-6, unit="us", scale=1e-6,
ndecimals=4)) ndecimals=4))
self.setattr_argument("integer", NumberValue(42,
step=1, ndecimals=0))
self.setattr_argument("string", StringValue("Hello World")) self.setattr_argument("string", StringValue("Hello World"))
self.setattr_argument("scan", Scannable(global_max=400, self.setattr_argument("scan", Scannable(global_max=400,
default=NoScan(325), default=NoScan(325),
@ -65,7 +67,8 @@ class ArgumentsDemo(EnvExperiment):
print(self.pyon_value) print(self.pyon_value)
print(self.boolean) print(self.boolean)
print(self.enum) print(self.enum)
print(self.number) print(self.number, type(self.number))
print(self.integer, type(self.integer))
print(self.string) print(self.string)
for i in self.scan: for i in self.scan:
print(i) print(i)

View File

@ -78,7 +78,12 @@ class EnumerationValue(_SimpleArgProcessor):
class NumberValue(_SimpleArgProcessor): class NumberValue(_SimpleArgProcessor):
"""An argument that can take a numerical value (typically floating point). """An argument that can take a numerical value.
If ndecimals = 0, scale = 1 and step is integer, then it returns
an integer value. Otherwise, it returns a floating point value.
The simplest way to represent an integer argument is
``NumberValue(step=1, ndecimals=0)``.
:param unit: A string representing the unit of the value, for user :param unit: A string representing the unit of the value, for user
interface (UI) purposes. interface (UI) purposes.
@ -94,7 +99,8 @@ class NumberValue(_SimpleArgProcessor):
step=None, min=None, max=None, ndecimals=2): step=None, min=None, max=None, ndecimals=2):
if step is None: if step is None:
step = scale/10.0 step = scale/10.0
_SimpleArgProcessor.__init__(self, default) if default is not NoDefault:
self.default_value = default
self.unit = unit self.unit = unit
self.scale = scale self.scale = scale
self.step = step self.step = step
@ -102,8 +108,29 @@ class NumberValue(_SimpleArgProcessor):
self.max = max self.max = max
self.ndecimals = ndecimals self.ndecimals = ndecimals
def _is_int(self):
return (self.ndecimals == 0
and int(self.step) == self.step
and self.scale == 1)
def default(self):
if not hasattr(self, "default_value"):
raise DefaultMissing
if self._is_int():
return int(self.default_value)
else:
return float(self.default_value)
def process(self, x):
if self._is_int():
return int(x)
else:
return float(x)
def describe(self): def describe(self):
d = _SimpleArgProcessor.describe(self) d = {"ty": self.__class__.__name__}
if hasattr(self, "default_value"):
d["default"] = self.default_value
d["unit"] = self.unit d["unit"] = self.unit
d["scale"] = self.scale d["scale"] = self.scale
d["step"] = self.step d["step"] = self.step