Skip to contentSkip to conversation

data_smart: use a split/filter/rejoin for _remove

PATCH New Christopher Larson 1 participant 2 messages Aug 27, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 data_smart: use a split/filter/rejoin for _remove

From: Christopher Larson <[email protected]>

This is more idiomatic, and from the limited performance testing I did, is faster as well. See https://gist.github.com/kergoth/6360248 for the naive benchmark.

---
 lib/bb/data_smart.py | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
lib/bb/data_smart.py +3 -7
index 3fb88a9..6229fbf 100644
@@ -589,13 +589,9 @@ class DataSmart(MutableMapping):
         if expand and value:
             value = self.expand(value, None)
         if value and flag == "_content" and local_var and "_removeactive" in local_var:
-            for i in local_var["_removeactive"]:
-                if " " + i + " " in value:
-                    value = value.replace(" " + i + " ", " ")
-                if value.startswith(i + " "):
-                    value = value[len(i + " "):]
-                if value.endswith(" " + i):
-                    value = value[:-len(" " + i)]
+            filtered = filter(lambda v: v not in local_var["_removeactive"],
+                              value.split(" "))
+            value = " ".join(filtered)
         return value
 
     def delVarFlag(self, var, flag, **loginfo):
Signature · 1 line
1.8.3.4
#1 lore raw

PATCH 2/2 data_smart: allow removal of multiple words at once with _remove

From: Christopher Larson <[email protected]>

FOO = "foo bar baz"
FOO_remove = "foo baz"

---
 lib/bb/data_smart.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bb/data_smart.py +1 -1
index 6229fbf..d325018 100644
@@ -418,7 +418,7 @@ class DataSmart(MutableMapping):
                             self.setVar(append, sval)
                         elif op == "_remove":
                             removes = self.getVarFlag(append, "_removeactive", False) or []
-                            removes.append(a)
+                            removes.extend(a.split())
                             self.setVarFlag(append, "_removeactive", removes, ignore=True)
 
                     # We save overrides that may be applied at some later stage
Signature · 1 line
1.8.3.4