-3

I'm very new to python. How can I convert a unit in python? I mean not using a conversion function to do this. Just as a built-in syntax in python, like the complex numbers works. E.g., when I typed 1mm in python command line, and expect the result is 0.001

>>> 1mm
0.001
#Just like the built-in complex numbers or scintific expressions
>>> 1j
1j
>>> 1e3
1000

I totally have no idea, do anybody knows how complex number or scintific expressions work in Python? Or any idea on how to do it.

thanks,

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
Jack_wang
  • 1
  • 1
  • 1
  • 1
    You can't add new syntax like that to Python. The complex number syntax is built into the language and you can't extend it without modifying the internals of Python. – BrenBarn May 18 '13 at 18:24
  • What is wrong with using functions? The alternative is to write a interactive shell program in Python, and have *that* interpret your conversions for you. – Martijn Pieters May 18 '13 at 18:30
  • I mean can I add a package or something else, to supporting some syntax which I defined besides the python syntax, still in python command line, and do not modifying the internals of Python, just add some new package? – Jack_wang May 18 '13 at 18:44
  • To use functions is not very friendly for user to type in some statements, e.g.," >>> length = Converter("1mm")", one more like to use ">>> length=1mm" directly. – Jack_wang May 18 '13 at 18:50
  • 2
    @Jack_wang: No, you can't. You could write your own program that accepts your own syntax and converts into Python, then passes it to Python, but then you'd have to work in a shell using your program, not a regular Python shell. You can't make a regular Python shell use your special syntax. – BrenBarn May 18 '13 at 18:51
  • Thank you all, I'll try it with writing a interactive shell. – Jack_wang May 18 '13 at 20:16

3 Answers3

1

how bout

mm = 0.001

1*mm

not sure if that is what you are asking for ... if you have ever messed with report lab they do simillar stuff. (although they use it to convert pixels to actual border sizes and what not)

eg:

 inch = DPI*some_thing
 margin = 2*inch
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • I defined some keywords(Length, Width etc..) in a module, like in Keys.py. then user import this Keys.py in his/her python script, and write: >>> Length=1mm >>> Width=3mm >>> print Length*Width, that is to say, I want to let "mm" as a python's syntax just like the complex numbers or scintific expressions to use for user. But your methord is good. thank you – Jack_wang May 18 '13 at 20:04
  • as the example you showd me, one would more like to just write >>> margin = 2inch, no a "*" between the num and the unit. That's what I want. Thank you – Jack_wang May 18 '13 at 20:24
  • you would need to use ply(or some other grammar library) and write a complete grammar that implements that as well as whatever other functionality you want to allow the user ... fair warning this is an incredible amount of work to avoid a `*` – Joran Beasley May 18 '13 at 21:05
0

If you are doing scientific work with physical units, it is a good idea to use a units library (not built-in) like quantities which also supports scientific packages like numpy. For example:

>>> from quantities import meter
>>> q = 1 * meter
>>> q.units = 'ft' # or 'foot' or 'feet'
>>> print q
3.280839895013123 ft
elyase
  • 39,479
  • 12
  • 112
  • 119
  • Thank you for sharing the module quantities. It's really very convinient for unit conversion. I'm afraid that it could not do what I want. Even I define q = 1 * meter, I still should not write a >>> 3q, which q is as a unit of meter. Am I right? – Jack_wang May 18 '13 at 20:12
  • I dont understand, do mean something like `from quantities import meter as q`, i.e renaming `meter`? You can define your own units and the variables will conserve its units if you do calculations with them. – elyase May 18 '13 at 20:17
  • Hi, as the example you showed for me, the last command ">>> print q", I'd like to know, can I write ">>> print 3q", I'm afraid that should be not use like that. thanks. – Jack_wang May 18 '13 at 20:27
  • not in the style 3*q, just 3q, q as an unit. Sorry for my urgly description. – Jack_wang May 18 '13 at 20:30
  • in your case, q = 1*meter, meter is a unit, one can use this unit, e.g., >>> lenth = 3*meter, but one should not write >>> lenth = 3meter. That's my point. I'm trying to "meter" as a unit which python can interprit, as python's syntax. – Jack_wang May 18 '13 at 20:35
  • No, that is not possible in Python. Some languages(like F#) have native support for units. Python doesn't, `3q` would be interpreted as a variable name(an invalid one because vars can't start with an number). – elyase May 18 '13 at 21:31
0

Python doens't have built-in units, so you'll need to install a package specifically for that.

Axiompy is a package that can do this.

Install with pip install axiompy

If you want to convert from millimetres to metres, like in the question, you'd use:

from axiompy import Units
units = Units()
print(units.unit_convert(3 * units.millimetre, units.metre))