Archive for the 'Python' Category

How to delete an entry with SQLObject

Thursday, December 23rd, 2004

A while back I discovered that the SQLObject documentation didn’t say anything about deleting entrys from your database. And just now I saw that Michelle who is maintaining the PyWebOff Blog ran into the same problem. She found the solution but didn’t give it away, so here it is :
items.Category.delete(cid)
In this case, items is the module where I have collected all my SQLObject classes, Category is the name of an SQLObjectified class, cid is the id of an entry in the Category table and delete is the method that deletes the entry :-)

Merry christmas everyone!

First CherryPy 2 alpha release out

Tuesday, October 19th, 2004

The first public alpha release of CherryPy2 was made available yesterday. It represents a step away from extensions to the Python language and includes no “toy code”. Because of this, the compilation step is no longer necessary and no modifications have to be done to make current Python editors correctly colorize the code and build browsable code trees.

After you have downloaded the CherryPy2 alpha available from the
CherryPy website, unpack it to a temporary directory. Then, assuming you already have a recent version of Python installed on your computer:

  1. Open a shell (Konsole, Bash, Dos, whatever you have)
  2. Navigate to the temporary directory
  3. Type python setup.py install

Now, the cherrypy module should be installed into your $PYTHON/Lib/site-packages directory. (Edit: It might be $PYTHON/site-packages, depends on OS) Go there and open the tutorials directory. To run a tutorial, do the following

  1. Execute a tutorial script (type python tutorial1.py in a shell f.ex.)
  2. Open a web browser and point it to http://localhost:8080
  3. Play with it
  4. Open the tutorial1.py file in an your favorite Python editor and inspect the code.
    The tutorial files are well commented so it’s easy to understand what is happening.

There’s a small error in the 2 bonus tutorials, they try to import CherryPy.
Just change that to import cherrypy and run.

The templating system that has been built into CherryPy is also released as a pure Python module to go along with CherryPy2 or any other web framework. It’s now called CherryTemplate.

Comparing the different Python web frameworks

Sunday, September 12th, 2004

Deciding on a framework for Pythonic web programming is no easy task. There is fortunately help out there for the aspiring Python web programmer.

PyWebOff is a compare-and-contrast exercise to evaluate the strengths and weaknesses of some of the major Python web application frameworks. It started quite recently and currently only covers WebWare and Quixote. It looks like a very ambitious project under development and you can follow the progress in the PyWebOff Blog .The other frameworks included in PyWebOff’s plans for the future are CherryPy, Twisted, Zope, Karrigell and CGI.

Other valuable web pages for evaluating Python web frameworks are Ian Bicking’s Web Framework Shootout and the Python web frameworks overview at the official Python website.

PyPe and CherryClasses

Friday, August 20th, 2004

The CherryPy web framework (for Python) have some great features, like automatic url-to-method mapping. But it uses some “toy code” that makes it hard to handle for some Python editors. PyPe is a Python editor written in Python, so it’s easy to modify for a Python programmer. Let’s modify the builtin class browser so it handles CherryPy files perfectly. To follow this guide you should have PyPe installed (I have some problems with version 1.9.3, but 1.9.2 works perfectly) and a CherryClass (any .cpy file from the CherryPy distribution up to the current 0.10) or this file. Future versions of CherryPy will use only Regular Python code so unfortunately this guide will not be so useful within a few months. Let’s first take a look at the PyPe class browser’s presentation of a CherryClass :

CherryClass before

As you see, it only shows the functions. We want it to create a browsable tree with the CherryClass at the top level, sections as children of the CherryClass, and functions as children of the sections.

First, CherryPy uses an extension of Python classes called CherryClasses, identified by the keyword CherryClass. CherryClasses are divided into multiple sections called variable, aspect, mask, view and function. We need to make PyPe code parser aware of these keywords. The function we must modify is called fun and is a method of the function fast_parser in the parsers.py file. The fun function starts at line 35 (for PyPy 1.9.3). The relevant code is from line 100.

if ls[:4] == 'def ':
fun('def ', line, ls, line_no, stk)
elif ls[:6] == 'class ':
fun('class ', line, ls, line_no, stk)
elif ls[:1] == '#':
a = ls.lower().find('todo:')
if a+1:
todo.append((line_no, ls.count('!'), ls[a+5:].strip()))

You can see that it identifies class and function declarations and “todo” comments (a PyPe special feature). We just need to make it aware of the special CherryPy keywords. We also need 2 small hacks to make CherryClass sections reside within the CherryClass in the tree (because these are not indented) and to identify them as sections. The code to add, preferably after

elif ls[:6] == 'class ':
fun('class ', line, ls, line_no, stk)

is this:

elif ls[:12] == 'CherryClass ':
fun('CherryClass ', line, ls, line_no, stk)
elif ls[:9] == 'variable:':
ls = "section variable:"
line = " " + ls
fun('section ', line, ls, line_no, stk)
elif ls[:5] == 'mask:':
ls = "section mask:"
line = " " + ls
fun('section ', line, ls, line_no, stk)
elif ls[:5] == 'view:':
ls = "section view:"
line = " " + ls
fun('section ', line, ls, line_no, stk)
elif ls[:9] == 'function:':
ls = "section function:"
line = " " + ls
fun('section ', line, ls, line_no, stk)
elif ls[:7] == 'aspect:':
ls = "section aspect:"
line = " " + ls
fun('section ', line, ls, line_no, stk)

The 2 hacks are ls = "section keyword:" to add the section keyword before the type of section, and line = " " + ls to artificially indent the section identifier lines with one space. If you are normal, you have indented functions with at least two spaces and sections are then placed at a level above functions. Now, let’s take a look at how the PyPe class browser presents our CherryClass :

CherryClass after

Wohoo!!!! Just what we wanted!!!

One more thing to do is to modify the file browser so it associates .cpy files with Python/CherryPy. I’ll leave this as an exercise to the reader. Look at the code at line 101 and below in configuration.py and it should be quite easy.