Python component model

Tim Chase python.list at tim.thechases.com
Tue Oct 10 09:59:37 EDT 2006


> There's no doubt that Python's excellent introspection mechanism allows 
> an outside RAD-like tool to inspect the workings of any Python object. 
> But that does not make it a component model in my original use of the 
> term on this thread. A RAD tool needs to know what properties and events 
> within a class can be manipulated visually, and it needs to be able to 
> serialize those properties and events so that they are set at run-time 
> automatically once an object is created.

A little visual inspection of some objects:

tim at oblique:~$ python
Python 2.3.5 (#2, Sep  4 2005, 22:01:42)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more 
information.
 >>> class Person(object):
...     def __init__(self, name, age=None):
...             self.name = name
...             self.age = age
...     def whoami(self):
...             if self.age is not None:
...			return "%s (%i)" % (
...				self.name,
...				self.age)
...             return self.name
...
 >>> p = Person("Sandy")
 >>> [s for s in dir(p) if not s.startswith('_') and 
callable(eval('p.%s' % s))]
['whoami']
 >>> [s for s in dir(p) if not s.startswith('_') and not 
callable(eval('p.%s' % s))]
['age', 'name']

Thus, you have the ability to find an object's methods/events 
(things that are callable()), and its properties (things that are 
not callable()).  Any "RAD" tool that wants can pull these 
properties, just as my command-line RAD tool can ;)

As for serializing them,

 >>> import shelve
 >>> d = shelve.open('tmp/stuff.shlv')
 >>> d['person'] = p
 >>> p = 'hello'
 >>> p
'hello'
 >>> p = d['person']
 >>> p.whoami()
'Sandy'
 >>> p.age = 42
 >>> p.whoami()
'Sandy (42)'
 >>> d['person'] = p
 >>> d.close()
 >>> p = 'hello2'
 >>> p
'hello2'
 >>> d = shelve.open('tmp/stuff.shlv')
 >>> p = d['person']
 >>> p.whoami()
'Sandy (42)'

which seems to work fine for me.  This can be used for creating 
all sorts of flavors of objects at design time, storing them, and 
then restoring them at runtime.

-tkc







More information about the Python-list mailing list