Home arrow Python arrow Page 2 - Windows Programming in Python: Creating COM Servers

Developing COM Servers Step by Step - Python

In an earlier article I discussed accessing COM components from within Python programs. However, I left a question dangling, namely, can COM servers be created in Python, and can they be accessed by applications created in other languages or platforms such as Visual Basic? The answer is an emphatic yes.

TABLE OF CONTENTS:
  1. Windows Programming in Python: Creating COM Servers
  2. Developing COM Servers Step by Step
  3. Annotating the Class with Attributes
  4. Creating COM Servers in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 8
January 22, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Developing a COM server in Python is easier than in any other language. If the process is broken down into steps there are primarily two steps, which are developing the class and annotating the class with attributes. Furthermore, in the first step you must decide on which model the class must be based. 

Developing the class

The philosophy of COM is that a fixed interface must be built and the class must be modeled around it. In essence, once the interface has been defined, it must not be changed. To work with this philosophy there are three design patterns that can be used. They are:

  • COM base class, pure Python subclass
  • Pure Python base class, COM subclass
  • COM interface, Python delegate

The COM interface, Python delegate pattern is the most commonly used. The reason for this is the ability to develop the class first.

In the pattern of a COM base class, pure Python subclass, a base class has to be defined and exposed as a COM server. The methods in the base class perform no tasks. The base class works as a COM interface. Then a sub class, which implements the base class, has to be developed; this class provides the actual services. In other words a class that implements the base class performs the tasks. This pattern is most appropriate when designing a class whose main function is to be used from COM and not from Python.

Now let's look at a pure Python base class, COM subclass pattern. It is in a way the opposite of the previous pattern. Here, the existing COM class is inherited from a Python base class. The COM class can be differentiated by the the attributes used to annotate the class, which will be discussed shortly.

Finally, let's examine the COM interface, Python delegate pattern. In this case, the COM interface is defined. The class that defines the COM interface has variables internal to it that point to the pure Python counterpart. The Python counterpart is known as the delegate. The methods of class that represent the COM interface translate their arguments, return values as needed, and forward them to the delegate.

Let's take the third pattern as an example. For the sake of the example, suppose there is a BookSet class that implements the methods of the COM interface. Now if the third pattern is applied to define a COM interface, it would look like this:

class COMBookSet:
  _reg_clsid_ = '{38CB8241-D698-11D2-B806-0060974AB8A9}'
  _reg_progid_ = 'Doubletalk.BookServer'
  _public_methods_ = ['double']

  def __init__(self):
    self.__BookSet = doubletalk.bookset.BookSet()

  def double(self, arg):
    # trivial test function to check it's alive
    return arg * 2

The __BookSet variable points to the pure Python class BookSet. This is how it delegates the task to the pure Python class. You will observe that there are certain attributes such as _reg_clsid_. The explanation of these attributes is coming up next.



 
 
>>> More Python Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: