diff mbox series

[v2,1/1] utils.py: add var_in_numeric_range()

Message ID 20240130220213.3124647-1-joe.slater@windriver.com
State New
Headers show
Series [v2,1/1] utils.py: add var_in_numeric_range() | expand

Commit Message

Slater, Joseph Jan. 30, 2024, 10:02 p.m. UTC
From: Joe Slater <joe.slater@windriver.com>

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

Signed-off-by: Joe Slater <joe.slater@windriver.com>
---
 lib/bb/utils.py | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Peter Kjellerstedt Feb. 5, 2024, 3:44 p.m. UTC | #1
> -----Original Message-----
> From: bitbake-devel@lists.openembedded.org <bitbake-
> devel@lists.openembedded.org> On Behalf Of Joe Slater via
> lists.openembedded.org
> Sent: den 30 januari 2024 23:02
> To: bitbake-devel@lists.openembedded.org
> Cc: joe.slater@windriver.com; randy.macleod@windriver.com
> Subject: [bitbake-devel] [v2][bitbake][PATCH 1/1] utils.py: add
> var_in_numeric_range()
> 
> From: Joe Slater <joe.slater@windriver.com>
> 
> Add a function returning a value based on whether a
> bitbake variable is in a numeric range.
> 
> Signed-off-by: Joe Slater <joe.slater@windriver.com>
> ---
>  lib/bb/utils.py | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/lib/bb/utils.py b/lib/bb/utils.py
> index 068b631c9..8bb2d5d2d 100644
> --- a/lib/bb/utils.py
> +++ b/lib/bb/utils.py
> @@ -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?

> +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_".

> +    """
> +    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.

> +    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
diff mbox series

Patch

diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index 068b631c9..8bb2d5d2d 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -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.