From: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
To: "joe.slater@windriver.com" <joe.slater@windriver.com>,
"bitbake-devel@lists.openembedded.org"
<bitbake-devel@lists.openembedded.org>
Cc: "randy.macleod@windriver.com" <randy.macleod@windriver.com>
Subject: RE: [bitbake-devel] [v2][bitbake][PATCH 1/1] utils.py: add var_in_numeric_range()
Date: Mon, 5 Feb 2024 15:44:21 +0000 [thread overview]
Message-ID: <DB5PR02MB10213F886903D7557413ABCA7EF472@DB5PR02MB10213.eurprd02.prod.outlook.com> (raw)
In-Reply-To: <20240130220213.3124647-1-joe.slater@windriver.com>
> -----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
prev parent reply other threads:[~2024-02-05 15:44 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-30 22:02 [v2][bitbake][PATCH 1/1] utils.py: add var_in_numeric_range() joe.slater
2024-02-05 15:44 ` Peter Kjellerstedt [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DB5PR02MB10213F886903D7557413ABCA7EF472@DB5PR02MB10213.eurprd02.prod.outlook.com \
--to=peter.kjellerstedt@axis.com \
--cc=bitbake-devel@lists.openembedded.org \
--cc=joe.slater@windriver.com \
--cc=randy.macleod@windriver.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).