Re: [PyCrust] Attribute list hook
Brought to you by:
pobrien
From: Neil H. <ne...@sc...> - 2001-08-12 00:09:17
|
Me: > If not, I propose a convention which is to provide a getAttributeNames > method on any class that wants to expose its dynamic (or semi-dynamic) > attributes to shells. Here is an implementation to stick on PythonCard's widget class: def getAttributeNames(self): names = [] for method in self.__class__.__dict__.keys(): if method.startswith("get"): name = method[3:] if name and self.__class__.__dict__.has_key("set" + name): names.append(name[0].lower() + name[1:]) for base in self.__class__.__bases__: for method in base.__dict__.keys(): if method.startswith("get"): name = method[3:] if name and base.__dict__.has_key("set" + name): names.append(name[0].lower() + name[1:]) return names This implementation only returns names where there is both a set* and a get* method available. Other implementations may allow read only attributes or may interrogate a database to find the list. PyCrust can be changed to use this with the getAttributes method in PyCrustInterp.py changed to: def getAttributes(self, object): # Return a list of attributes, including inherited, for an object. try: attributes = dir(object) if hasattr(object, '__class__'): attributes += self.getAttributes(object.__class__) try: attributes += object.getAttributeNames() except AttributeError: pass if hasattr(object, '__bases__'): ... Neil |