if a != b: return False return True def animalGenerator(): yield "cat" yield "dog" yield "mouse" for i in animalGenerator(): assert i in ("cat", "dog", "mouse") assert isinstance(i, str) for i in untrusted.iterator(animalGenerator()): assert i in ("cat", "dog", "mouse") assert isinstance(i, untrusted.string) it = untrusted.iterator(animalGenerator()) # an iterator always returns itself assert iter(it) is it assert same(untrusted.string("cat"), next(it)) assert same(untrusted.string("dog"), next(it)) assert same(untrusted.string("mouse"), next(it)) try: _ = next(it) raise AssertionError
def __reversed__(self): # underlying str doesn't have __reversed__ for us to wrap return untrusted.iterator(reversed(self.value))
def __iter__(self): yield from untrusted.iterator(self.obj, valueType=self._valueType)
def __iter__(self): return untrusted.iterator(self.obj, valueType=self._valueType)
def _to_untrusted_iterator(xs, _keyType, valueType): return untrusted.iterator(xs, valueType=valueType)
import untrusted # untrusted.iterator example #------------------------------------------------------------------------------- # This is a simple iterator type for a single step over any iterable of # untrusted values. # The default type is an iterator of untrusted.strings # it = untrusted.iterator(open("example.txt"), valueType=untrusted.string) # Or, more simply, it = untrusted.iterator(open("example.txt")) print("repr(it): %s" % repr(it)) for i in it: print("repr(i): %s" % repr(i)) # You can come up with more complicated types, e.g. nested lists. someValues = [ ["cat", "dog", "zebra"], ["apple", "pear", "pineapple"], ["green", "red", "rainbow", "<span color=\"green\">red</span>"] ] myIteratorType = untrusted.iteratorOf(untrusted.iterator)