Rocking the blogosphere

Archive for the 'Programming' 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.

Popularity: 23% [?]

SD West 2008

This week I’m at SD West 2008. I’m hoping to learn a few things about C++, especially C++0x and Boost.

Popularity: 80% [?]

Setuid demystified

Interesting that the Unix calls for setting user ids of processes are so varied and so complex, that they warranted an entire paper. I only skimmed, but I did get a pretty good description of the “saved uid” parameter of the setresuid call.

Setuid demystified (PDF)

Popularity: 63% [?]

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.

Popularity: 59% [?]

Perl->{’5.10′} += (”//” && > Operator[s])? =~ /amusing/

A snippet from the latest freshmeat.net announcement of Perl 5.10 that landed in my email inbox:

This version includes a new smart match operator, a switch statement,
the // defined-or operator, regular expression improvements, and
other language changes…

Funny, I was just thinking that what Perl needs most is more operators… :-)

Popularity: 72% [?]

Darcs 2

Chris pointed out that Darcs 2 is available for testing:

http://wiki.darcs.net/index.html/DarcsTwo

Popularity: 100% [?]

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']

Popularity: 47% [?]

Next Page »