TurboGears: Supporting Cast

# Other projects used by TurboGears

# Other parts

### ElementTree

Fredrik Lundh's fast and easy XML parser and generator library.

### FormEncode

Ian Bicking's validation and conversion package.

### TestGears

Kevin Dangoor's unittest extension

### json-py

Patrick Logan's JSON converter for Python.

When you download TurboGears, you get several packages in the bargain. Though the four main packages are the ones you'll interact most directly with, these other packages are also working behind the scenes and available for your use to make your life better.

## ElementTree

For anyone who's ever had to write code to the W3C interfaces in Java, you know how difficult working with XML can be. ElementTree provides a programming interface that is far easier for common XML tasks. It is not the be-all and end-all XML library, but it's a very practical approach to much of what you'll need to do.

TurboGears also includes cElementTree, which is an implementation of the ElementTree API in C. cElementTree is very fast.

Kid uses ElementTree to parse and work with your templates.

Here's an example of ElementTree usage from the ElementTree site:

<![CDATA[ from elementtree.ElementTree import Element, SubElement, ElementTree # build a tree structure root = Element("html") head = SubElement(root, "head") title = SubElement(head, "title") title.text = "Page Title" body = SubElement(root, "body") body.set("bgcolor", "#ffffff") body.text = "Hello, World!" # wrap it in an ElementTree instance, and save as XML tree = ElementTree(root) tree.write("page.xhtml") ]]>

## FormEncode

When data comes in from the web, you get a blob of strings that map to whatever was on the form the user submitted. Odds are that what you'd like to have is something different. Perhaps you want a boolean instead of the string "True" or perhaps you want an int instead of the string "42". Or, maybe you need to verify that the user entered "301-555-1212" for their phone number (let's ignore the usability issues of that, shall we?).

FormEncode provides a framework for converting incoming values to their appropriate Python versions and converting Python values to whatever is needed on the other end. In the process of doing the conversion, it also makes sure that the values are valid and provides a means to describe what the problem is.

FormEncode is used by SQLObject to do conversions to and from database types. You'll also encounter it in TurboGears directly when using the validation feature.

Here's a quick example of using a form encode validator:

## TestGears

Python's standard unittest module is based on the venerable JUnit. It's a straightforward framework, but there are some usability niggles here and there.

For example, it's pretty common to have a TestSuite that covers your whole project. It's a good idea to run the full TestSuite frequently. Unfortunately, you have to keep that suite up-to-date by hand. TestGears provides a simple means to gather up all of the tests in your project. If you have a distutils setup.py script (and you do automatically, if you use TurboGears' quickstart command), you can just run "python setup.py testgears" to run all of your tests.

Even though writing a TestCase is easy, sometimes even that is more overhead than you really need. With TestGears, if you put functions with names beginning with "test" in a file that begins with "test_", your tests will run automatically as part of the suite.

As a unittest extension, you should be able to get other TestRunners and whatnot working directly with your TestGears tests.

## json-py

JSON (JavaScript Object Notation) specifies a way to represent data as nested hash tables, lists and simple values. For transmitting data to a browser for further manipulation, JSON has some significant advantages over XML: the syntax is native JavaScript so it's fast, can be interpreted directly via eval() and is supported equally by all browsers.

The json-py library provides conversion routines to migrate simple values from Python to JSON and vice-versa.