| 
									
										
										
										
											1992-04-02 10:37:02 +00:00
										 |  |  | # New dir() function | 
					
						
							| 
									
										
										
										
											1991-11-12 15:38:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # This should be the new dir(), except that it should still list | 
					
						
							|  |  |  | # the current local name space by default | 
					
						
							| 
									
										
										
										
											1992-04-02 10:37:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1991-11-12 15:38:08 +00:00
										 |  |  | def listattrs(x): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         dictkeys = x.__dict__.keys() | 
					
						
							|  |  |  |     except (AttributeError, TypeError): | 
					
						
							|  |  |  |         dictkeys = [] | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         methods = x.__methods__ | 
					
						
							|  |  |  |     except (AttributeError, TypeError): | 
					
						
							|  |  |  |         methods = [] | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         members = x.__members__ | 
					
						
							|  |  |  |     except (AttributeError, TypeError): | 
					
						
							|  |  |  |         members = [] | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         the_class = x.__class__ | 
					
						
							|  |  |  |     except (AttributeError, TypeError): | 
					
						
							|  |  |  |         the_class = None | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         bases = x.__bases__ | 
					
						
							|  |  |  |     except (AttributeError, TypeError): | 
					
						
							|  |  |  |         bases = () | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     total = dictkeys + methods + members | 
					
						
							|  |  |  |     if the_class: | 
					
						
							|  |  |  |         # It's a class instace; add the class's attributes | 
					
						
							|  |  |  |         # that are functions (methods)... | 
					
						
							|  |  |  |         class_attrs = listattrs(the_class) | 
					
						
							|  |  |  |         class_methods = [] | 
					
						
							|  |  |  |         for name in class_attrs: | 
					
						
							|  |  |  |             if is_function(getattr(the_class, name)): | 
					
						
							|  |  |  |                 class_methods.append(name) | 
					
						
							|  |  |  |         total = total + class_methods | 
					
						
							|  |  |  |     elif bases: | 
					
						
							|  |  |  |         # It's a derived class; add the base class attributes | 
					
						
							|  |  |  |         for base in bases: | 
					
						
							|  |  |  |             base_attrs = listattrs(base) | 
					
						
							|  |  |  |             total = total + base_attrs | 
					
						
							|  |  |  |     total.sort() | 
					
						
							|  |  |  |     return total | 
					
						
							|  |  |  |     i = 0 | 
					
						
							|  |  |  |     while i+1 < len(total): | 
					
						
							|  |  |  |         if total[i] == total[i+1]: | 
					
						
							|  |  |  |             del total[i+1] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             i = i+1 | 
					
						
							|  |  |  |     return total | 
					
						
							| 
									
										
										
										
											1991-11-12 15:38:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-04-02 10:37:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1991-11-12 15:38:08 +00:00
										 |  |  | # Helper to recognize functions | 
					
						
							| 
									
										
										
										
											1992-04-02 10:37:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1991-11-12 15:38:08 +00:00
										 |  |  | def is_function(x): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     return type(x) == type(is_function) | 
					
						
							| 
									
										
										
										
											1991-11-12 15:38:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-04-02 10:37:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Approximation of builtin dir(); but note that this lists the user's | 
					
						
							| 
									
										
										
										
											1991-11-12 15:38:08 +00:00
										 |  |  | # variables by default, not the current local name space. | 
					
						
							| 
									
										
										
										
											1992-04-02 10:37:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-01 11:34:53 +00:00
										 |  |  | def dir(x = None): | 
					
						
							| 
									
										
										
										
											2004-07-18 06:16:08 +00:00
										 |  |  |     if x is not None: | 
					
						
							|  |  |  |         return listattrs(x) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         import __main__ | 
					
						
							|  |  |  |         return listattrs(__main__) |