Archive for the 'Python' Category

Deleteting a many-to-many relation with SQLObject

Tuesday, January 4th, 2005

If you have the need to delete an object with SQLObject you will probably like to delete a many-to-many relation some time too. First, I have to establish the relation. It’s explained in the SQLObject documentation so no details on that in this blog post. But it doesn’t say anything about how you can delete this relation. After searching the SQLObject mailing list and Wiki I finally found a clue in the source code and it’s really simple.

I am working on a website directory so I’ll just create an SQLObjectified website and an SQLObjectified category to put the website in. Categories can have many websites and websites can be placed in many categories so obviously I need a many-to-many relation. items is the module where I keep my SQLObject classes.
website = items.Website(url="http://www. .......", more="params")
category = items.Category(title=”Cool category”,more=”params”)
website.addCategory(category)

This establishes a website, a category and a relation between them. So if I want to move the website from that category I need to delete the relation between them. The magic code is
website.removeCategory(category)
That’s inserting 2 objects into a database and then creating and deleting (or should I say removing?) a relation between them in 4 simple lines of code. SQLObject surely isn’t perfect and the documentation could have been more extensive but this example demonstrates that you can do things with SQLObject in a fraction of the lines you need if you’re working with SQL.

Update : The SQLObject documentation actually says that a removeCategory method is created for this, it’s just hidden away in the reference section, under RelatedJoin: Many-to-Many.

CherryPy 2 beta is out

Friday, December 31st, 2004

Details including changelog and download at the CherryPy website. My simple quickstart guide for the CherryPy 2 alpha release should still be valid.

Brilliant feature #1 : PyPe todo comments

Thursday, December 30th, 2004

The Python editor PyPe is intended to be a simple editor that includes what you need and not much more. It does however include a nice little feature that I haven’t seen anywhere else. It’s called todo comments. A comment in Python looks like this
#A comment
A # sign followed by your comment. A PyPe todo comment is a perfectly valid Python comment and looks like this
#Todo: a todo comment
That’s a # sign followed by the word todo, a colon and the todo comment. Remember that you can’t have anything in front of it, so this line
a = 5 #Todo: a todo comment
includes a valid Python comment but it is not a valid todo comment because of the python code in front of it.

So what’s special about that? Let’s first take a look at a PyPe screenshot :

PyPe screenshot showing the text area, code browser and todo box

The PyPe view consists of a text area where you write your Python code, a browsable code tree to the upper right and a box with shortcuts to your todo comments to the lower right.

Whenever you think about something that needs to be done or find a problem with your Python code that you can’t fix instantly, add a todo comment and PyPe adds it to your todo list. The todo list is built when you open your file and refreshed everytime you press F5 (note: you might have to enable todo comments in the document menu).

Example todo comments:

Screenshot of example todo comments

This results in the following todo list :

Screenshot of the corresponding PyPe todo list

You can add todo comments anywhere in your code and when you doubleclick an entry in the todo list you are taken to the line that contains the todo comment. Simple, effective and brilliant!

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.