forked from M-Labs/artiq
1
0
Fork 0

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
python/numpy "as expected".
* NumberValue now returns an integer if ``ndecimals`` = 0, ``scale`` = 1 and
``step`` is integer.
1.0rc2
------

View File

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

View File

@ -78,7 +78,12 @@ class EnumerationValue(_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
interface (UI) purposes.
@ -94,7 +99,8 @@ class NumberValue(_SimpleArgProcessor):
step=None, min=None, max=None, ndecimals=2):
if step is None:
step = scale/10.0
_SimpleArgProcessor.__init__(self, default)
if default is not NoDefault:
self.default_value = default
self.unit = unit
self.scale = scale
self.step = step
@ -102,8 +108,29 @@ class NumberValue(_SimpleArgProcessor):
self.max = max
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):
d = _SimpleArgProcessor.describe(self)
d = {"ty": self.__class__.__name__}
if hasattr(self, "default_value"):
d["default"] = self.default_value
d["unit"] = self.unit
d["scale"] = self.scale
d["step"] = self.step