Patchworkβ [oe] base.bbclass: add 'any' and 'all' functions.

login
register
about
Submitter Christopher Larson
Date 2010-01-19 20:01:07
Message ID <1263931267-18470-1-git-send-email-clarson@mvista.com>
Download mbox | patch
Permalink /patch/1485/
State Rejected, archived
Headers show

Comments

Christopher Larson - 2010-01-19 20:01:07
Python 2.5+ has these functions built-in, but we don't yet require 2.5+.

Signed-off-by: Chris Larson <clarson@mvista.com>
---
 classes/base.bbclass |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
Holger Freyther - 2010-01-24 10:48:25
On Tuesday 19 January 2010 21:01:07 Chris Larson wrote:
> Python 2.5+ has these functions built-in, but we don't yet require 2.5+.
> 

do we use all/any in the metadata? or is MVista using? If the answer is yes 
then... I'm acking the patch.
Christopher Larson - 2010-01-26 21:17:00
On Sun, Jan 24, 2010 at 3:48 AM, Holger Hans Peter Freyther <
holger+oe@freyther.de <holger%2Boe@freyther.de>> wrote:

> On Tuesday 19 January 2010 21:01:07 Chris Larson wrote:
> > Python 2.5+ has these functions built-in, but we don't yet require 2.5+.
> >
>
> do we use all/any in the metadata? or is MVista using? If the answer is yes
> then... I'm acking the patch.
>

We don't yet.  I've used it in some classes and .inc files in MVL6 that I
plan on pushing to OE.  They're much more useful than you'd think.  As an
example, I had some code where I had a list of path prefixes which were
allowed, and wanted to check a filename against them:

if any(path.startswith(prefix + "/") for prefix in allowedpaths):

Patch

diff --git a/classes/base.bbclass b/classes/base.bbclass
index 4d25258..dc56282 100644
--- a/classes/base.bbclass
+++ b/classes/base.bbclass
@@ -1,5 +1,20 @@ 
 BB_DEFAULT_TASK ?= "build"
 
+# Useful Python 2.5+ built-in functions
+def any(iterable):
+    """ Return True if any element of the iterable is true.  If the iterable is empty, return False. """
+    for element in iterable:
+        if element:
+            return True
+    return False
+
+def all(iterable):
+    """ Return True if all elements of the iterable are true (or if the iterable is empty). """
+    for element in iterable:
+        if not element:
+            return False
+    return True
+
 python () {
     env = {}
     for v in d.keys():