Skip to contentSkip to conversation

utils.py: add var_in_numeric_range()

PATCH In review [email protected] 2 participants 2 messages Jan 30, 2024 – Feb 5, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 utils.py: add var_in_numeric_range()

From: Joe Slater <[email protected]>

Add a function returning a value based on whether a bitbake variable is in a numeric range.

---
 lib/bb/utils.py | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/bb/utils.py +11 -0
index 068b631c9..8bb2d5d2d 100644
@@ -1810,6 +1810,17 @@ def is_local_uid(uid=''):
                 return True
     return False
 
+def var_in_numeric_range(d, var, first=1, last=0, truevalue="", falsevalue=""):
+    """
+    Return a value depending on whether a bitbake variable is in a numeric
+    range.  Conventionally, 0 as the end of the range matches all values.
+    """
+    try:
+        v = int(d.getVar(var))
+    except:
+        return falsevalue
+    return truevalue if v >= first and (v <= last or last == 0) else falsevalue
+
 def mkstemp(suffix=None, prefix=None, dir=None, text=False):
     """
     Generates a unique filename, independent of time.
Signature · 1 line
2.25.1
#1 lore raw
Quoted 25 lines

-----Original Message-----
From: [email protected] <bitbake- [email protected]> On Behalf Of Joe Slater via lists.openembedded.org
Sent: den 30 januari 2024 23:02
To: [email protected]
Cc: [email protected]; [email protected]
Subject: [bitbake-devel] [v2][bitbake][PATCH 1/1] utils.py: add var_in_numeric_range()

From: Joe Slater <[email protected]>

Add a function returning a value based on whether a bitbake variable is in a numeric range.

---
 lib/bb/utils.py | 11 +++++++++++
 1 file changed, 11 insertions(+)
lib/bb/utils.py +0 -0
index 068b631c9..8bb2d5d2d 100644

lib/bb/utils.py def is_local_uid(uid=''): · line 1812

Quoted 3 lines
lib/bb/utils.py +0 -0
@@ -1810,6 +1810,17 @@ def is_local_uid(uid=''):
                 return True
     return False
 

It would make more sense to put this function after filter() so that it is located together with the other similar functions?

lib/bb/utils.py def is_local_uid(uid=''): · line 1816

Quoted 4 lines
lib/bb/utils.py +4 -0
+def var_in_numeric_range(d, var, first=1, last=0, truevalue="", falsevalue=""):
+    """
+    Return a value depending on whether a bitbake variable is in a numeric
+    range.  Conventionally, 0 as the end of the range matches all values.

You should use None to indicate no limit instead. That way you can do likewise for first so that it too can have no limit.

I would also recommend to better match the APIs for the existing functions, e.g.:

def var_in_numeric_range(variable, first, last, truevalue, falsevalue, d):

or even:

def in_numeric_range(variable, first, last, truevalue, falsevalue, d):

since neither of contains(), contains_any() or filter() are prefixed with "var_".

lib/bb/utils.py def is_local_uid(uid=''): · line 1821

Quoted 5 lines
lib/bb/utils.py +5 -0
+    """
+    try:
+        v = int(d.getVar(var))
+    except:
+        return falsevalue

Since the function is named var_in_numeric_range rather than var_in_int_range, you should support any numeric ranges:

try:
var = int(d.getVar(variable))
f = int(str(first)) if first is not None else None l = int(str(last)) if last is not None else None
except ValueError:
try:
var = float(d.getVar(variable))
f = float(str(first)) if first is not None else None l = float(str(last)) if last is not None else None
except ValueError:
return falsevalue

Using int(str(first)) above rather than int(first) makes sure that if first and/or last are specified as decimal numbers, then all the calculations will be using floats.

Quoted 7 lines
lib/bb/utils.py +2 -1
+    return truevalue if v >= first and (v <= last or last == 0) else falsevalue
+
 def mkstemp(suffix=None, prefix=None, dir=None, text=False):
     """
     Generates a unique filename, independent of time.
--

2.25.1

//Peter