Python Rocks! and other rants 10.6.2005
Weblog of Kent S Johnson

2005-06-10

Why I love Python 4

Sometimes Java almost seems to go out of its way to make coding difficult, while Python goes out of its way to make it easy. Here is a case in point.

I needed a Java method that accepts a duration expressed as a number of minutes in a string, and returns the same duration formatted as HH:MM:SS. In Python, this is trivial:

def formatDuration(durStr):
  hours, minutes = divmod(int(durStr), 60)
  return '%02d:%02d:00' % (hours, minutes)

Java, on the other hand, makes me jump through hoops to do the same thing:

static String formatDuration(String spcsfDur) {
    int duration = Integer.parseInt(spcsfDur);
    int minutes = duration % 60;
    int hours = (duration - minutes) / 60;
    
    Object[] args = new Object[] {
            new Integer(hours),
            new Integer(minutes)
    };

    String durStr = MessageFormat.format("{0,number,00}:{1,number,00}:00", args);
    
    return durStr;
}

Ouch.

posted at 09:24:16    #    comment []    trackback []
June 2005
MoTuWeThFrSaSu
   1 2 3 4 5
6 7 8 9101112
13141516171819
20212223242526
27282930   
Aug
2004
 Jul
2005

Comments about life, the universe and Python, from the imagination of Kent S Johnson.

XML-Image Letterimage

BlogRoll

© 2005, Kent Johnson