def flatmatches(self, t): """Flattens t and then sees if any part of it matches self.pattern. """ try: # See if user gave entire type if self.matches(t): return True except TypeError: # This might fail, if it does just move on pass else: if isinstance(t, basestring): return self.matches(t) elif isinstance(t, (tuple, list)): return any([self.matches(i) for i in flatten(t)])
def test_flatten(): exp = ["hello", None, 1, 3, 2, 5, 6] obs = [x for x in flatten(["hello", None, (1, 3, (2, 5, 6))])] assert_equal(exp, obs)