Published on (
 See this if you're having trouble printing code examples

Python News

RDF and Python


Also in Python News:

BitTorrent Style

Twisted Python

Python Escapes Classroom

Humongous Python

Py in Print

In a trackless sea of online documents, finding the article you want can be a struggle. While hyperlinks connect documents together, they don't say anything about the documents' relationships to each other. There is no real framework in HTML for metadata or information about the data itself. This is why we need RDF, the Resource Description Framework. RDF, a core technology of the Semantic Web, gives us a uniform way to make statements about resources and about their relations to other resources.

RDF statements are like simple sentences. They have a subject, an object, and a predicate (i.e., a verb). In RDF terms, this is called a triple. You can break down even the most complex RDF documents into a series of triples. There is plenty more to know about RDF, like who defines the predicates, and how resources should be identified, and special applications of RDF like RSS (used to summarize the contents of a site), but it all breaks down into triples that describe resources and relationships.

People commonly think of RDF as an XML technology. While XML is an important means of sharing RDF data, it is only one way to represent RDF information. You could also store it in a relational database, as a list of three-item tuples, or in whatever form works for your program. Many toolkits and APIs convert RDF information from XML to some other native storage format and back again. 4RDF, a part of 4Suite's XML processing tools, is one of the best-known open source RDF tools for Python. 4RDF gives you the basic tools to read, manipulate, and write RDF files in XML. Once you have the RDF data in Python, you can easily add to it, examine it, and edit it. What you might most want to do with RDF information is search it. 4RDF has tools for that. You can search the data for exact statement matches or use regular expressions to find the document relationships you want.

Another tool for RDF is James Tauber and Daniel Krech's Redfoot. Like 4RDF, Redfoot provides tools for parsing and manipulating RDF, but its big claim to fame is bringing those tools to the Web. Redfoot includes a web application and server for viewing and editing RDF statements and building new RDF files interactively. You can extend Redfoot's server class to build your own custom server, but you can also provide new pages and instructions to the server using RedCode, a combination of Python code and XHTML. One of the examples in their recent 0.9.6 release uses a new RSS 1.0 module to connect to RSS (or RDF) news feeds and incorporate them in your web pages. Using RedCode, the authors created this customized server in under a hundred lines of code.

I encountered some problems getting the sample programs included in release 0.9.6 to run. On both Linux and Windows, I had to edit the run scripts to include the current directory (/redfoot-0.9.6/apps) in the PYTHONPATH. I also had to specify the port for the server or it would default to 80, the standard web server port. Those two changes were enough to run the samples on my Linux box. Because my Windows system is Windows 98, I had to make yet a couple more changes in the batch file. I changed %* to %1 to pass in the command line argument. Since I have no setlocal or endlocal commands, I removed them from the batch file. Here is my modified run.bat file:

@echo off
set PYTHONPATH=../lib/;.
echo Usage (example): run module_name
@echo on

python ../lib/redfoot/ -p 8000 %1 

Finally, Redfoot 0.9.6 is not Python 2.1 compatible. If you already have 2.1 installed, I suggest getting a copy of PythonWare's PY20. You may want to have both versions of Python handy for a while anyway, and PythonWare's installation of Python can coexist with Python 2.1.

Don't be too intimidated by these changes. They won't take long. If you are at all interested in RDF -- and you definitely should be -- you will want to look at Redfoot.

Stephen Figgins administrates Linux servers for Sunflower Broadband, a cable company.

Read more Python News columns.

Copyright © 2009 O'Reilly Media, Inc.