Using Python to Harness Windows.doc

(1947 KB) Pobierz
Using Python To Harness Windows

Robinson Analytics

 

 

 

Using Python to Harness Windows

 

 

 

 

 

Tutorial Notes

 

O’Reilly Python Conference, Monterey, 21-24 August 1999

 

Andy Robinson, Robinson Analytics Ltd.

 

 

 

 

 

 

 

 

 

 

 

 

 

These notes closely follow the slides for the tutorial and include all code samples.


Table of Contents

 

 

 

Table of Contents              2

1              Background              4

2              What is Python good for on Windows?              5

3              How Python works on Windows              6

4              The Pythonwin IDE              9

5              Introduction to Python and COM              16

6              Adding a Macro Language              31

7              Client Side COM and Excel              39

8              Automating Word              45

9              Distributing our Application with DCOM              52

10              Database Access              54

11              Communications              61

12              System Administration              63

13              Active Scripting              64

14              GUI Development              66

15              Delphi              68

16              C and C++  Level Integration              69


 

 

 

 

 

 

 

Part 1: - Fundamentals

 

                            PythonWindowsTutorial.doc


Using Python To Harness Windows

1              Background

1.1        What we will cover

·         How Python works on Windows

·         What’s in Pythonwin

·         Building applications with Python and COM

·         Getting started on common tasks

·         Automating Office applications

·         Connecting to databases

·         Communications

·         GUI libraries

1.2        What we won’t cover

·         Low-level Windows internals

·         Hardcore COM - how it works

·         NT Services

·         NT processes, events and threading models

2              What is Python good for on Windows?

2.1        An integration tool

·         Works with files

·         Works with DLLs and C programs

·         Works with COM

·         Works with Networks

·         Works with Distributed Objects

2.2        “Low-threat” needs that Python fills in the corporate world

·         Adding a macro language to applications

·         Rapid Prototyping of object models and algorithms

·         Building test harnesses for other systems

·         Data Cleaning and Transformation

·         Python as Glue

 

3              How Python works on Windows

3.1        Installation and setup

Two files to download from http://www.python.org/download/download_windows.html:

·         py152.exe – Python itself

·         win32all.exe – Windows extensions

What you end up with:

 

 

 

3.2       
The Python Core on Windows

python15.dll – 545kb, the language, exports almost everything

python.exe – 5kb console mode program

pythonw.exe – 6kb non-console mode program – avoids ugly black DOS boxes when you don’t want standard input/outpu

 

Note: some people like to copy python.exe and pythonw.exe to their system directory, especially on Win95/98

Extensions and their meaning

.py              Python source.

.pyc              “Compiled” python source

.pyd              Extension module written in C – actually a DLL which has been renamed to .pyd

.pyw              (advanced) – a Python source file you wish to have run with pythonw.exe, not python.exe.

py, pyx and pyw all runnable with double-click (or right-click and choose Run).

 

Working with the command prompt on Win95/98

You need Python on your path, or a doskey macro!

 

C:\Scripts> doskey p="C:\Program Files\Python\Python.exe" $*

C:\Scripts>p hello.py

Hello from Python

 

C:\Scripts>doskey n=start notepad.exe $*

C:\Scripts>doskey pw=start pythonwin.exe $*

C:\Scripts>n hello.py

C:\Scripts>pw hello.py

Note also that you can drag filenames and directories from explorer into MSDOS window.


Working with the command prompt on NT

Much nicer!  Readline-like recall with up and down arrows.

NT knows what a py file is, so you can type:

C:\Scripts>hello.py

Hello from Python

 

C:\Scripts>

You can go one further with the PATHEXT variable.  To kmake it permanent, go to Control Panel | System | Environment:

C:\Scripts>echo %PATHEXT%

.exe;.bat;.cmd

C:\Scripts>set PATHEXT=%PATHEXT%;.py

C:\Scripts>echo %PATHEXT%

.exe;.bat;.cmd;.py

C:\Scripts>hello

Hello from Python

 

C:\Scripts>

..and of course you can use NT’s other command line tools, like the scheduler to run Python jobs.

3.3        The Python for Windows Extensions

win32all includes:

·         the win32 extensions

·         the Pythonwin editor and MFC framework

·         The PythonCOM framework

·         Lots of help and examples

4              The Pythonwin IDE

Pythonwin 2.0:

 

Key features:

·         C editor component

·         Syntax coloring

·         drop-down completion (as far as is possible in Python) and argument lists

·         class and function browser which operates across modules

 

4.1       
Modes

Pythonwin support a number of command line parameters:

Command Line

Description

/edit filename

Starts Pythonwin, and opens the named file for editing

/run filename

Starts Pythonwin, and runs the specified script.

/nodde

...
Zgłoś jeśli naruszono regulamin