Building a Wiki

With a basic knowledge of REST, the architecture of the Web and CGI, the main way of hooking up programs to that architecture, you're ready to design and build a basic application. The next few pages detail the construction of a simple content management system called a wiki. The wiki was invented in 1995 by Ward Cunningham and is best known today as the base for Wikipedia (www.wikipedia.org), a free online encyclopedia (see Figure 20-4). Cunningham's original wiki (http c2.com cgi wiki ) is...

Example Rotating Files

In this example, you tackle a more difficult real-world file management task. Suppose that you need to keep old versions of a file around. For instance, system administrators will keep old versions of system log files. Often, older versions of a file are named with a numerical suffix for instance, web.log.1, web.log.2, and so on in which a larger number indicates an older version. To make room for a new version of the file, the old versions are rotated The current version of web.log becomes...

Exposing a SOAP Interface to Bitty Wiki

In principle, there's no reason why you shouldn't be able to run a SOAP server from a CGI script Remember that despite all the additional complexity and mystique of SOAP, it's just like REST and XML-RPC in that it's just a document being POSTed to a URL and another document being sent in return. Unfortunately, SOAPpy doesn't provide a CGI script that serves SOAP requests, only a standalone server, SOAPServer. ZSI, the other SOAP implementation for Python, does offer a CGI-based server. The...

Generating Iterators for Loops

Python has a special feature that enables you to create iterators the range function This code produces an obvious- looking result > > > print(*f) By itself, this doesn't seem profound, but it is essential for situations when you need to use a for loop that will continue for a specific number of iterations, and that isn't based on an existing list and this number may not be determined at the time when the program was written, but it becomes known only when the program is already running....

Get more out of wroxcom

Take an active role online by participating in our P2P forums p2p.wrox.com Hundreds of our books are available online through Books24x7.com Download short informational pieces and code to keep you up to date and out of trouble Sign up for our free monthly newsletter at newsletter.wrox.com Ready for more Wrox We have books and e-books available on .NET, SQL Server, Java, XML, Visual Basic, C C++, and much more We always like to get feedback from our readers. Have a book idea Need community...

How It Works

You now have a class whose intent is clearly spelled out. You've seen most of these behaviors in functions that you saw in Chapter 5, but now you have a structure within which you can combine all of these behaviors. This class will have interface methods that enable the omelet to use a Fridge object cooperatively, and it will still offer the capability to create customized omelets as it could in Chapter 5. Remember that all of the following code has to be indented one level beneath the Omelet...

HTML as a Subset of XML

XML bears a striking resemblance to HTML. This isn't entirely by accident. XML and HTML both sprang from SGML and share a number of syntactic features. Earlier versions of HTML aren't directly compatible with XML, because XML requires that every tag be closed, and certain HTML tags don' t require a closing tag, such as < br> and < mg < However, the W3C has declared the XHTML schema in an attempt to bring the two standards in line with each other. XHTML can be manipulated using the same...

Introducing Wish ListBargain Finder

Amazon lets individuals and booksellers advertise their used copies of books on its site, and Amazon presents the lowest used price for a book alongside its own price for a new book. If you look back at that XML search result for James Joyce, you'll see that A Portrait of the Artist as a Young Man is available new from Amazon for 8.10 (OurPrice), but people are also selling used copies for as low as 1.95 (UsedPrice). That's a pretty good price, even when you factor in shipping. Many of the...

Passing Parameters from Python to C

After you have everything built and installed, importing your new extension module and invoking its one function is easy *> * import foo > *> dir(foo) '_doc_', '__file_', '__name_', 'bar' If you tried to pass in any arguments to your function, the interpreter will rightfully complain File < * tdin* 11 , line 1, in TypeError bar() takes no arguments (1 given) Because you'll most likely want to define functions that do accept arguments, you can use one of the other signatures for your C...

Programming Basics and Strings

This chapter is a gentle introduction to the practice of programming in Python. Python is a very rich language with many features, so it is important to learn to walk before you learn to run. Chapters 1 through 3 provide a basic introduction to common programming ideas, explained in easily digestible paragraphs with simple examples. If you are already an experienced programmer interested in Python, you may want to read this chapter quickly and take note of the examples, but until Chapter 3 you...

Putting Your Program into Its Own File

As the examples in this book get longer, typing the entire code block begins to be a burden. A single mistake causes you to retype in the entire block of code you are working on. Long before you've gotten to the point where you have more than, say, 40 lines of code to type, you are unlikely to want to have to do it more than once. You are probably already aware that programmers write programs that are saved as source code into files that can be opened, edited, and run without a great deal of...

Representation of Data in XMLRPC

