Using Python To Harness Windows
Advanced ? ? ?
map from one accounts structure to another analyse and trace cash flows Multidimensional analysis
5.10 What we’d like…
Page 21 of 71
6795.doc
Using Python To Harness Windows
5.11 Design Patterns for the COM Server
COM servers and Python apps handle some arg types differently… ? Unicode String Handling – Gotcha Number One! (hopefully goes in 1.6) # our ordinary save method for use from Python def save(self, filename): f = open(filename,'wb') cPickle.dump(self.__journal,f) f.close() # what we would need for use from COM def save(self, unicode_filename): # convert it to a python string: python_filename = str(unicode_filename) f = open(python_filename,'wb') cPickle.dump(self.__journal,f) f.close() ?
…so a single class not the best design for real apps. Others options: ? ? ?
We go for option 3: Delegate. Keeps our Python package pure and portable.
Startup Code: # comservers.py – to be expanded 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 is alive return arg * 2 if __name__ == '__main__': win32com.server.register.UseCommandLine(COMBookSet)
COM Base Class, Python Server Pure Python Base Class, COM Subclass COM interface, Python Delegate Wrap/Unwrap subobjects
Page 22 of 71
6795.doc
Using Python To Harness Windows
5.12 Visual Basic GUI Startup Code
Public BookServer As Object Private Sub MDIForm_Load() InitCOMServer frmJournal.Show End Sub Private Sub MDIForm_Unload(Cancel As Integer) CloseCOMServer End Sub Sub InitCOMServer() 'called when the program starts On Error GoTo InitCOMServer_error Set BookServer = CreateObject(\ Exit Sub InitCOMServer_error: Dim msg As String msg = \BookServer.\ \the Python \ \ MsgBox msg End End Sub Sub CloseCOMServer() Set BookServer = Nothing End Sub Sub TestCOMServer() 'just to check it is alive Dim hopefully_four As Integer hopefully_four = BookServer.Double(2) MsgBox \alive\End Sub Private Sub mnuToolsTestServer_Click() 'this helps establish if the COM server is alive 'using a minimal diagnostic function in the modMain module TestCOMServer End Sub With a little luck…
Page 23 of 71
6795.doc
Using Python To Harness Windows
5.13 Our first view – The Journal
Goal:
Date-Ordered List of Transactions
Python Code Needed:
# more methods for COMBookSet – must be named in _public_methods_ def load(self, filename): self.__BookSet.load(str(filename)) def count(self): # return number of transactions return len(self.__BookSet) def getTransactionString(self, index): return self.__BookSet[index].asString() Visual Basic Code – File / Open handler
Private Sub mnuFileOpen_Click() Dim sFile As String With dlgCommonDialog .DialogTitle = \ .CancelError = False 'ToDo: set the flags and attributes of the common dialog control .Filter = \ .ShowOpen If Len(.FileName) = 0 Then Exit Sub End If sFile = .FileName End With BookServer.Load sFile 'display something helpful in the Journal caption frmJournal.Caption = sFile & \Transactions\End Sub
Page 24 of 71
6795.doc