Created
March 10, 2014 18:26
-
-
Save barnash/9471105 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def our_for(iterable, what_to_do): | |
| iterator = iter(iterable) | |
| while True: | |
| try: | |
| i = next(iterator) | |
| what_to_do(i) | |
| except StopIteration as e: | |
| return | |
| class A: | |
| def __init__(self, values, counts): | |
| self.vals = values | |
| self.cnts = counts | |
| def __iter__(self): | |
| return AIterator(self.vals, self.cnts) | |
| class AIterator: | |
| def __init__(self, values, counts): | |
| self.all = [values[i] for i in range(len(values)) for j in range(counts[i])] | |
| self.i = 0 | |
| def __next__(self): | |
| if self.i == len(self.all): | |
| raise StopIteration | |
| else: | |
| res = self.all[self.i] | |
| self.i += 1 | |
| return res | |
| def countdown_gen(gen=True): | |
| if gen: | |
| def inner_gen(): | |
| print("2") | |
| yield 2 | |
| print("1") | |
| yield 1 | |
| print("launch") | |
| yield "launch" | |
| return inner_gen() | |
| else: | |
| print("2 1 launch") | |
| def events_list(n): | |
| return [num for num in range(n) if num % 2 == 0] | |
| def events_gen(n): | |
| for i in range(n): | |
| if i % 2 == 0: | |
| yield i | |
| def all_events_gen(): | |
| i = 0 | |
| while True: | |
| yield i | |
| i += 2 | |
| def events_gen2(n): | |
| return (num for num in range(n) if num % 2 == 0) | |
| class B: | |
| def __init__(self, values, counts): | |
| self.vals = values | |
| self.cnts = counts | |
| def __iter__(self): | |
| for i in range(len(self.vals)): | |
| for j in range(self.cnts[i]): | |
| yield self.vals[i] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment