Chapter 23. Miscellaneous

In this chapter, we will give brief preview introductions to miscellaneous areas of Python programming we did not have time to explore more fully. We hope to eventually develop these into full chapters for future editions of this book.

23.1 Web Services

There are many Web services and applications on the Net, providing a wide variety of services. You will find application programmer interfaces (APIs) from most of the big players today, i.e., Yahoo!, Google, eBay, and Amazon, to name a few. In the past, APIs have been used just to access data using these services; however, today’s APIs are different. They are rich and fully featured, and you are able to actually integrate services into your own personal Web sites and Web pages, commonly known as “Mash-ups.”

This is an area of active interest that we will continue to explore (REST, XML, RSS, etc.), but for now, we are going to take a trip back in time to play around with an older interface that is both useful and has longevity, the stock quote server from Yahoo! at http://finance.yahoo.com.

23.1.1 Yahoo! Finance Stock Quote Server

If you visit the Web site and pull up a quotation for any stock, you will find a Uniform Resource Locator (URL) link under the basic quote data labeled “Download Data,” which lets users download a CSV file suitable for importing into Microsoft Excel or Intuit Quicken:

If your browser’s MIME settings are set correctly, your browser will actually launch Excel with the resulting data. This is due primarily to the final variable (key-value) pair found in the link, e=.csv. This variable is actually not used by the server as it always sends back data in CSV format anyway.

If we use our friend urllib.urlopen(), we see that for any stock ticker symbol, one CSV string is returned:

image

The string would then have to be manually parsed (by stripping the trailing whitespace and splitting on the comma delimiter). As an alternative to parsing the data string ourselves, we can use the csv module, introduced in Python 2.3, which does both the string split and the whitespace strip. Using csv, we can replace the for loop above with the following assuming all other lines are left intact:

image

image

By analyzing the argument field f passed to the server and from reading Yahoo!’s online help for this service, you will see that the symbols (sl1d1t1c1ohgv) correspond to: ticker symbol, last price, date, time, change, open price, daily high, daily low, and volume.

You can get more information by checking the Yahoo! Finance Help pages—just search for “download data” or “download spreadsheet format.” Further analysis of the API reveals a few more options such as the previous closing price, the percentage change of the current price to the previous close, the 52-week high and low, etc. All in all, the options can be summarized in Table 23.1 along with the formats of the returned components.

Table 23.1. Yahoo! Finance Stock Quote Server Parameters

image

The field names are given in the order you want them from the server. Just concatenate them together as a single argument to the field parameter f as part of the requesting URL. As mentioned in the returned value footnote, some of components returned are quoted separately. It is up to the parser to properly extract the data. Observe the resulting (sub)strings when parsed manually vs. using the csv module in our example above. If a value is not available, the quote server returns “N/A” (separately quoted since that field is, which makes it consistent ... a good thing).

For example, if we give the server a field request of f=sl1d1c1p2, we get a string like the following returned for a valid stock ticker:

"YHOO",166.203125,"2/23/2000",+12.390625,"+8.06%"

For the case where the stock is no longer publicly traded, we get something like this instead (note again how fields that come back quoted still do, even if N/A):

"PBLS.OB",0.00,"N/A",N/A,"N/A"

The quote server will also allow you to specify multiple stock ticker symbols, as in s=YHOO,GOOG,EBAY,AMZN. You will get back one row of data like the above for each company. Just keep in mind that “[any] redistribution of quotes data displayed on Yahoo! is strictly prohibited,” as quoted in the Yahoo! Finance Help pages, so you should only be using these data for personal reasons. Also be aware that all of the quotes you download are delayed.

Using what we know now, let us build an example (Example 23.1) application that will read and display some stock quote data for some of our favorite Internet companies.

Example 23.1. Yahoo! Finance Stock Quote Example (stock.py)

This script downloads and displays stock prices from the Yahoo! quote server.

image

If we run this script, we will get output that looks like the following:

image

23.2 Programming Microsoft Office with Win32 COM

One of the most useful things that you can do in an everyday business environment is to integrate support for Win32 applications. Being able to read data from and write data to such applications can often be very handy. Your department may not be necessarily be running in a Win32 environment, but chances are, your management and other project teams are. Mark Hammond’s Windows Extensions for Python allows programmers to interact natively with Win32 applications in their native environment. (It can be downloaded at the book’s Web site.)

