Saltycrane logo

Sofeng's Blog

Notes on Python, Django, and web development on Ubuntu Linux

     Posts for October 2008

How to escape (percent-encode) a URL with Python


import urllib
print urllib.quote_plus("http://www.yahoo.com/")
print urllib.quote_plus("Kruder & Dorfmeister")

Results:

http%3A%2F%2Fwww.yahoo.com%2F
Kruder+%26+Dorfmeister

It is easy to be drawn to the urlencode function in the Python urllib module documentation. But for simple escaping, only quote_plus, or possibly quote is needed. I believe this is the appropriate solution to Python urlencode annoyance and O'Reilly's Amazon Hack #92.

For reference: Percent-encoding on Wikipedia

Read more...

How to set the font for new frames in Emacs 23


I had been using the following elisp to set my font in Emacs 23. However, it did not work when creating new frames with C-x 5 2.

(if (>= emacs-major-version 23)
    (set-default-font "Monospace-11"))

Here is how I set the font for all frames:

(if (>= emacs-major-version 23)
    (modify-all-frames-parameters
     '((font . "Monospace-11"))))

See the documentation for modify-all-frames-parameters for more information.

Read more...

Running a Twisted Perspective Broker example with twistd


I've been using Twisted's Perspective Broker to manage networking for my Python program. Perspective Broker allows me to run a Python program on a remote machine and perform remote method calls on an object in the Python program. It also allows me to serialize objects and transfer them over TCP.

Once I got a Perspective Broker server and client running, I wanted to create a "Twisted Application" and run it using twistd, the Twisted Daemon. Two major options are: creating a .tac file and creating a twistd plugin. Below, I show the steps I took to create a ...

Read more...

Twisted links


Twisted tutorials

Read more...

Can't block for a Deferred in Twisted


Despite the existence of the promising waitForDeferred/deferredGenerator and the newer inlineCallbacks, it appears there is no way to block while waiting for a Deferred. Brian Granger described the problem on the Twisted mailing list:

I have a function that returns a Deferred. I need to have the result of this Deferred returned in a (apparently) blocking/synchronous manner:
def myfuncBlocking():
  d = myfuncReturnsDeferred()
  ...
  result =

  return result
I need to be able to call this function like:
result = myfuncBlocking()
The question is how to get the result out of the Deferred() and make it *look* like myfuncBlocking() has blocked.
glyph provided ...

Read more...

Running functions periodically using Twisted's LoopingCall


Twisted is pretty cool-- it is very powerful, but I haven't had the easiest time learning it. Here is a simple example that runs a couple functions periodically (at different rates) using LoopingCall.

For more information, here are the Twisted docs for LoopingCall.

from datetime import datetime
from twisted.internet.task import LoopingCall
from twisted.internet import reactor

def hyper_task():
    print "I like to run fast", datetime.now()

def tired_task():
    print "I want to run slowly", datetime.now()

lc = LoopingCall(hyper_task)
lc.start(0.1)

lc2 = LoopingCall(tired_task)
lc2.start(0.5)

reactor.run()

Results:

I like to run ...
Read more...

Installing beanstalkd and pybeanstalk on Ubuntu


Nikolay pointed me at beanstalkd, a lightweight, message queue service partially inspired by the popular caching system, memcached. It features a blocking "reserve" call so workers don't need to poll for new jobs. However, some might miss the data persistence since the queue is stored in memory. beanstalkd has client libraries in Erlang, Perl, PHP, Python, and Ruby. Of course, I will use the Python version, pybeanstalk. Simon Willison also found beanstalkd interesting. (Credit to him for some of the words above.)

Update 2008-10-13: For a helpful example on using beanstalkd and pybeanstalk see Parand's beanstalkd tutorial.

Here ...

Read more...

Installing Python 2.6 from source on Ubuntu Hardy


Python 2.6 was released yesterday! This version aims to smooth the transition from Python 2.5 to Python 3.0 which is planned for release soon (currently available as a release candidate). Python 3.0 will be break backwards compatibility with the 2.x series. Python 2.6 is backwards compatible with 2.5. All the backwards compatible features of 3.0 have been backported to 2.6.

One of the new 2.6 features I'm particularly intersted in is the new multiprocessing module which has a similar interface to the threading module, but it uses processes instead ...

Read more...

Installing Emacs 23 from CVS on Ubuntu Hardy


I have been using the emacs-snapshot package in Ubuntu Hardy. However, when I tried to use Tramp, I got an error message: Variable binding depth exceeds max-specpdl-size. I couldn't find out how to fix this, and I didn't want to use Emacs 22.3 because it doesn't have Xft (anti-aliased fonts), so I decided to live on the bleeding edge and install Emacs 23 from CVS. Besides the INSTALL and INSTALL.CVS files, I also used theBlackDragon's article for reference.

Update 2008-10-09: I just found that Romain Francoise maintains an emacs-snapshot Debian package and it has ...

Read more...
Created with Django | Hosted by Webfaction