| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  | """Example of a generator: re-implement the built-in range function
 | 
					
						
							|  |  |  | without actually constructing the list of values. | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  | OldStyleRange is coded in the way required to work in a 'for' loop before | 
					
						
							|  |  |  | iterators were introduced into the language; using __getitem__ and __len__ . | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | def handleargs(arglist): | 
					
						
							|  |  |  |     """Take list of arguments and extract/create proper start, stop, and step
 | 
					
						
							|  |  |  |     values and return in a tuple"""
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         if len(arglist) == 1: | 
					
						
							|  |  |  |             return 0, int(arglist[0]), 1 | 
					
						
							|  |  |  |         elif len(arglist) == 2: | 
					
						
							|  |  |  |             return int(arglist[0]), int(arglist[1]), 1 | 
					
						
							|  |  |  |         elif len(arglist) == 3: | 
					
						
							|  |  |  |             if arglist[2] == 0: | 
					
						
							|  |  |  |                 raise ValueError("step argument must not be zero") | 
					
						
							|  |  |  |             return tuple(int(x) for x in arglist) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             raise TypeError("range() accepts 1-3 arguments, given", len(arglist)) | 
					
						
							|  |  |  |     except TypeError: | 
					
						
							|  |  |  |         raise TypeError("range() arguments must be numbers or strings " | 
					
						
							|  |  |  |         "representing numbers") | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  | def genrange(*a): | 
					
						
							|  |  |  |     """Function to implement 'range' as a generator""" | 
					
						
							|  |  |  |     start, stop, step = handleargs(a) | 
					
						
							|  |  |  |     value = start | 
					
						
							|  |  |  |     while value < stop: | 
					
						
							|  |  |  |         yield value | 
					
						
							|  |  |  |         value += step | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  | class oldrange: | 
					
						
							|  |  |  |     """Class implementing a range object.
 | 
					
						
							|  |  |  |     To the user the instances feel like immutable sequences | 
					
						
							|  |  |  |     (and you can't concatenate or slice them) | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |     Done using the old way (pre-iterators; __len__ and __getitem__) to have an | 
					
						
							|  |  |  |     object be used by a 'for' loop. | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |     def __init__(self, *a): | 
					
						
							|  |  |  |         """ Initialize start, stop, and step values along with calculating the
 | 
					
						
							|  |  |  |         nubmer of values (what __len__ will return) in the range"""
 | 
					
						
							|  |  |  |         self.start, self.stop, self.step = handleargs(a) | 
					
						
							|  |  |  |         self.len = max(0, (self.stop - self.start) // self.step) | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |     def __repr__(self): | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |         """implement repr(x) which is also used by print""" | 
					
						
							| 
									
										
										
										
											2004-02-12 17:35:32 +00:00
										 |  |  |         return 'range(%r, %r, %r)' % (self.start, self.stop, self.step) | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |     def __len__(self): | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |         """implement len(x)""" | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |         return self.len | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |     def __getitem__(self, i): | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |         """implement x[i]""" | 
					
						
							|  |  |  |         if 0 <= i <= self.len: | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |             return self.start + self.step * i | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2007-07-17 20:59:35 +00:00
										 |  |  |             raise IndexError('range[i] index out of range') | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test(): | 
					
						
							| 
									
										
										
										
											2007-12-02 09:40:06 +00:00
										 |  |  |     import time, builtins | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |     #Just a quick sanity check | 
					
						
							| 
									
										
										
										
											2010-07-04 17:47:30 +00:00
										 |  |  |     correct_result = list(builtins.range(5, 100, 3)) | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |     oldrange_result = list(oldrange(5, 100, 3)) | 
					
						
							|  |  |  |     genrange_result = list(genrange(5, 100, 3)) | 
					
						
							|  |  |  |     if genrange_result != correct_result or oldrange_result != correct_result: | 
					
						
							|  |  |  |         raise Exception("error in implementation:\ncorrect   = %s" | 
					
						
							|  |  |  |                          "\nold-style = %s\ngenerator = %s" % | 
					
						
							|  |  |  |                          (correct_result, oldrange_result, genrange_result)) | 
					
						
							| 
									
										
										
										
											2007-07-17 20:59:35 +00:00
										 |  |  |     print("Timings for range(1000):") | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |     t1 = time.time() | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |     for i in oldrange(1000): | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |         pass | 
					
						
							|  |  |  |     t2 = time.time() | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |     for i in genrange(1000): | 
					
						
							| 
									
										
										
										
											2003-04-24 17:13:18 +00:00
										 |  |  |         pass | 
					
						
							|  |  |  |     t3 = time.time() | 
					
						
							| 
									
										
										
										
											2007-12-02 09:40:06 +00:00
										 |  |  |     for i in builtins.range(1000): | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  |         pass | 
					
						
							|  |  |  |     t4 = time.time() | 
					
						
							| 
									
										
										
										
											2007-07-17 20:59:35 +00:00
										 |  |  |     print(t2-t1, 'sec (old-style class)') | 
					
						
							|  |  |  |     print(t3-t2, 'sec (generator)') | 
					
						
							|  |  |  |     print(t4-t3, 'sec (built-in)') | 
					
						
							| 
									
										
										
										
											1992-08-13 12:14:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-27 23:17:35 +00:00
										 |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     test() |