from __future__ import * 2003/11

2003-11-24

for loops and empty iterables

Just van Rossum and I were discussing the for:else:, while:else: and try:else: syntax on the MacPythonChannel this morning (morning for me, at least). I'm not sure if this is sufficiently covered elsewhere, but here is the pattern I use to check for empty iterables:

class NoValue:
    """A unique placeholder, since None is sometimes used in iterables"""

def doesSomethingWithIterable(iterable):
    x = NoValue
    for x in iterable:
        pass # something useful could go here ;)
    else:
        if x is NoValue:
            pass # do something because iterable was empty
        else:
            pass # do something because iterable was not empty
posted at 11:11:44    #    comment []    trackback []
 
2003-11-19

Comparing iterables

Python doesn't have a way to compare arbitrary iterables (without converting them to a tuple or list). It's also easy to get it wrong if you try and do it with itertools (since imap and izip will consume an extra element from the first sequence if it's longer). Here's how to do it right:

from itertools import ifilter, imap, chain

class StopToken:
    def __cmp__(self, other):
        if self is other:
            return 0
        return -1
StopToken = StopToken()

def cmpseq(a, b, StopToken=StopToken):
    a, b = chain(a, (StopToken,)), chain(b, (StopToken,))
    for rval in ifilter(None, imap(cmp, a, b)):
        return rval
    try:
        a.next()
        return 1
    except StopIteration:
        try:
            b.next()
            return -1
        except StopIteration:
            return 0
posted at 11:10:40    #    comment []    trackback []
 
2003-11-15

argh ranlib

Apparently I had neglected to ranlib /usr/local/lib/libjpeg.a before building PIL. So if you have downloaded PIL from my repository you'll want to rm -rf /Library/Python/2.3/PIL* and then reinstall.

Sorry about that!

posted at 17:14:24    #    comment []    trackback []
 
2003-11-14

Stupid vim tricks

In some projects, like PyObjC, the coding style is to use tabs in C/ObjC code, and spaces in Python. I'm lazy, and haven't set it to do this on load, and I often forget to :set noet (not expand tabs) in vim. The quick trick I use to convert tabs to spaces (or vice versa) is to:
  • set/unset et as appropriate

  • make sure my sw (shiftwidth) and ts (tabstop) are both 4 (this is in my .vimrc)

  • indent the block of code once (this converts, but gives me extra indent) with > in visual mode

  • dedent it with < in visual mode

Voila, the whole mess is now converted from tabs to spaces or vice versa. Here's what I find to be comfortable in my .vimrc (I do have some custom stuff for python, pyrex, coloring, etc. but that's not as important):

set ffs=unix,dos,mac
set ts=4
set bs=2
set et
set sw=4
set sm
set sta
set sts=4
set ai
posted at 12:02:56    #    comment []    trackback []
 
2003-11-12

Using aeve to make iTunes talk to iChat

I was originally going to write a little bit about the unicodedata module, but google tells me that Ludoo already did. So instead, here's a quick aeve script that will set your iChat status message to the current track in iTunes (make sure to run with pythonw, it needs WindowManager access).

import aeve
ichat = aeve.talkto('com.apple.iChat')
itunes = aeve.talkto('com.apple.iTunes')
ichat.status_message = u'\N{BEAMED EIGHTH NOTES} %s - %s' % (
    itunes.current_track.name, 
    itunes.current_track.artist)

Also see Donovan Preston's iTunesStatus script (requires Twisted and Quotient) that makes the same information available from a web browser!

posted at 18:46:08    #    comment []    trackback []
 
2003-11-11

High Performance Computing for OS X

High Performance Computing for OS X, a site by Gaurav Khanna, offers precompiled binaries, links and reviews of many open source and commercial compilers, libraries, and software related to heavy duty numerical work on OS X. It is a must see if you're looking for a Fortran compiler, BLAST, MPI, Octave, etc.

posted at 13:03:44    #    comment []    trackback []
 
2003-11-10

More PackageManager fun

