MacPython Logo from __future__ import *

buy music albums Silver Apples buy mp3 albums Tarrus Riley buy tracks mp3 Kravits buy Reaper albums mp3 buy Kravits albums music buy music Evita CD online albums mp3 Silver Apples download Madonna CD music buy tracks music Kravits download music albums Silver Apples

2003-11-24

for loops and empty iterables

Filed under: python — bob @ 2:11 pm

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

2003-11-19

Comparing iterables

Filed under: python — bob @ 2:10 pm

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

2003-11-14

Stupid vim tricks

Filed under: python, vim — bob @ 3:02 pm
In some projects, like PyObjC, the coding style is to use tabs in C/Objective-C 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

2003-11-12

Using aeve to make iTunes talk to iChat

Filed under: aeve, python — bob @ 9:46 pm

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!

2003-11-11

High Performance Computing for OS X

Filed under: python — bob @ 4:03 pm

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.

2003-11-10

More PackageManager fun

Filed under: packman, python — bob @ 11:58 pm
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

Making docutils do python syntax highlighting (for HTML output)

Filed under: docutils, python — bob @ 6:18 pm

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.

Output will look like this:

>>> from __future__ import *
  File "<stdin">, line 1
SyntaxError: future statement does not support import *
>>>

2003-11-08

Yet Another Line Ending Conversion Program

Filed under: python — bob @ 6:52 pm

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)

Powered by WordPress