Rocking the blogosphere

Archive for the 'Python' Category

Python ternary operator

Today’s Python discovery:

Python doesn’t have the C style ?: ternary operator (e.g.: cond ? valueIfTrue : valueIfFalse).

But as of Python 2.5 it has a ternary operator with its own syntax: value_when_true if condition else value_when_false

For example:

>>> 'a' if 1 == 1 else 'b'
'a'
>>> 'a' if 1 == 0 else 'b'
'b'

This is actually clearer and more Pythonic than that ?:

Unfortunately, for Python versions < 2.5, you don’t have this. I’ve seen people use: (condition and [value_when_true] or [value_when_false])[0]

IMHO, this is clever - in a bad way. Yuck. Personally, I think I’d rather just do:


def if_cond_val1_else_val2(cond, val1, val2):
   if cond: return val1
   else: return val2

This adds 3 lines to your program (or 1 if you stick it in a module that you import from your programs) and won’t cause your colleagues to hate you.

TiVo HME SDK for Python

Just stumbled up on this (via TiVoBlog who in turn found it via TiVo Lovers) and will have to give it a try sometime:

From TiVo HME SDK for Python:

An implementation of TiVo’s HME (Home Media Extensions) protocol for Python, as a module (hme.py), a simple server (hmeserver.py), and examples (mostly ported from TiVo’s Java HME SDK). Everything is released under the LGPL 2.1+, except where noted. (Most of the examples are Common Public License.)

I developed this in Python 2.5.1, and haven’t tested it with other versions, but it does nothing exotic. (hme.py depends only on the struct module. hmeserver.py is a bit more demanding.) But I have tested it in Linux, Mac OS X, and Windows XP.

Hacking OS X’s Python dbhash and bsddb modules to work

By default on my Leopard system, the dbhash and bsddb Python modules cannot be loaded.

marc@hyperion:~$ python
Python 2.5.1 (r251:54863, Oct  5 2007, 21:08:09)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbhash
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.5
        /lib/python2.5/dbhash.py", line 5, in <module>
    import bsddb
  File "/System/Library/Frameworks/Python.framework/Versions/2.5
        /lib/python2.5/bsddb/__init__.py", line 51, in <module>
    import _bsddb
ImportError: No module named _bsddb

I managed to get it to work though by installing bsddb3 from the pybsddb site and hacking the file /System/Library/Frameworks/Python.framework/Versions/2.5
/lib/python2.5/dbhash.py
:

marc@hyperion:~$ diff -u dbhash.py.orig dbhash.py
--- dbhash.py.orig      2007-11-28 10:16:33.000000000 -0800
+++ dbhash.py   2007-11-28 10:36:52.000000000 -0800
@@ -2,7 +2,7 @@

 import sys
 try:
-    import bsddb
+    import bsddb3 as bsddb
 except ImportError:
     # prevent a second import of this module from spuriously succeeding
     del sys.modules[__name__]

Here’s the patch.

Now it works:

marc@hyperion:~$ python
Python 2.5.1 (r251:54863, Oct  5 2007, 21:08:09)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbhash
>>> dir(dbhash)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', 'bsddb',
'error', 'open', 'sys']

reviewboard - Google Code

Just started using this at work for code reviews and it looks impressive and useful so far.

reviewboard - Google Code

Incidentally, it’s written in Python, using Django.

links for 2007-07-22

links for 2007-04-08

New features in JavaScript 1.7

Firefox 2.0 includes JavaScript 1.7 - here’s a breakdown of the new features.

Looking at this list of features, which includes destructuring assignments, generators, and list comprehensions, it seems to me that the Mozilla guys must have been heavily influenced by Python.

Next Page »