summaryrefslogtreecommitdiff
path: root/grc/core/params/param.py
diff options
context:
space:
mode:
authoraru31 <guptarpit1997@gmail.com>2019-02-02 21:40:09 +0530
committerAndrej Rode <mail@andrejro.de>2019-02-14 12:32:57 +0100
commit4befd86c7446812e553c4d0f2dff8ae74a5042bc (patch)
tree3968656ca2e93d64ac8152d84f9a91d242e35d35 /grc/core/params/param.py
parentec752effae559255b3aa5d2468f57a8139feb45f (diff)
grc: parse suffixes for numeric values
Previously only python scientific notation was supported. Now engineering notation e.g. k,M,G is supported as input format as well.
Diffstat (limited to 'grc/core/params/param.py')
-rw-r--r--grc/core/params/param.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/grc/core/params/param.py b/grc/core/params/param.py
index 69aa6fd811..3e5d3f2e28 100644
--- a/grc/core/params/param.py
+++ b/grc/core/params/param.py
@@ -65,6 +65,22 @@ class Param(Element):
self._lisitify_flag = False
self.hostage_cells = set()
self._init = False
+ self.scale = {
+ 'E': 1e18,
+ 'P': 1e15,
+ 'T': 1e12,
+ 'G': 1e9,
+ 'M': 1e6,
+ 'k': 1e3,
+ 'm': 1e-3,
+ 'u': 1e-6,
+ 'n': 1e-9,
+ 'p': 1e-12,
+ 'f': 1e-15,
+ 'a': 1e-18,
+ }
+ self.scale_factor = None
+ self.number = None
def _init_options(self, values, labels, attributes):
"""parse option and option attributes"""
@@ -162,6 +178,19 @@ class Param(Element):
def get_evaluated(self):
return self._evaluated
+ def is_float(self, num):
+ """
+ Check if string can be converted to float.
+
+ Returns:
+ bool type
+ """
+ try:
+ float(num)
+ return True
+ except ValueError:
+ return False
+
def evaluate(self):
"""
Evaluate the value.
@@ -174,6 +203,7 @@ class Param(Element):
self._stringify_flag = False
dtype = self.dtype
expr = self.get_value()
+ scale_factor = self.scale_factor
#########################
# ID and Enum types (not evaled)
@@ -191,6 +221,10 @@ class Param(Element):
elif dtype in ('raw', 'complex', 'real', 'float', 'int', 'hex', 'bool'):
if expr:
try:
+ if isinstance(expr, str) and self.is_float(expr[:-1]):
+ scale_factor = expr[-1:]
+ if scale_factor in self.scale:
+ expr = str(float(expr[:-1])*self.scale[scale_factor])
value = self.parent_flowgraph.evaluate(expr)
except Exception as e:
raise Exception('Value "{}" cannot be evaluated:\n{}'.format(expr, e))