def test_compile_function(self): # Assert creating and caching Pattern with compile(). t = search.Taxonomy() p = search.compile("JJ?+ NN*", search.STRICT, taxonomy=t) self.assertEqual(p.strict, True) self.assertEqual(p[0].optional, True) self.assertEqual(p[0].tags, ["JJ"]) self.assertEqual(p[1].tags, ["NN*"]) self.assertEqual(p[1].taxonomy, t) # Assert regular expression input. p = search.compile(re.compile(r"[0-9|\.]+")) self.assertTrue(isinstance(p[0].words[0], search.regexp)) # Assert TypeError for other input. self.assertRaises(TypeError, search.compile, 1) print "pattern.search.compile()"
def grep(pattern, sentences): pattern = compile(pattern) for id, sentence in sentences: match = pattern.search(sentence) if match: yield id, color_hit(sentence, match), match