PythonWindowsTutorial 下载本文

Using Python To Harness Windows

5.3 A Minimal COM Server

# SimpleCOMServer.py - almost as small as they come! class PythonUtilities: _public_methods_ = [ 'SplitString' ] _reg_progid_ = \ # NEVER copy the following ID # Use \ _reg_clsid_ = \ def SplitString(self, val, item=None): import string if item != None: item = str(item) return string.split(str(val), item) # Add code so that when this script is run by Python.exe, it self-registers. if __name__=='__main__': print \ import win32com.server.register win32com.server.register.UseCommandLine(PythonUtilities)

5.4 Using the minimal server from VB or VBA

Page 17 of 71

6795.doc

Using Python To Harness Windows

5.5 Why write Python COM Servers?

? ?

Easiest way to expose Python functionality to your own or other applications Python is best at the business logic, other tools are best at other things (e.g. VB GUIs)

5.6 Doubletalk – Sample Application

Python Financial Modelling Toolkit. Models “Sets of Books” and “Transactions”

Good candidate for this architecture because ? ?

Very wide general applicability – from local data input app to back-office server

Every company needs to customize it a little!

How could we sell it? ? ? ? ? ? ? ? ? ?

100% Native Windows GUI

Distributed, Dynamic Multi-tier Network Architecture

Embedded Scripting Language – lets you customize the way it works! Extensible Plug-In Architecture Command Prompt for Power Users Integration with Word and Excel Open Database Connectivity

Option to run critical tasks on Unix servers without changing a line of code! Totally Buzzword Compliant!

Page 18 of 71

6795.doc

Using Python To Harness Windows

Now to discuss what the app is about:

5.7 Transactions

Crudely, a movement of money.

All accounts must sum to zero!

Simple two-line (“Double-Entry”) Date: 01/01/1998 Comment: Start the company Cash +10 000 Share Capital -10 000

Multi-line Date: 10/03/1999 Comment: Sell Widgets Cash +117.50 Sales Category 1 -50.00 Sales Category 2 -30.00 Sales Category 3 -20.00 Sales tax on all three (owed to Customs & Excise) -17.50

Functionality: ? Store ? Edit ? Add ? Validate

? effectOn(self, account) ? Extra keys/values

? add, multiply – an algebra for financial transactions!

Page 19 of 71

6795.doc

Using Python To Harness Windows

5.8 Accounts

Accounts form a tree – this is the “Balance Sheet”…

? ?

Represent tree as dotted string notation: “MyCo.Assets.Cash.PiggyBank” Assets, Cash and Expenditure are positive; Liabilities, Income and Profit are negative.

5.9 BookSets

A wrapper around a list of transactions. ? ?

Fundamental change operations ? ?

Querying ? ? ? ?

get history of an account get the ?tree of accounts?

get all balances on date -> Balance Sheet report

get all changes between two dates -> Profit & Loss reports Add/Edit/Delete transactions Rename Account

Load/Save with cPickle (one of Python?s killer features!) Import/Export ASCII text, list/dictionary/tuple structures etc.

Page 20 of 71

6795.doc