python, "a in b" keyword, how about multiple a's?
alternatives = ("// @in ", "// @out ", "// @ret ")
if any(a in sTxT for a in alternatives):
print "found"
if all(a in sTxT for a in alternatives):
print "found all"
any()
and all()
takes an iterable and checks if any/all of them evaluate to a true value. Combine that with a generator expressions, and you can check multiple items.
any(snippet in text_body for snippet in ("hi", "foo", "bar", "spam"))
If you're testing lots of lines for the same words, it may be faster to compile them as a regular expression. eg:
import re
words = ["// @in ", "// @out ", "// @ret "] + ["// @test%s " % i for i in range(10)]
my_regex = re.compile("|".join(map(re.escape, words)))
for line in lines_to_search:
if my_regex.search(line): print "Found match"
Some quick timing shows that this is usually faster than the any(word in theString for word in words)
approach. I've tested both approaches with varying text (short/long with/without matches). Here are the results:
{ No keywords } | {contain Keywords }
short long short long
regex : 0.214 27.214 0.147 0.149
any in : 0.579 81.341 0.295 0.300
If performance doesn't matter though, the any()
approach is more readable.