[Python-Dev] Adding syntax for units of measure
Christian Heimes
lists at cheimes.de
Tue Jun 23 12:51:55 CEST 2009
Tobias C. Rittweiler schrieb:
> Hi!
>
> Has anyone added special syntax to allow writing numeric literals with
> physical units? So you can write 12m + 34cm, and would get 12.34m.
>
> My question is how would you modify the BNF the most sensible way to
> allow for this? The above example is simple, but think of 42 km/h.
You don't need special syntax in order to work with units. You just need
to normalize all input to SI units like meter, second and meter per second:
km=1000.
m=1.
dm=0.1
cm=0.01
mm=0.001
s= 1.
h = 3600.
m_s=m/s
km_h=km/h
length_in_mm = (12*m + 34*cm)/mm
speed = 5*km_h
For what purpose do you want to have physical units in Python syntax? Do
you need to verify your formulas?
Christian
More information about the Python-Dev
mailing list