
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. (For my purposes, modifying the BNF is perfectly reasonable, but if you can depict a good, and convenient!, way that would not result in modifying it, I'd like to hear it, too.) Thanks in advance for your input, -T.

Tobias C. Rittweiler wrote:
Has anyone added special syntax to allow writing numeric literals with physical units? So you can write 12m + 34cm, and would get 12.34m.
Python-dev is for concrete discussion of development of the next versions. Questions and speculative discussion should generally be directed to python-list and concrete proposals to python-ideas. There have been posts on this subject on python-list and you can search the archive. That list is mirrored as newsgroup gmane.comp.python.general at news.gmane.org, which I believe has its own searchable archive. tjr

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

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.
(For my purposes, modifying the BNF is perfectly reasonable, but if you can depict a good, and convenient!, way that would not result in modifying it, I'd like to hear it, too.)
Hi, normally you wouldn't add units to the language itself. When using them programmatically, it should be no effort to use a class that represents a quantity with unit. This can be made as easy as making "m" an object of that type, so that you only need to type "2*m" to get two meters. For the interactive shell, using a wrapper that allows simplified input is also a possibility, like IPython's "-profile physics" mode, or something like http://bitbucket.org/birkenfeld/phsh/ which allows you to write
`1 m` + `12 cm` 1.12 m
cheers, Georg

>> Has anyone added special syntax to allow writing numeric literals with >> physical units? So you can write 12m + 34cm, and would get 12.34m. ... Georg> normally you wouldn't add units to the language itself. ... Georg> For the interactive shell, using a wrapper that allows simplified Georg> input is also a possibility, like IPython's "-profile physics" Georg> mode, or something like http://bitbucket.org/birkenfeld/phsh/ Georg> which allows you to write >>>> `1 m` + `12 cm` Georg> 1.12 m Also, check out the magnitude module (in PyPI). I use it to specify the units of the computation but allow users to input values using units which are meaningful to them. So, for example, if a value has units of time they could enter 1m or 60s and get the same internal value. Skip
participants (5)
-
Christian Heimes
-
Georg Brandl
-
skip@pobox.com
-
Terry Reedy
-
Tobias C. Rittweiler