[python-win32] COM+metaclass+property issue

Thomas Lilley ted_lilley at yahoo.com
Fri Feb 18 11:28:41 CET 2005


I'm trying to manufacture a class that provides attributes which mimic the HTML collections offered by the Internet Explorer COM object.  My platform is Win32 (Windows XP) and ActiveState's latest Python 2.3.  The problem is, my attributes are based on properties, which work fine except for when I have a win32com collection returned, they don't appear to have any information in them.  Part of my code:
 
from win32com.client import Dispatch

class metaie(type):
    def __init__(cls, name, bases, dict):
        super(metaie, cls).__init__(name, bases, dict)
        for tag in cls.tags:
            setattr(cls, tag, property(lambda self: self.ie.Document.getElementsByTagName(tag)))
 
class ie(object):
    __metaclass__ = metaie
    tags = ["a", "area", "body", "button", "fieldset", "form", "frame", "head",
            "html", "img", "li", "link", "ol", "table", "td", "tr", "ul"]
    def __init__(self):
        self.ie = Dispatch("InternetExplorer.Application")

An example of the problem:
 
>>>myie = ie()
>>>myie.ie.Document.Navigate(http://www.google.com/)
>>>myie.table.length
0
>>>myie.ie.getElementsByTagName("table").length
2
 
A quick note: ie is my wrapper class, while ie.ie is the win32com Dispatch object representing Internet Explorer.
 
My code should be creating a property attribute for each tag in the tags list and binding it to getElementsByTagName() with the tag pre-loaded by the lambda expression.  This means that reading the attribute myie.table should be equivalent to a call to ie.ie.Document.getElementsByTagName("table").  But for some reason, it's not working.  Other property assignments in the metaclass that don't refer to getElementsByTagName work fine (for example, I make ie.url an attribute that navigates to whatever location you assign to it).  Is there any guru who understands these tools out there who can help?
 
In case you're wondering, the reason I brought metaclasses into the picture is that you can't iterate through a list of attribute assignments within the class definition using setattr or __dict__ because the former requires a class name (doesn't exist until the class statement completes) and the latter doesn't seem to exist for classes, at least, I couldn't find a class __dict__.

		
---------------------------------
Do you Yahoo!?
 Meet the all-new My Yahoo! – Try it today! 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20050218/f751c28a/attachment.html


More information about the Python-win32 mailing list