| 1 | n/a | """Example of a generator: re-implement the built-in range function |
|---|
| 2 | n/a | without actually constructing the list of values. |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | OldStyleRange is coded in the way required to work in a 'for' loop before |
|---|
| 5 | n/a | iterators were introduced into the language; using __getitem__ and __len__ . |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | """ |
|---|
| 8 | n/a | def handleargs(arglist): |
|---|
| 9 | n/a | """Take list of arguments and extract/create proper start, stop, and step |
|---|
| 10 | n/a | values and return in a tuple""" |
|---|
| 11 | n/a | try: |
|---|
| 12 | n/a | if len(arglist) == 1: |
|---|
| 13 | n/a | return 0, int(arglist[0]), 1 |
|---|
| 14 | n/a | elif len(arglist) == 2: |
|---|
| 15 | n/a | return int(arglist[0]), int(arglist[1]), 1 |
|---|
| 16 | n/a | elif len(arglist) == 3: |
|---|
| 17 | n/a | if arglist[2] == 0: |
|---|
| 18 | n/a | raise ValueError("step argument must not be zero") |
|---|
| 19 | n/a | return tuple(int(x) for x in arglist) |
|---|
| 20 | n/a | else: |
|---|
| 21 | n/a | raise TypeError("range() accepts 1-3 arguments, given", len(arglist)) |
|---|
| 22 | n/a | except TypeError: |
|---|
| 23 | n/a | raise TypeError("range() arguments must be numbers or strings " |
|---|
| 24 | n/a | "representing numbers") |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | def genrange(*a): |
|---|
| 27 | n/a | """Function to implement 'range' as a generator""" |
|---|
| 28 | n/a | start, stop, step = handleargs(a) |
|---|
| 29 | n/a | value = start |
|---|
| 30 | n/a | while value < stop: |
|---|
| 31 | n/a | yield value |
|---|
| 32 | n/a | value += step |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | class oldrange: |
|---|
| 35 | n/a | """Class implementing a range object. |
|---|
| 36 | n/a | To the user the instances feel like immutable sequences |
|---|
| 37 | n/a | (and you can't concatenate or slice them) |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | Done using the old way (pre-iterators; __len__ and __getitem__) to have an |
|---|
| 40 | n/a | object be used by a 'for' loop. |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | """ |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | def __init__(self, *a): |
|---|
| 45 | n/a | """ Initialize start, stop, and step values along with calculating the |
|---|
| 46 | n/a | nubmer of values (what __len__ will return) in the range""" |
|---|
| 47 | n/a | self.start, self.stop, self.step = handleargs(a) |
|---|
| 48 | n/a | self.len = max(0, (self.stop - self.start) // self.step) |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | def __repr__(self): |
|---|
| 51 | n/a | """implement repr(x) which is also used by print""" |
|---|
| 52 | n/a | return 'range(%r, %r, %r)' % (self.start, self.stop, self.step) |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | def __len__(self): |
|---|
| 55 | n/a | """implement len(x)""" |
|---|
| 56 | n/a | return self.len |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | def __getitem__(self, i): |
|---|
| 59 | n/a | """implement x[i]""" |
|---|
| 60 | n/a | if 0 <= i <= self.len: |
|---|
| 61 | n/a | return self.start + self.step * i |
|---|
| 62 | n/a | else: |
|---|
| 63 | n/a | raise IndexError('range[i] index out of range') |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | def test(): |
|---|
| 67 | n/a | import time, builtins |
|---|
| 68 | n/a | #Just a quick sanity check |
|---|
| 69 | n/a | correct_result = list(builtins.range(5, 100, 3)) |
|---|
| 70 | n/a | oldrange_result = list(oldrange(5, 100, 3)) |
|---|
| 71 | n/a | genrange_result = list(genrange(5, 100, 3)) |
|---|
| 72 | n/a | if genrange_result != correct_result or oldrange_result != correct_result: |
|---|
| 73 | n/a | raise Exception("error in implementation:\ncorrect = %s" |
|---|
| 74 | n/a | "\nold-style = %s\ngenerator = %s" % |
|---|
| 75 | n/a | (correct_result, oldrange_result, genrange_result)) |
|---|
| 76 | n/a | print("Timings for range(1000):") |
|---|
| 77 | n/a | t1 = time.time() |
|---|
| 78 | n/a | for i in oldrange(1000): |
|---|
| 79 | n/a | pass |
|---|
| 80 | n/a | t2 = time.time() |
|---|
| 81 | n/a | for i in genrange(1000): |
|---|
| 82 | n/a | pass |
|---|
| 83 | n/a | t3 = time.time() |
|---|
| 84 | n/a | for i in builtins.range(1000): |
|---|
| 85 | n/a | pass |
|---|
| 86 | n/a | t4 = time.time() |
|---|
| 87 | n/a | print(t2-t1, 'sec (old-style class)') |
|---|
| 88 | n/a | print(t3-t2, 'sec (generator)') |
|---|
| 89 | n/a | print(t4-t3, 'sec (built-in)') |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | if __name__ == '__main__': |
|---|
| 93 | n/a | test() |
|---|