The Win32 programming universe is expansive. Most of it available from the Windows Extensions for Python package, i.e., Windows API, processes, Microsoft Foundation Classes (MFC) Graphical User Interface (GUI) development, Windows multithreaded programming, services, remote access, pipes, server-side COM programming, and events. And don’t forget about IronPython (http://codeplex.com/Wiki/View.aspx?ProjectName=IronPython), an implementation of the Python language in C# for the .NET/Mono development environment. In this section, we are going to focus on one part of the Win32 programming universe, which easily has practical applications for client-side, COM programming.

23.2.1 Client-Side COM Programming

We can use Component Object Model, better known as COM (or its marketing name, ActiveX), to communicate with tools such as Outlook and Excel. For programmers, the pleasure comes with being able to “control” a native Office application directly from their Python code.

Specifically, when discussing the use of a COM object, e.g., launching of an application and allowing code to access methods and data of that applications, this is referred to as COM client-side programming. Server-side COM programming is where you are implementing a COM object for clients to access.

Core Note: Python and Microsoft COM (Client-Side) Programming

image

Python on the Windows 32-bit platform contains connectivity to COM, a Microsoft interfacing technology that allows objects to talk to one another, or more higher-level applications to talk to one another, without any language- or format-dependence. We will see in this section how the combination of Python and COM (client programming) presents a unique opportunity to create scripts that can communicate directly with Microsoft Office applications such as Word, Excel, PowerPoint, and Outlook.

The prerequisites to this section include running on a Win32 platform with both Python and the Windows Extensions for Python installed. You must also have one or more Microsoft applications available to try the examples with. The download instructions for the Windows Extensions should be adequate to get your system ready to go. Since PythonWin comes with the Extensions distribution, we recommend IDE for building and testing your Win32 scripts.

In this section, we will show you how you can interact with an Office application. We will present a few examples, some of them quite useful, and describe how they work in detail. You will also find several of these at the “Python Cookbook” Web site. We confess to readers that we are not COM or VisualBasic experts and we are well aware that our examples can be vastly improved. We would like to solicit the readership to drop us a line and send us any comments, suggestions, or improvements that you would consider for the general audience.

Let us start with very simple examples of how to launch and interact with Microsoft Excel, Word, PowerPoint, and Outlook. Before we show you examples of all we have discussed, we want to point out that client-side COM applications all follow similar steps in execution. The typical way in which you would interact with these applications is something like this:

  1. Launch application
  2. Add appropriate document to work on (or load one from disk)
  3. Make application visible (if desired)
  4. Perform all desired work on document
  5. Save or discard document
  6. Quit

Enough talking ... let us take a look at some code. Below are a series of scripts that control a different Microsoft application. All import the win32com. client module as well as a couple of Tk modules to control the launching (and completion) of each application. Also like we did in Chapter 19, we use the .pyw file extension to suppress the unneeded DOS command window.

23.2.2 Microsoft Excel

Our first example is a demonstration using Excel. Of the entire Office suite, we find Excel to be the most programmable. It is quite useful to pass data to Excel so that you can both take advantage of the spreadsheet’s features as well as viewing data in a nice printable format. It is also useful to be able to read data from a spreadsheet and process data with the power of a real programming language like Python. We will present a more complex example using Excel at the end of this section, but we have to start somewhere. So, we start with Example 23.2.

Example 23.2. Excel Example (excel.pyw)

This script launches Excel and writes data to spreadsheet cells.

image

Line-by-Line Explanation

Lines 1–6, 31

We import Tkinter and tkMessageBox only to use the showwarning message box upon termination of the demonstration. We withdraw() the Tk top-level window to suppress it (line 31) before bringing up the dialog box (line 26). If you do not initialize the top level beforehand, one will automatically be created for you; it won’t be withdrawn and will be an annoyance on-screen.

Lines 11–17

After the code starts (or “dispatches”) Excel (an application), we add a workbook (a spreadsheet that contains sheets that the data are written to; these sheets are organized as tabs in a workbook), and grab a handle to the active sheet, meaning the sheet that is displayed. Do not get all worked up about the terminology, which may be confusing mostly because a “spreadsheet contains sheets.”

Core Note: Static and dynamic dispatch

image

On line 13, we use what is known as static dispatch. Before starting up the script, we ran the Makepy utility from PythonWin. (Start the IDE, select Tools ⇒ COM Makepy utility and choose the appropriate application object library.) This utility program creates and caches the objects that are needed for the application. Without this prep work, the objects and attributes will have to be built during runtime; this is known as dynamic dispatch. If you want to run dynamically, then use the regular Dispatch() function:

xl = win32com.client.Dispatch('%s.Application' % app)

The Visible flag must be set to True to make the application visible on your desktop, then pause so that the user can see each step in the demonstration (line 16). For an explanation of the sleep() call on line 17, just read the next paragraph.

Lines 19–24

In the application portion of the script, we write out the title of our demonstration to the first and upper leftmost cell, (A1) or (1, 1). We then skip a row and then write “Line N” where N is numbered from 3 to 7, pausing 1 second in between each row so that users can see our updates happening live. (The cell updates would occur too quickly without the delay.)

Lines 26–32

A warning dialog box comes up after the demo letting the user know that he or she can quit this demo once they have observed the output. The spreadsheet is closed without saving, ss.Close([SaveChanges=]False), and the application exits. Finally, the “main” part of the script just initializes Tk and runs the core part of the application.

Running this script results in an Excel application window, which should look similar to Figure 23-1.

Figure 23-1. Python-to-Excel demonstration script (excel.pyw)

image

23.2.3 Microsoft Word

The next demonstration is with Word. Using Word for documents is not as applicable to the programming world as there is not much data involved. One could consider using Word for generating form letters, however. In Example 23.3, we create a document by simply writing one line of text after another.

Example 23.3. Word Example (word.pyw)

This script launches Word and writes data to the document.

image

The Word example follows pretty much the same script as the Excel example. The only difference is that instead of writing in cells, we have to insert the strings into the text “range” of our document and move the cursor forward after each write. We also have to manually provide the line termination characters of carriage RETURN following by NEWLINE ( ).

If we run this script, a resulting screen might look like Figure 23-2.

Figure 23-2. Python-to-Word demonstration script (word.pyw)

image

23.2.4 Microsoft PowerPoint

Applying PowerPoint in an application may not seem commonplace, but you could consider using it when you are rushed to make a presentation. You can create your bullet points in a text file on the plane, then upon arrival at the hotel that evening, use a script that parses the file and auto-generates a set of slides. You can further enhance those slides by adding in a background, animation, etc., all of which are possible through the COM interface. Another use case would be if you had to auto-generate or modify new or existing presentations. You can create a COM script controlled via a shell script to create and tweak each presentation generated. Okay, enough speculation ... now let us take a look at our PowerPoint example (i.e., Example 23.4).

Example 23.4. PowerPoint Example (ppoint.pyw)

This script launches PowerPoint and writes data to the “shapes” on a slide.

image

Again, you will notice similarities to both the Excel and Word demonstrations above. Where PowerPoint differs is in the objects you write data to. Instead of a single active sheet or document, PowerPoint is somewhat trickier because each presentation slide can have a different layout. With a presentation, you have multiple slides, and each slide can have a different layout. (Recent versions of PowerPoint have 30 different layouts!) The actions you can perform on a slide depend on which layout you have chosen for each page.

In our example, we just use a (title and) text layout (line 17) and fill in the main title (lines 19–20), Shape[0] or Shape(1)—Python sequences begin at index 0 while Microsoft software starts at 1—, and the text portion (lines 22–26), Shape[1] or Shape(2). To figure out which constant to use, you will need a list of all the ones available to you. For example, ppLayoutText is defined as a constant with a value of 2 (integer), ppLayoutTitle is 1, etc. You can find the constants in most Microsoft VB/Office programming books or online by just searching on the names. Alternatively, you can just use the integer constants without having to name them via win32.constants.

The PowerPoint screenshot is shown in Figure 23-3.

Figure 23-3. Python-to-PowerPoint demonstration script (ppoint.pyw)

image

23.2.5 Microsoft Outlook

Finally, we give an Outlook demonstration, which uses even more constants than PowerPoint. As a fairly common and versatile tool, use of Outlook in an application makes sense, like it does for Excel. There are always e-mail addresses, messages, and other data that can be easily manipulated in a Python program. Example 23.5 is an Outlook example that does a little bit more than our previous examples.

Example 23.5. Outlook Example (olook.pyw)

This script launches Outlook, creates a new message, “sends” it, and lets you view it by opening and displaying both the Outbox and the message itself.

image

In this example, we use Outlook to send an e-mail to ourselves. In order to make the demonstration work, you need to turn off your network access so that you do not really send the message, and thus are able to view it in your Outbox folder (and delete it if desired after viewing it). After launching Outlook, we create a new mail message and fill out the various fields such as recipient, subject, and body (lines 15–21). We then call the send() method (line 22) to spool the message to the Outbox where it will be moved to “Sent Mail” once the message has actually been transmitted to the mail server.

Like PowerPoint, there are many constants available ... olMailItem (with a constant value of 0) is the one used for e-mail messages. Other popular Outlook items include olAppointmentItem (1), olContactItem (2), and olTaskItem (3). Of course, there are more, so you will have to find a VB/Office programming book or search for the constants and their values online.

In the next section (lines 24–27), we use another constant, olFolderOutbox (4), to open the Outbox folder and bring it up for display. We find the most recent item (hopefully the one we just created) and display it as well. Other popular folders include: olFolderInbox (6), olFolderCalendar (9), olFolderContacts (10), olFolderDrafts (16), olFolderSentMail (5), and olFolderTasks (13). If you use dynamic dispatch, you will likely have to use the numeric values instead of the constants’ names (see previous Core Note).

Figure 23-4 shows a screen capture of just the message window.

Figure 23-4. Python-to-Outlook demonstration script (olook.pyw)

image

Before we get this far, however, from its history we know that Outlook has been vulnerable to all kinds of attacks, so Microsoft has built some protection into Outlook for restricting access to your address book and being able to send mail on your behalf. When attempting to access your Outlook data, the screen shown in Figure 23-5 pops up where you must explicitly give permission to an outside program.

Figure 23-5. Outlook address book access warning

image

Then when you are trying to send a message from an external program, you get the warning dialog shown in Figure 23-6, where you have to wait until the timer expires before you are allowed to select “Yes”.

Figure 23-6. Outlook e-mail transmission warning

image

Once you pass all the security checks, everything else should work smoothly. There is software available to help get you around these checks but they have to be downloaded and installed separately.

On this book’s Web site at http://corepython.com, you will find an alternative script that combines these four smaller ones into a single application that lets users choose which of these demonstrations to run.

23.2.6 Intermediate Example

Now that we have gotten a good taste of Office programming, let us build a more useful application by combining the material from this section with that of the Web Services section. If we were to combine the stock quote example with our Excel demonstration script, we would get an application that would download stock quotes from the net and pop them directly into Excel without having to create or use CSV files as a medium (see Example 23.6).

Example 23.6. Stock Quote and Excel Example (estock.pyw)

This script downloads stock quotes from Yahoo! and writes the data to Excel.

image

Line-by-Line Explanation

Lines 1–13

We import all of the attributes from both the original Web services stock quote (stock.py) above and Excel scripts here as well as define the same constants.

Lines 15–32

The first part of the core function launches Excel as seen earlier (lines 17–21). The title and timestamp are then written to cells (lines 23–29), along with the column headings, which are then bolded (line 30). The remaining cells are dedicated to writing the actual stock quote data, starting in row 6 (line 32).

Lines 34–43

We open the URL as before (line 34), but instead of just writing the data to standard output, we fill in the spreadsheet cells, one column of data at a time, and one company per row (lines 35–42).

Lines 45–51

The remaining lines of our script mirror code that we have seen before.

Figure 23-7 shows a window with real data after executing our script.

Figure 23-7. Python-to-Excel stock quote demonstration script (estock.pyw)

image

Note that the data columns lose the original formatting of the numeric strings because Excel stores them as numbers using the default cell format. We lose the formatting of the numbers to two places after the decimal point, e.g., “34.2” is displayed even though Python passed in “34.20”; and for the “change from previous close column,” we lose not only the decimal places but also the plus sign ( + ) indicating a positive change in value. (Compare the output in Excel to the output from the original text version [stock.py]. These problems will be addressed by an exercise at chapter’s end.)

23.3 Python and Java Programming with Jython

23.3.1 What Is Jython?

Jython is one of those tools that can unite two diverse programming populations. For one, it caters to Python programmers embedded in a Java development environment and gives them the ability to rapidly prototype solutions that seamlessly integrate into an existing Java platform. Another reason is that it helps simplify the lives of millions of Java programmers out there by giving Java a scripting language environment. No longer do Java programmers have to write a test harness or driver application to simply test a class they have just written.

Jython gives you most of what Python has to offer along with the ability to instantiate and interact with Java classes too! Jython code is dynamically compiled into Java bytecode, plus you can extend Java classes in Jython. You can also extend Python using Java. It is quite easy to write a class in Python and then use it as a Java class. You can always statically compile a Jython script into Java bytecode.

Jython can be downloaded from the book’s Web site or at http://jython.org. After installation and seeing some default startup notices of processing new .jar files, starting up Jython’s interactive interpreter looks eerily like you’re using Python. And yes, Virginia, you can still do the same old “Hello World!” in Python:

image

The only difference is that you now have (to wait for) Java’s long startup time. Once you have accepted that inevitability, you can move on to greater things. The more interesting thing about the Jython interactive interpreter is that now you can do “Hello World!” using Java(!):

image

Java gives Python users the added bonuses of native exception handling (not available in standard Python, or “CPython” as it is called, when being referred to among other implementations) as well as use of Java’s own garbage collector (so Python’s did not have to be [re]implemented for Java).

23.3.2 Swing GUI Development (Java or Python!)

By having access to all Java classes, we have a much broader universe of what is possible. One example is GUI development. In Python, we have the default GUI of Tk via the Tkinter module, but Tk is not a native Python toolkit. However, Java does have Swing, and it is native. With Jython, we can actually write a GUI application using Swing components ... not with Java, but using Python.

A simple “Hello World!” GUI written in Java followed by its equivalent in Python is given in Examples 23.7 and 23.8, respectively, both of which mimic the Tk examples tkhello3.py found earlier in the GUI programming chapter. These programs are called swhello.java and swhello.py, respectively.

Example 23.7. Swing “Hello World” in Java (swhello.java)

This program creates a GUI just like tkhello3.py but uses Swing instead of Tk. It is written in Java.

image

Example 23.8. Swing “Hello World” in Python (swhello.py)

This is an equivalent Python script to the above Java program and executed with the Jython interpreter.

image

The code for both matches that of tkhello3.py except they use Swing instead of Tk. The hallmark of the Python version is the significant reduction in the number of lines of code necessary to do the same thing in Java. The Python code is more expressive, with each line of code having more significance. In short, there is less “white noise.” Java code tends to have a lot more boilerplate code to get work done, while Python lets you concentrate on the important parts of your application: the solution to the problem you are trying to solve.

Since both applications are compiled to Java bytecode, it is no surprise that both applications look exactly alike when executing on the same platform (see Figure 23-8).

Figure 23-8. Swing Hello World Demonstration Scripts (swhello.{java,py})

image

Jython is a great development tool because you get the expressiveness of Python plus the rich API in the Java libraries. If you are a current Java developer, we hope that we have whet your appetite in terms of what you can now do with the power of Python behind you. If you are new to Java, Jython will be able to ease you in gently. You can prototype in Jython, then port easily to Java as necessary.

23.4 Exercises

Web Services

23-1. Web Services. Take the Yahoo! stock quote example (stock.py) and change the application to save the quote data to a file instead of displaying it to the screen. Optional: You may change the script so that users can choose to display the quote data or save it to a file.

23-2. Web Services. Update the Yahoo! stock quote example (stock.py) to download other stock quote data given the additional parameters listed above. Optional: You may add this feature to your solution to the above exercise.

23-3. Web Services and the csv Module. Convert stock.py to using the csv module to parse the incoming data, like we did in the example code snippet. Extra Credit: Do the same thing to the Excel version of this script (estock.py).

23-4. REST and Web Services. Study how REST and XML are used in more modern-day Web services APIs and applications. Describe the additional functionality you get over older systems like the Yahoo! quote server, which uses URL parameters.

23-5. REST and Web Services. Build an application framework using Python’s support for REST and XML that will allow you to share and reuse this code when writing applications that use any of the newer Web services and APIs available today. Display your code using APIs from Yahoo!, Google, eBay, and/or Amazon.

Microsoft Office Programming

23-6. Microsoft Excel and Web Pages. Create an application that will read data from an Excel spreadsheet and map all of it to an equivalent HTML table. (You may use the third-party HTMLgen module if desired.)

23-7. Microsoft Office Applications and Web Services. Interface to any existing Web service, whether REST- or URL-based, and write data to an Excel spreadsheet or format the data nicely into a Word document. Format them properly for printing. Extra Credit: Support both Excel and Word.

23-8. Microsoft Outlook and Web Services. Similar to the previous problem, do the same thing, but put the data into a new e-mail message that you send with Outlook. Extra Credit: Do the same thing but send the e-mail with regular SMTP instead. (You may wish to refer to Chapter 17 on Internet Client Programming.)

23-9. Microsoft PowerPoint. Design a presentation slide creator. Design the specification of a text file that users will create with Word or a normal text editor. Using the specification format, read in the presentation data and create the appropriate PowerPoint slides all as part of a single presentation.

23-10. Microsoft Outlook, Databases, and Your Address Book. Write a program that will extract the contents of an Outlook address book and store the desired fields into a database. The database can be a text file, DBM file, or even an RDBMS. (You may wish to refer to Chapter 21, Database Programming.) Extra Credit: Do the reverse ... read in contact information from a database (or allow for direct user input) and create or update records in Outlook.

23-11. Microsoft Outlook and E-mail. Develop a program that backs up your e-mail by taking the contents of your Inbox and/or other important folders and saves them in (as close to) regular “box” format to disk.

23-12. Microsoft Outlook Calendar. Write a simple script that creates new Outlook appointments. Take at least the following as user input: start date and time, appointment name or subject, and duration of appointment.

23-13. Microsoft Outlook Calendar. Build an application that dumps the contents of your appointments to a destination of your choice, i.e., to the screen, to a database, to Excel, etc. Extra Credit: Do the same thing to your set of Outlook tasks.

23-14. Multithreading. Update the Excel version of the stock quote download script (estock.pyw) so that the downloads of data happen “concurrently” using multiple Python threads. Optional: You may also try this exercise with Visual C++ threads using win32process.beginthreadex().

23-15. Excel Cell Formatting. In the spreadsheet version of the stock quote download script (estock.pyw), we saw in Figure 23-7 how the stock price does not default to two places after the decimal point even if we pass in a string with the trailing zero(s). When Excel converts it to a number, it uses the default setting for the number format.

(a) Change the numeric format to correctly go out to two decimal places by changing the cell’s NumberFormat attribute to “0.00.”

(b) We can also saw that the “change from previous close” column loses the “+” in addition to the decimal point formatting. However, we discover that making the correction in part (a) to both columns only solves the decimal place problem... the plus sign is automatically dropped for any number. The solution here is to change this column to be text instead of a number. You can do this by changing the cell’s NumberFormat attribute to “@.”

(c) By changing the cell’s numeric format to text, however, we lose the right alignment that comes automatically with numbers. In addition to your solution to part (b), you must also now set the cell’s HorizontalAlignment attribute to the Win32 Excel constant xlRight. After you come up with the solutions to all three parts, your output will now look more acceptable, as shown in Figure 23-9.

Figure 23-9. Improving the Python-to-Excel stock quote script (estock.pyw)

image

Java, Python, Jython

23-16. Jython. What is the difference between Jython and CPython?

23-17. Java and Python. Take an existing Java application and port it to Python. Write down your experience in a journal. When complete, give an executive summary of what has to be accomplished, what some of the important steps are, and what common operations you have to perform to make it happen.

23-18. Java and Python. Study the Jython source code. Describe how some of Python standard types are implemented in Java.

23-19. Java and Python. Extend Python by writing an extension in Java. What are the necessary steps? Demonstrate your working solution by showing how it works in with the Jython interactive interpreter.

23-20. Jython and Databases. Find an interesting exercise from the Chapter 21 and port it to Jython. One of the best things about Jython is that starting in 2.1, it now comes with a JDBC database module called zxJDBC that is nearly Python DB-API 2.0-compliant.

23-21. Python and Jython. Find a Python module not available in Jython (yet) and port it. Consider submitting it as a patch to the Jython distribution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset