[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[minion-cvs] Add a "stringContains" function to simplify spelling of



Update of /home/minion/cvsroot/src/minion/lib/mixminion
In directory moria.seul.org:/tmp/cvs-serv8845/lib/mixminion

Modified Files:
	Common.py 
Log Message:
Add a 'stringContains' function to simplify spelling of
"a.find(b)!=-1", and avoid the common "a.find(b)!=0" error.



Index: Common.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/Common.py,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -d -r1.25 -r1.26
--- Common.py	22 Nov 2002 00:15:07 -0000	1.25
+++ Common.py	2 Dec 2002 03:21:54 -0000	1.26
@@ -7,7 +7,7 @@
 
 __all__ = [ 'MixError', 'MixFatalError', 'onReset', 'onTerminate',
             'installSignalHandlers', 'secureDelete', 'secureRename',
-            'ceilDiv', 'floorDiv', 'getLog' ]
+            'ceilDiv', 'floorDiv', 'getLog', 'stringContains' ]
 
 import os
 import signal
@@ -51,6 +51,19 @@
 def ceilDiv(a,b):
     "Compute ceil(a / b). See comments for portability notes."
     return divmod(a-1,b)[0]+1
+
+#----------------------------------------------------------------------
+
+# We create an alias to make the intent of substring-checking code
+# more explicit.  It's a bit easier to remember "stringContains(s1,
+# s2)" than "s1.find(s2)!=-1".
+#
+# Note that if s2 is a single character, "s2 in s1" works fine.  Also,
+# starting with Python 2.3, the single-caracter restiction is gone.
+def stringContains(s1, s2):
+    """Return true iff s2 is contained within s1; that is, for some i,
+       s1[i:i+len(s2)] == s2"""
+    return s1.find(s2) != -1
 
 #----------------------------------------------------------------------
 def createPrivateDir(d, nocreate=0):