The XML-RPC methodName can be any string, but XML-RPC methods are traditionally grouped into named packages, such as searchsort in the preceding example. In a Python implementation, this makes it look like a module called searchsort that contains the functions to expose, like sortList. XML-RPC parameters can be any of the following types < string< < ames Joyce< < string> An array (items can be of any type, or a mixed type) < data> < array> A dictionary (keys must be strings...

Single Threaded Multitasking with select

The reason PythonChatClient spawns a separate thread to gather user input is that a call to sys. stdin.readline won't return until the user enters a chat message or server command. A naive chat client might call sys.stdin.readline and wait for the user to type something in, but while it was waiting the other users would keep chatting and the socket connection from the server would fill up with a large backlog of chat. No chat messages would be displayed until the user pressed the Enter key...

The Internet Protocol Stack

Different programming languages operate at different levels of abstraction. Python is a very high- level language capable of all kinds of tasks, but the Python interpreter itself isn't written in Python It's written in C, a lower-level language. C, in turn, is compiled into a machine language specific to your computer architecture. Whenever you type a statement into a Python interpreter, there is a chain of abstraction reaching down to the machine code, and even lower to the operation of the...

Unicode and bit

Unicode and 8-bit strings have been replaced with text and binary data. All text is considered to be Unicode, but the encoded Unicode is now presented as strictly binary data. Hence, text is stored in str, whereas data is stored in bytes. If you should ever try to mix these two data types, it will result in the raising of a TypeError. If you want to mix str and bytes, you must convert them. If you wanted to, for instance, convert a byte to a str, you would use bytes.decode(). To go from a str...

Whats New in Python

Python is constantly changing in little ways. Python 3.1 has evolved from version 2.6, but it contains important changes. This appendix introduces you to the changes relevant to the topics covered in this book. This means that this is not an exhaustive treatment by any means but only a selection of topics touched on in the book topics that you may want to know as someone new to Python. You can find the official list of changes to Python 3.1 at http docs.python.org 3-1 whatsnew 3.1.html. If a...

Wiki Searchand Replace Using the REST Web Service

What good is this web service for BittyWiki Well, here's an only slightly contrived example Suppose that you get someone to host a BittyWiki installation for an open-source project you're working on, called Foo. You create a lot of wiki pages that mention the name of the project in their text (Foo is a triphasic degausser for semantic defribulation) and in the titles of the pages (BenefitsOfFoo, FooDesign, and so on). All is going well until one day when you decide to change the name of your...

Wiki Searchand Replace Using the Xmlrpc Web Service

Remember WikiSpiderREST.py, the script that crawled BittyWiki pages using its REST API to perform search-and-replace operations You had to write a custom class (BittyWikiRESTAPl) to construct the right URLs to use against the REST interface, and a custom XML parser to process the response documents you got in return. Of course, once you have written that stuff, it can be reused in any application that uses BittyWiki's REST API, but the main selling point of XML-RPC is that such classes aren't...

Working with Relational Databases

Relational databases have been around for decades so they are a mature and well-known technology. People who work with relational databases know what they are supposed to do, and how they are supposed to work, so relational databases are the technology of choice for complex data storage. In a relational database, data is stored in tables that can be viewed as two-dimensional data structures. The columns, or vertical part of the two-dimensional matrix, are all of the same type of data like...

Working with Templates

Now that you understand the ways that views and URLconfs work, you can begin to work with templates. As stated previously, embedding HTML in your Python is not the best way to go about creating views. In this section, you learn to use templates instead. Templates, in their simplest form, are a way to ensure consistency across a site. Using a template also saves time you may recall the discussion about the 10,000 author bios Like an HTML document, templates are made up of many different tags too...

Creating GUI Widgets with Tkinter

The first thing to understand is that most GUI frameworks, including Tkinter, are based on a widget model. A widget is a component of a GUI buttons, labels, and text boxes are all widgets. Most widgets have graphical representations on screen, but some widgets, such as tables and boxes, exist only to contain other widgets and arrange them on the screen. A GUI is constructed out of an arrangement of widgets. In the following section, you create a simple GUI by defining some widgets and placing...

Safety When Accessing Form Values

Why is form.getfirst 'fieldName' safer than form 'fieldName' The root of the problem is that sometimes a single form submission can legitimately provide two or more values for the same field for instance, this happens when a user selects more than one value of a selection box that allows multiple selections . If this happens, form 'fieldName' will return a list of values for example, all the selected values in the multiple-selection box instead of a single value. This is fine as long as your...

Accessing Databases from Jython

JDBC, or Java Database Connectivity, provides a set of APIs to access databases in a consistent manner. Most, but not all, differences between databases can be ignored when working with JDBC. Python has a set of database APIs as well, as described in Chapter 14. A large difference between the Python APIs and the Java APIs is that the Java JDBC drivers are almost all written entirely in Java. Furthermore, almost all JDBC drivers are written by the database vendors. Most Python DB drivers, such...

Writing Java EE Servlets in Jython

Most Java development revolves around enterprise applications. To help or hinder, depending on your view , Java defines a set of standards called Java EE, or Java Platform Enterprise Edition. The Java EE standards define an application server and the APIs such a server must support. Organizations can then choose application servers from different vendors, such as WebSphere from IBM, WebLogic from Bea, JBoss from the JBoss Group, and Tomcat from the Apache Jakarta project. Java developers write...

Human Readable API Documentation

In my opinion, no matter which web service protocol you're using, nothing beats an up- to - date human -readable description of an API. This can be written manually or generated through introspection and the use of Python docstrings. Up next are three sample documents that describe the three web service APIs for the BittyWiki application created in this chapter. They're all extremely short, but they contain all the information a user needs to write an application using any of them. To get the...

Fetching Mail from a POP Server with poplib

Parsing a local mail spool didn't require going over the network, because you ran the script on the same machine that had the mail spool. There was no need to involve a network protocol, only a file format the format of UNIX mailboxes, derived mainly from RFC 2822 . However, most people don't have a UNIX shell account on their mail server or if they do, they want to read mail on their own machine instead of on the server . To fetch mail from your mail server, you need to go over a network,...

The LAME Project

LAME is or was an acronym that originally stood for LAME Ain't an MP3 Encoder. Whether or not it's officially considered an MP3 encoder isn't important to you, because it functions as a most excellent free and open-source library that is capable of encoding MP3s. Dozens of software projects use LAME but not many are implemented in Python, which is why you'll be using it as an example to demonstrate just how easy it is to create extension modules for Python that leverage an existing C code base,...