I threw a couple more packages into my OS X 10.3 Package Manager repository ( http://undefined.org/python/pimp/ ). Highlights include:
  • pycrypto 1.9a6

  • Pyndex 0.3.2a

  • pyOpenSSL 0.5.1

  • pyPgSQL 2.4

  • Quotient 0.7.0

  • Reverend 0.2.4

  • spambayes 1.0a7

  • Twisted 1.1.0

posted at 20:58:24    #    comment []    trackback []
 

Making docutils do python syntax highlighting (for HTML output)

http://undefined.org/python/docpytils-0.0.tgz is a simple docutils directive that does simple python syntax highlighting in ReST.. basically, after importing docpytils, you can do something like this:

.. pycode::
    
    python code here..

Like raw, you can also use :file: or :url: as arguments. To integrate this with PyDS, make sure the three python files are on sys.path somewhere (or inside PyDS), and import it from StructuredText.py

Output will look like this:

>>> from __future__ import *
  File "<stdin>", line 1
SyntaxError: future statement does not support import *
>>>
posted at 15:18:08    #    comment []    trackback []
 
2003-11-09

PyQt is built

Well it seems pretty buggy and throws a bunch of spurious warnings, so I'm not going to put it in Package Manager..

You'll need the Qt/Mac snapshot I posted in the last entry, here's the source+objects for PyQt and SIP.. a make install should be fast, compiling took a while:
posted at 20:50:56    #    comment []    trackback []
 

Building Qt sure does take a long time

Well, Qt/Mac GPL is at 3.2.2 now, and there's a new snapshot of SIP and PyQt for OS X.. so I decided to try and build it all today.

I'm still working on PyQt, but once it's ready it'll be in my PackageManager repository. Until then, here's Qt built static with threads, gif support, and QScintilla. This should save you a few hours, but it's a big download. For OS X 10.3 *only*!

qt-10.3-thread-gif-qscintilla-static-3.2.2.tar.bz2 [44.3mb]

Unarchive this to /, it will create /Developer/qt-mac-free-3.2.2 and /Developer/qt. I moved the dylibs to /Developer/qt/lib/dylib, so you can just move those down to lib if you want to compile dynamically. This is all the source to Qt, as well as whatever it produces when you compile it. The sources are unchanged, and the sources for QScintilla are not included (but are also unchanged).

posted at 20:16:48    #    comment []    trackback []
 
2003-11-08

Yet Another Line Ending Conversion Program

This is what I've been using.. it's really simple, and should convert Win32, Mac, or Unix line endings to whatever your native line ending is. A possible future optimization would be to use temp files instead, but they're annoying.

#!/usr/bin/env python
import sys, os, shutil
from cStringIO import StringIO

def tonative(fname):
    buff = StringIO()
    infile = file(fname, 'rU')
    infile.readline()
    infile.seek(0)
    if infile.newlines == os.linesep:
        return
    for line in infile:
        buff.write(line.strip() + os.linesep)
    infile.close()
    outfile = file(fname, 'wb')
    buff.seek(0)
    shutil.copyfileobj(buff, outfile)
    outfile.close()

if __name__ == '__main__':
    for fname in sys.argv[1:]:
        tonative(fname)

Is there any way to syntax highlight Python code in PyDS structured text?

Update: There is now, but I had to write it :) Details coming shortly.

posted at 15:52:16    #    comment []    trackback []
 

First thoughts about PyDS

  • It's not always responsive. Needs to be refactored to be more async.

  • There should be a button that rewrites your preview. I can't get it to refresh after editing a blog entry?

  • What the heck are those timers? Why are there 4 "backup databases" in the list? Can I edit them? Is that what add means?

  • What's an enclosure?

  • (Maybe a PyCS question) how do I get notified when someone comments?

  • How do I put syntax highlighted Python code in a structured text entry? Do I have to hack the code, or just do it as HTML and use another tool?

posted at 01:20:48    #    comment []    trackback []
 
2003-11-07

Adios, Señor Radio

Well, that was fun. I tossed radio and went through the fun game of packaging up the bazillion modules that PyDS needs, patching it a bit to make it play nice with OS X.. and here I am, perhaps one of the first PyDS users on OS X 10.3.

Since I'm a nice guy, I'm going to let any OS X 10.3 user easily try PyDS (you MUST be an admin user). Here's what you have to do:

sudo chgrp admin /System/Library/Frameworks/Python.framework/Versions/2.3/bin
sudo chmod g+w /System/Library/Frameworks/Python.framework/Versions/2.3/bin
  • Add /System/Library/Frameworks/Python.framework/Versions/2.3/bin to your PATH

  • Go to my Package Manager repository: http://undefined.org/python/pimp/

  • Read the instructions, you will have to download the MacPython 2.3 Additions if you have not already.

  • Open up Package Manager to my repository (remember, read the instructions) and install PyDS

  • Note that SOAPpy may error.. if so, just click install again, that's been reported to work. If you get the WriteableBin error, then you didn't do both of the sudo commands properly, or you are not an admin user. You must be an admin user, or none of this will work.

You ''should'' have a working PyDS install at this point. To start PyDS, perform these commands from Terminal:

pyds-start
open `pyds-browse`

Voilá! (These instructions worked for at least two people, I promise)

Note that I moved the system wide files to /Library/Application Support/PyDS and the user files to ~/Library/Application Support/PyDS -- so it behaves more like a real OS X application. GUI tools will be coming later, if I end up getting into this thing.

posted at 23:58:40    #    comment []    trackback []
November
MoTuWeThFrSaSu
      1 2
3 4 5 6 7 8 9
10111213141516
17181920212223
24252627282930
Oct Dec

Bob's Rants

XML-Image Letterimage

© 2003-2004, Bob Ippolito