How It Works Elx
In this example, you can pass any command-line arguments to the script. The 1 2 3 4 shown here just helps to see the arguments held in the sys.argv property. The sys.path property holds a very small number of directories, especially when compared to the standard C-Python distribution. For example, you can run the same script with the python interpreter as shown here python jysys.py 1 2 3 4 Python sys.path ' Users ericfj writing python', ' System Library Frameworks Python. ' System Library...
Working with the Python DB API
Once you have a connection, you can use the same techniques shown in Chapter 14. The zxJDBC module provides a DB 2.0 API-compliant driver. Well, mostly compliant. For example, you can create a database table using the following code cursor.execute create table user userid integer, username varchar, firstname varchar, lastname varchar, phone varchar index userid on user userid After creating a table, you can insert rows using code like the following insert into user values Be sure to commit any...
Glade a GUI Builder for pyGTK
The great thing about pyGTK is that you almost never have to write the GUI by hand. That's what Glade is for. Glade is a GUI construction kit It provides a GUI you can use to design your own GUI. Once you're done, it writes a description of a GUI layout to an XML file see Chapter 15 for more information about XML . A library called libglade can then read that XML file and render the corresponding GUI. Instead of instantiating a bunch of Python objects and calling show on all of them, you just...
Characteristics of REST
Much of this chapter is dedicated to writing applications that use the features of the REST architecture to best advantage. As a first step toward learning about those features, here's a brief look at some of the main aspects of REST. Fielding's dissertation on architectural styles and REST is available at www.ics.uci. Chapter 5 describes REST. Introductions that are more informal are available at the REST Wiki, at http rest. blueoxen.net , and at the Wikipedia entry for REST, at http...
A Hierarchical Markup Language
At the core of XML is a simple hierarchical markup language. Tags are used to mark off sections of content with different semantic meanings, and attributes are used to add metadata about the content. Following is an example of a simple XML document that could be used to describe a library lt xml version 1.0 gt lt library owner John Q. Reader gt lt book gt lt title gt Sandman Volume 1 Preludes and Nocturnes lt title gt lt author gt Neil Gaiman lt author gt lt book gt lt book gt lt title gt Good...
Exercises
1. In the Python shell, type the string, Rock a by baby, n ton the tree top, t twhen the wind blows n t t t the cradle will drop. Experiment with the number and placement of the t and n escape sequences and see how this affects what you see. What do you think will happen 2. In the Python shell, use the same string indicated in the prior exercise, but display the string using the print function. Again, play with the number and location of the n and t escape sequences. What do you think will...
Creating a Real Glade Application
Of course, the Glade version of our two-button application doesn't do anything, any more than the version that just used Python code did. In this section, we'll create a complex GUI, with some signal handlers, for an application called pyRAP. This is a chat-themed GUI that could be used as a client for the Python Chat Server described in Chapter 16. Create a new Glade project called PyRAP, and create a new window as shown previously. To create a basic GUI, start with a Vertical Box widget, also...
SocketServer
Sockets are very useful, but Python isn't satisfied with providing the same C-based socket interface you can get with most languages on most operating systems. Python goes one step further and provides SocketServer, a module full of classes that let you write sophisticated socket-based servers with very little code. Most of the work in building a SocketServer is defining a request handler class. This is a subclass of the SocketServer module's BaseRequestHandler class, and the purpose of each...
Design of the Python Chat Server
In IRC, a client that connects to a server must provide a nickname a short string identifying the person who wants to chat. A nickname must be unique across a server so that users can't impersonate one another. Our server will carry on this tradition. An IRC server provides an unlimited number of named channels, or rooms, and each user can join any number of rooms. Our server will provide only a single, unnamed room, which all connected users will inhabit. Entering a line of text in an IRC...
PyGTK Introduction
GUIs are not as simple as they look. Once you've understood the basic concepts, however, you'll find them understandable, and proper program design will help you navigate around the major roadblocks. The author's experience with GUI toolkits, and with pyGTK specifically, stems from developing Immunity CANVAS, a cross-platform commercial product written completely in Python. Note that the same techniques described here are the basis for the new large projects being written by the Ximian team now...
Back to the wftk
For the third section of example programming, look at the wftk again, this time in its originally intended capacity of being a workflow toolkit. Essentially, there are two things to keep in mind about applications of the wftk's workflow functionality First, workflow is activated when events occur in the system basically, the addition or modification of objects. After a list is defined, you can define a workflow to be activated when an object is added for instance, the addition could contain...
Advanced Widgets
Not all widgets are as easy to use as simple Entry Boxes or Spin Buttons. As you've seen with the text view in the previous demo, some widgets require initialization to use. A large part of this initialization requirement is that the widgets themselves are portals into the data set, not the data set itself. For example, you might have multiple text views, each representing different parts of the same text buffer. Classical GUI design text refers to this as the model, view, controller design. It...
Exercises 1
Do the following four exercises in codeEditor and save the results in a file called ch2_exercises.py. You can run it from within codeEditor by selecting and dragging everything into the Python shell. 1. In the Python shell, multiply 5 and 10. Try this with other numbers as well. 2. In the Python shell, divide 122 by 23. Now divide 122.0 by 23. Try multiplying floats and integers using other numbers. 3. Print every number from 6 through 14 in base 8. 4. Print every number from 9 through 19 in...
How It Works Fgn
Now that you've defined new methods, the f object can use them. When you re-created f with the eggs, milk, and cheese you made the object out of the new Fridge class, so it has the new methods you've added available to it. Finally, it's time for the methods to get items from the Fridge. Here you can do the same thing you did for the methods to add to the Fridge, focusing on a single method that will take care of the hard stuff and letting the interface methods rely on this hard-working guy def...
The Python Chat Server
For the mirror server, the capability to support multiple simultaneous connections is useful but it doesn't change what the server actually does. Each client interacts only with the server, and not even indirectly with the other clients. This model is a popular one web servers and mail servers use it, among others. There is another type of server, though, that exists to connect clients to each other. For many applications, it's not the server that's interesting it's who else is connected to it....
How It Works Ngs
From here on out, everything indented will be available through the objects created inside of this class. You've already seen this with functions in Chapter 5, and similar rules apply to classes. Note that you have the option for the built-in docstring with classes, as you do with functions. They behave the same way and are very useful for providing an easy way to get information about the class. You should try creating the Fridge class as shown in the preceding example. Note that a capital F...
Multithreaded Servers
One problem with both of these implementations of the mirror server is that only one client at a time can connect to a running server. If you open two telnet sessions to a running server, the second session won't finish connecting until you close the first one. If real servers worked this way, nothing would ever get done. That's why most real servers spawn threads or subprocesses to handle multiple connections. The SocketServer module defines two useful classes for handling multiple connections...
