Python Programming on WIN32by Mark Hammond, Andy Robinson
Python is growing in popularity; based on download statistics, there are now over 450,000 people using Python, and more than 150,000 people using Python on Windows. Use of the language has been growing at about 40% per year since 1995, and there is every reason to believe that growth will continue.Despite Python's increasing popularity on Windows, Python
Python is growing in popularity; based on download statistics, there are now over 450,000 people using Python, and more than 150,000 people using Python on Windows. Use of the language has been growing at about 40% per year since 1995, and there is every reason to believe that growth will continue.Despite Python's increasing popularity on Windows, Python Programming on Win32 is the first book to demonstrate how to use it as a serious Windows development and administration tool. Unlike scripting on Unix, Windows scripting involves integrating a number of components, such as COM or the various mail and database APIs, with the Win32 programming interface. While experienced Windows C++ programmers can find their way through the various objects, most people need some guidance, and this book is it. It addresses all the basic technologies for common integration tasks on Windows, explaining both the Windows issues and the Python code you need to glue things together.Topics include:
- The Python language and the PythonWin extensions
- Building a GUI with COM
- Adding a Macro language
- Distributing the application
- Client-side COM for output and data access
- Integration with mail and other internet protocols
- Managing users and drives
- O'Reilly Media, Incorporated
- Publication date:
- Sales rank:
- Product dimensions:
- 7.00(w) x 9.19(h) x 1.29(d)
Read an Excerpt
Chapter 1: IntroductionCase Studies of Python Deployment
Throughout this book we will talk about cases where Python has solved problems in the real world. Both of the authors use Python in their daily work and we will present a couple of examples how we are personally using Python to solve real world problems.
A Japanese Python?
One of the authors is currently working for a global investment company which is internationalizing its core applications to work with Far Eastern markets. The company's client platform is Windows, and core data is stored on Sybase servers and AS400 minicomputers, data flows back and forth among all three platforms continually. All three of these systems represent Japanese characters in totally different ways and work with different character sets. It was necessary not only to develop a library to convert between these encodings, but also to prove that it worked with 100% effectiveness for all the data that might be encountered in future years.
The first stage was to code up the conversions in Python, based on published algorithms and lookup tables. The interactive prompt let us look at the input and output strings very early on and get all of the details right working with single, short strings. We then developed classes to represent character sets' and character maps and fed in the published government character sets - very easy to do with Python's lists and dictionaries. We found subtle holes in published information and were able to correct for them. Having done this, we were able to prove that round-trip conversion was possible in many cases, and to identify the characters which would not survive a round trip in others.
An experienced C++ programmer then wrote a DLL to carry out String translations at high speed. Having a Python prototype allowed us to test the output very early on and compare the output. Python also generated test data sets with every valid character, something that would have taken months by hand. A Python wrapper was written around the DLL, and we wrote scripts to perform heavy-duty tests on it, feeding large customer databases through all possible conversions and back to the start. Naturally the tests uncovered bugs, but we found them all in two days rather than in months.
The DLL was then put to work converting large amounts of report data from mainframe to PC formats. Initially, a Python program used the DLL to perform translations. It would scan for files, decide what to do with them based on the names, break them up, and convert them a field at a time, and manage FTP sessions to send the data on to a destination database server. It also generated a web page for each file translated displaying its contents in an easy-to-read table, along with the results of basic integrity checks. This enabled testing of the data by users on two continents on a daily basis. When the data and algorithms were fully tested and known to be in their final shape, a fairly junior developer with six months experience wrote the eventual C++ program in under a week.
There's a Python on the Scoreboard!
A number of large sports stadiums in Australia (including the two largest with 100,000 person capacities) run custom scoreboard control software during all matches. This software is responsible for keeping and displaying the score for the game (including personal player information), and displaying other messages and advertising during the match. The information is displayed to the huge video scoreboards installed, as well as smaller strip scoreboards located around the ground and locally to the scorers PC using HTML.
The system runs on Windows NT computers, and needs to talk to a variety of custom software and hardware, including custom input device for score keeping and custom hardware to control the video and strip scoreboards. The system also needs to be able to read data during the game from an external database which provides detailed game statistics for each player.
The scoreboard software is written in C++ and Python. The C++ components of the system are responsible for keeping the match score, and maintaining the key score database. All of the scoreboard output functionality is written in Python, and exposes Python as a macro language for the scoreboard operators.
Each output device (for example, the video screen, strip scoreboard or HTML file) has a particular "language" that must be used to control the output. HTML, for example, uses <TAGS>, while the video scoreboard uses a formatting language somewhat similar to postscript. A common thread is that all output formats are text based.
A scheme has been devised that allows the scoreboard operator to embed Python code in the various layout formats. As the format is displayed, the Python code is executed to substitute the actual score. For example, the scoreboard operator may design a HTML page with code similar to
| <P>The player name is <I><%= player.Name %></I>
Anything within the <% tag is considered Python code, and the value substituted at runtime. Thus, this single HTML layout can be used to display the information for any player in the game.
The nature of Python has allowed this to provide features that would not be possible using other languages. One such feature is that the scoreboard operator is free to create new database fields for a player using Microsoft Access and use them in the scoreboard layouts immediately using player.FieldName syntax; thus the object model exposed to the user is actually partially controlled by the user. The use of Python also allows arbitrary code to be executed to control the formatting. For example, the scoreboard operator may use the following HTML to display the list of players in the home team:
|<p>Team <% = home.Name %>
|<% for player in home.Players: %>
|<P><%= player.Name %>
|<% #end %>
This has resulted in a situation programmers strive for, but see all too rarely; a system with enough flexibility that the users are able to do things with your software that you never dreamt could be done!
Other Python' Sightings in the Wild
To further dispel any impressions that Python is new, immature, or unsuited to critical applications, we've included a very small selection of projects and organizations using Python in the real world. These have been culled from a much longer list on the main Python web site., http:www.python.org/
- NASA's Johnson Space Center uses Python as the scripting language for its Integrated Planning System.
- UtraSeek Server, Infoseek's commercial web search engine, is implemented as a Python application, with some C extensions to provide primitive operations for fast, indexing and searching. The core product involves 11,000 lines of Python, and the user interface consists of 17,000 lines of Python-scripted HTML templates.
- The Red Hat Commercial Linux distributions use Python for their installation procedures.
- Caligari Corporation's 3D modeling and animation package, trueSpace 4, uses Python as a scripting language. Users can create custom modelling and animation effects, write interactive applications and develop game prototypes entirely inside trueSpace 4. We'll show you how to do something similar for your own applications in Part 2.
- IBM's East Fishkill factory uses Python to control material entry. exit, and data collection for an entire semiconductor plant.
- Scientists in the Theoretical Physics department of Los Alamos National Laboratory are using Python to control large-scale physics computations on massively parallel high-end servers and clusters. Python plays a central role in controlling simulations, performing data analysis and visualization.
- SMHI, the Swedish civilian weather, hydrological, and oceanographic organization, uses Python extensively to acquire data, analyze it and present it to outside interests such as the media. They are developing a Python-based "Radar Analysis and Visualization Environment" to use with the national network of weather radars.
Meet the Author
Mark Hammond is an independent Microsoft Windows consultant working out of Melbourne, Australia. He studied computer science at the South Australian Institute of Technology (now the University of South Australia), and then worked with several large financial institutions in Australia. He started his consulting operation in 1995. Mark has produced many of the Windows extensions for Python, including PythonWin, Active Scripting, and Active Debugging support, and coauthored the COM framework and extensions. He is also a leading authority on Active Scripting and related technologies and has spoken on this subject at Microsofts three most recent Professional Developers conferences. Apart from being a father to his teenage daughter, having an interest in live music, and providing way-too-many free Python extensions, Mark has no life!
Andy Robinson is a London-based consultant specializing in business analysis, object-oriented design, and Windows development. He studied physics and philosophy, then Japanese studies at Oxford. He spent a year in advertising in Tokyo, two more in investment banking, and a long spell as the finance director of a startup in the sports industry. Observing that in all these positions he always ended up having to rewrite software, he moved to full-time computer consulting four years ago. He is currently helping one of the world's largest fund managers to internationalize their systems to handle Asian languages, developing Python systems for financial analysis, and reporting. Back when Andy had spare time, his passions were track and field, and rock climbing. Right now his two sons, Tim and Harry, are taking up all of his time.
Most Helpful Customer Reviews
See all customer reviews