From dc44aad4116e0613267de8da4e6942e13a657a00 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 16 Apr 2016 14:20:45 +0800 Subject: [PATCH] environment: make NumberValue return integers when appropriate. Closes #397 --- RELEASE_NOTES.rst | 3 ++ .../master/repository/arguments_demo.py | 5 ++- artiq/language/environment.py | 33 +++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/RELEASE_NOTES.rst b/RELEASE_NOTES.rst index 860b2f4bb..93a997bab 100644 --- a/RELEASE_NOTES.rst +++ b/RELEASE_NOTES.rst @@ -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 ------ diff --git a/artiq/examples/master/repository/arguments_demo.py b/artiq/examples/master/repository/arguments_demo.py index 4d926d4f4..8c6370d56 100644 --- a/artiq/examples/master/repository/arguments_demo.py +++ b/artiq/examples/master/repository/arguments_demo.py @@ -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) diff --git a/artiq/language/environment.py b/artiq/language/environment.py index 2654b4702..35654aab4 100644 --- a/artiq/language/environment.py +++ b/artiq/language/environment.py @@ -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