Posts about python (old posts, page 15)

2008-04-09 14:06

A simple memcache memoizer for python>=2.2

Just a snippet of code because every once in a while I need something like the classic memoize decorator but am working on a CentOS 4 bix (with python 2.3!)

I am still testing it, and am not even sure it really works, but it should be close.

cache=memcache.Client([''], debug=0)

def memoize(fun):
    def inner(*args, **kwargs):
        cached = cache.get(key)
        if cached is None:
            val = fun(*args, **kwargs)
            print "Setting: ",key, "to: ",val
            return val
        return cached
    return inner

And later inside a class:

def myfun(self,arg):

And that's it. The basic idea I stole from a blog who was inspired by a Paul Graham book. It can be trivially turned into a decorator, of course (but then only works on 2.4 and later).

2008-02-25 18:31

Weird Django/PyODB bug

I am finishing my first large-ish Django app from scratch [1].

To login a user you can do something like this:

[root@wally app]# python shell
Python 2.3.4 (#1, May  2 2007, 19:26:00)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyodb import *
>>> import django.contrib.auth
>>> print django.contrib.auth.authenticate(username='user',password='pass')

And then you use that user object for django.contrib.auth.login

Some of the authentication data is available on a Microsoft SQL Server, and I get it via pyodb. And this happens when I try to authenticate the user (assume user and pass are valid, this is the smallest snippet that triggers the bug):

[root@wally app]# python shell
Python 2.3.4 (#1, May  2 2007, 19:26:00)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyodb import Connect
>>> import django.contrib.auth
>>> c1=Connect("sqlserver",uid="user",pwd="pass")
>>> print django.contrib.auth.authenticate(username='user',password='pass')

As you can see, the pyodb stuff should not interfere with the django stuff at all. I am only importing one function and connecting to the DB, I don't even execute any SQL or use any data from the SQL connection. But the calls to authenticate fail.

How are you supposed to debug this? I worked around it by moving the PyODB stuff to another module, but it's weird.

[1] It's a list server. Discussion groups, moderation features, subscriptions, the works. Like google groups, but yours and not ajaxy. It's not Open Source software, but if you want something similar, I am sure my customer will license it to you :-D

2008-02-18 10:06

Text-based presentations

There have been a few posts on planet KDE about text-based presentation tools (1 , 2 , 3) and while their solutions are all probably better than mine, I have to say I did something in the area about a year ago.

Since I like restructured text and PyQt that's what it's based on, but you can just use it with vi and a wyse60, if you want.

My project is called Tobogan and it has a working version.

It has a GUI, but let's ignore it for this post about text-based software ;-) and show the text behind it:

Why use Tobogan

(If you are a nerd)

:transitions: from_left,to_left,from_top,to_top,from_right,to_right,to_bottom,fade_out

It's nerd-oriented

It's trivial to display source code, with
proper syntax highlighting.

.. code-block:: python

    from base64 import *
    def myFirstFunction():
          print b64decode ('YnllIHdvcmxkIQ==')

Even for things like shell sessions.
Dammit, I am a nerd, I will try to add every nerd
feature I deem cool.

It does things simply

This is a subtitle?

Wanna show a flickr photo?

.. code-block:: rst

    .. flickr:: myPhotoTitle

How about using to share your files?
And whatever else you can think of, it probably
**can**\  be done.

.. footer:: Smart thing goes here

And here is the result after running on it.


  • If you move the cursor to the top, you will get (broken?) previous/next arrows
  • If you click on the window, you get to the next slide.

I have not touched this in almost a year, and it would need lots of love, but it's not a bad concept, IMVHO.

2008-02-12 12:15

Open with: Google Docs

The eee is small. It has very little storage. So, why should I use dozens of MB on a word processor? Because I get word documents in the mail every once in a while.

In fact, the only word documents I get are error messages from windows users. Here's the procedure every one of them seems to have found to tell me what's inside explorer's error pages:

  1. Capture the screen
  2. Paste it into Word
  3. Mail it to me

I suppose copy&paste of the page contents was too hard. But anyway, I usually manage by having kword or abiword handy, but I was thinking...

I am reading my email. That means I am on the net. That means google docs is right there. And they have a python API! 15 minutes later...

#!/usr/bin/env python

# Create a client class which will make HTTP requests with Google Docs server.
client =
# Authenticate using your Google Docs email address and password.
client.ClientLogin('[email protected]', 'whateveritis')

ms = gdata.MediaSource(file_path = sys.argv[1], content_type = "application/msword")
entry = client.UploadDocument(ms,"tmp_open_with_gdocs/%s"%sys.argv[1])
os.system("firefox '%s'"%entry.GetAlternateLink().href)

Try it, as long as you have firefox, a decent version of python and gdata it should open the doc you pass as first argument on google docs in firefox.

Remember that you need to delete it later if you don't want it, and rename it if you want to keep it with a decent name.

I am not turning it into a real app, but it's good enough for me. Put it in your path and associate it to your .doc files.

A similar thing for .xls is trivial. A script that would handle both, also.

Could please someone take this and make it a real app?

2008-02-09 12:02

rst2pdf: New and improved

My rst2pdf script has had several things happen to it.

  1. It got another guy working on it: Christoph Zwerschke
  2. It's on googlecode now:
  3. Christoph made a number of improvements:
    • bulleted and enumerated list simplified, use same font as text for bullets and numbers
    • links in table of contents work
    • compress literal sections horizontally so that they always fit on the page
  4. I have integrated hyphenation using wordaxe (works only with reportlab 2.1)

The output using hyphenation is really ugly right now (for example, I get a black square instead of an hyphen) but it's a small step forward.

2008-02-05 19:44

Rebelling against insanity: Wicd requires half of GNOME

UPDATE: you can get this program now at google code

I have been using my eee for a while already with a sort of Kubuntu in it.

However, my favourite wireless/wired network management app is wicd, which is a Python/GTK application. Or was, since version 1.4.1 requires python-gnome2-extras.

Which depends on ....

libart-2.0-2 (>= 2.3.18), libaspell15 (>= 0.60), libatk1.0-0 (>= 1.13.2), libbonobo2-0 (>= 2.15.0),
libbonoboui2-0 (>= 2.15.1), libc6 (>= 2.6-1), libcairo2 (>= 1.4.0),
libfontconfig1 (>= 2.4.0), libfreetype6 (>= 2.3.5), libgconf2-4 (>= 2.13.5),
libgda3-3, libgdl-1-0, libgdl-gnome-1-0, libgksu1.2-1 (>= 1.3.3), libgksu2-0 (>= 1.9.6),
libgksuui1.0-1, libglade2-0 (>= 1:2.6.1), libglib2.0-0 (>= 2.14.0), libgnome-keyring0 (>= 2.19.6),
libgnome2-0 (>= 2.17.3), libgnomecanvas2-0 (>= 2.11.1), libgnomeui-0 (>= 2.19.1),
libgnomevfs2-0 (>= 1:2.17.90), libgtk2.0-0 (>= 2.12.0), libgtkspell0 (>= 2.0.2),
libice6 (>= 1:1.0.0), libnspr4-0d (>=, liborbit2 (>= 1:2.14.8),
libpango1.0-0 (>= 1.18.2), libpng12-0 (>= 1.2.13-4), libpopt0 (>= 1.10), libsm6,
libstartup-notification0 (>= 0.8-1), libx11-6, libxcomposite1 (>=1:0.3-1),
libxcursor1 (>> 1.1.2), libxdamage1 (>= 1:1.1), libxext6, libxfixes3 (>= 1:4.0.1), libxi6, libxinerama1,
libxml2 (>= 2.6.29), libxrandr2 (>= 2:1.2.0), libxrender1, zlib1g (>= 1:,
python-support (>= 0.3.4), python (<< 2.6), python (>= 2.4), python-gtk2,
python-pyorbit, python-gnome2-desktop

In short: a 87MB download. That can't be good. In fact, there are almost no changes from 1.3.1 which didn't require all that! Except for one change that makes all the difference on a eee PC: vertical resizing to under 400px. :-(

So, because I am who I am, I did this:


It's a replacement for wicd's and Only needs PyQt4 which I already had and depends on:

libc6 (>= 2.6-1), libgcc1 (>= 1:4.2.1), libqt4-core (>= 4.3.2),
libqt4-gui (>= 4.3.2), libstdc++6 (>= 4.2.1), python-central (>= 0.5.8),
python (<< 2.6), python (>= 2.4), python-sip4 (>= 4.7), python-sip4 (<< 4.8)

See a difference there?

Took me about 3 hours to hack together, and works (except for wired config, the prefs dialog, static IP and scripts) but the hard work is done.

If anyone wants a copy, just ask. I expect KUbuntu could use something like it?

2007-11-28 14:01

Rethinking Linux Configuration: Part I

I have said Linux sucks. Here's my new project: Make it suck less, one bit at a time.

And I start with one of the big chunks: configuration.

Part I in a longish series (I expect 5 parts at least).

2007-11-05 17:34

Coming soon...

I have managed to create the most bizarre way to write a spreadsheet engine in Python.

I still need to polish some things, but here are the highlights:

  • Your formulas compile to C
  • C is inlined using Instant

Yes, that means you edit a cell in the GUI and you need to wait until gcc compiles the thing.

Is it going to be useful? Probably not. Is it cool? I say yeah.

I will polish it somewhat, create a sttandalone engine, and show it here.

Contents © 2000-2019 Roberto Alsina