#!/usr/bin/python """Wrap the standard Unix units command but do the calculation as well. Arguments are the amount, the from-unit and the to-unit""" import os, sys a = sys.argv if (len(a) < 4): print "usage: convert.py quantity from-units to-units" sys.exit() cmd = "/usr/bin/units" pipe = os.popen("%s -q %s %s" % (cmd, a[2], a[3])) try: numberpart = lambda x: float(x.readline().strip()[2:]) mult, div = numberpart(pipe), numberpart(pipe) print "%0.3f %s = %0.3f %s" % \ (float(a[1]), a[2], float(a[1]) * mult, a[3]) except ValueError, e: message = str(e).split(":") unit = message[len(message) — 1].split(" ") print "Unknown unit: \"%s\"" % unit[len(unit) — 1].strip()[1:-1]
Python wrapper to take the pain out of the units command #
Comments
Comments are closed for this post.
Comments are closed for this post.

Tomsky
Aug 23, 2004, 6:04
Comments are closed for this post.