Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Units of measurement

Reply
Thread Tools

Units of measurement

 
 
Paul Rubin
Guest
Posts: n/a
 
      01-17-2007
I'm sure this has been done before, but it just struck my fancy, an
example of Python's "emulating numeric types", inspired by the old
Unix "units" utility, and the Frink language. The language reference
for these features is somewhat unclear and I'll enter some sourceforge
items. Maybe I'll write a calculator-with-units around it. If I were
really industrious, I'd attempt a Frink-like interpreter, but don't
hold your breath.

================================================== ==============

try:
set
except NameError:
from sets import Set as set # python 2.3 ...

class Unit:
def __init__(self, coef, dimensions):
self.dimensions = dimensions
self.coef = coef
def __add__(self, other):
if self.dimensions != other.dimensions:
raise TypeError, ('dimension mismatch', self, other)
return Unit(self.coef + other.coef, self.dimensions)
def __sub__(self,other):
return self + (-1.0) * other
def __rmul__(self, x):
return x*self
def __rdiv__(self, x):
return x/self
def __mul__(self, other):
pc = self.coef * other.coef
sd, od = self.dimensions, other.dimensions
basis = set(sd.keys()) | set(od.keys())
pa = [(d, sd.get(d,0)+od.get(d,0)) for d in basis]
pd = dict([(d,a) for d,a in pa if a != 0])
return Unit(pc, pd)
def __repr__(self):
a = [repr(self.coef)]
for d,c in self.dimensions.iteritems():
s = str(d)
if c != 1: s += '^'+str(c)
a.append(s)
return '*'.join(a)
def __div__(self, other):
od = other.dimensions
inv = Unit(1.0 / other.coef,
dict([(d, -od[d]) for d in od]))
return self * inv
def __pow__(self, n):
n = n.coef
if self.dimensions and type(n) not in (int,long):
raise TypeError, ('exponent must be integer', self,n)
cn = self.coef ** n
sd = self.dimensions
return Unit(self.coef ** n,
dict([(d,sd[d]*n) for d in sd]))

def __coerce__(self, other):
if isinstance(other, Unit): return self, other
return self, Unit(other, {})

def __float__(self):
if self.dimensions:
raise TypeError, ('unit must be dimensionless for float cast', self)
return float(self.coef)
def __int__(self):
return int(float(self))

def base_unit(name):
return Unit(1.0, {name : 1})

meter = base_unit('m')
second = base_unit('s')
kg = base_unit('kg')
coulomb = base_unit('coulomb')

centimeter = meter / 100
inch = 2.54 * centimeter
foot = ft = 12 * inch
mile = 5280*foot
minute=60*second
hour=60*minute
speed_limit = 55 * mile / hour
furlong = mile / 8
day = 24 * hour
fortnight = 14 * day
# could include more units but you get the idea

c = 186282*mile/second
print 'speed of light =', c/(furlong/fortnight), 'furlongs per fortnight'
# ...
 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      01-17-2007
Paul Rubin wrote:
> I'm sure this has been done before, but it just struck my fancy, an
> example of Python's "emulating numeric types", inspired by the old
> Unix "units" utility, and the Frink language.


Oh yeah, it's been done before. Several times over, in fact.

Unum
http://home.tiscali.be/be052320/Unum.html

Caltech's pyre.units
http://www.cacr.caltech.edu/projects/pyre/

Konrad Hinsen's Scientific.Physics.PhysicalQuantity
http://dirac.cnrs-orleans.fr/ScientificPython/

Will Ware posted one to the list.
http://groups.google.com/group/comp....05aea353c1563d

And there was another one announced here sometime in the past year or so, IIRC,
but I don't recall the name of it or that of the author.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
 
 
 
Russ
Guest
Posts: n/a
 
      01-18-2007
Robert Kern wrote:
> Paul Rubin wrote:
> > I'm sure this has been done before, but it just struck my fancy, an
> > example of Python's "emulating numeric types", inspired by the old
> > Unix "units" utility, and the Frink language.

>
> Oh yeah, it's been done before. Several times over, in fact.


<cut>

> And there was another one announced here sometime in the past year or so, IIRC,
> but I don't recall the name of it or that of the author.


Perhaps you are referring to the scalar class at
http://RussP.us/scalar.htm

The nice thing about this one (which I wrote myself) is that after you
do your development and are ready for "production" runs, you can easily
disable the unit checks and get the efficiency of built-in numeric
types. That can be two orders of magnitude faster!

It comes with a complete user manual too (pdf and html). I'm using it
for my engineering work, and it's working great!

 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      01-18-2007
Russ wrote:
> Robert Kern wrote:


>> And there was another one announced here sometime in the past year or so, IIRC,
>> but I don't recall the name of it or that of the author.

>
> Perhaps you are referring to the scalar class at
> http://RussP.us/scalar.htm


Yup, you're the one.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      01-19-2007
Paul Rubin <http://(E-Mail Removed)> wrote:
>
>I'm sure this has been done before, but it just struck my fancy, an
>example of Python's "emulating numeric types", inspired by the old
>Unix "units" utility, and the Frink language.
>...
># could include more units but you get the idea
>...
>c = 186282*mile/second
>print 'speed of light =', c/(furlong/fortnight), 'furlongs per fortnight'
># ...


I could not skip to the next message without cutting, pasting, and
executing that script to find out what the speed of light actually was in
furlongs per fortnight.

Now I need to figure out how to work that into a cocktail party
conversation. "Hey, the deficit isn't the only thing that is approaching
1.8 trillion..."
--
Tim Roberts, http://www.velocityreviews.com/forums/(E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      01-19-2007
On Fri, 19 Jan 2007 08:35:59 GMT, Tim Roberts <(E-Mail Removed)> declaimed
the following in comp.lang.python:


> I could not skip to the next message without cutting, pasting, and
> executing that script to find out what the speed of light actually was in
> furlongs per fortnight.
>

Table 15.15 "Units of Length, Speed, and Mass" (page 716) in the
"Explanatory Supplement to the Astronomical Almanac" includes that set
of units for c in the footnote area...
--
Wulfraed Dennis Lee Bieber KD6MOG
(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
response time measurement program sunshineSoft@pacbell.net Java 2 12-24-2007 08:46 AM
ontology for units of measurement and/or physical quantities Robert Dodier XML 2 09-14-2006 11:53 PM
Measurement preferences Paul Blarmy Firefox 3 06-01-2005 03:01 PM
memory-use measurement Reiner Funck Java 1 11-10-2004 08:56 PM
JPS 1.4: unit of measurement in the printDialog() =?ISO-8859-1?Q?J=F6rg_Marti?= Java 0 08-12-2003 06:18 AM



Advertisments