Python Rocks! and other rants
Weblog of Kent S Johnson

#64 2005-06-10 09:24:16

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.

Categories: Python

 
© Kent S Johnson Creative Commons License

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

kentsjohnson.com

Weblog home

All By Date

All By Category

Agile

Java

Python

Essays

XML-Image

BlogRoll

Powered by Firedrop2