[959eb01] | 1 | """ |
---|
| 2 | Convert units to strings that can be displayed |
---|
| 3 | This is a cleaned up version of unitConverter.py |
---|
| 4 | """ |
---|
| 5 | import re |
---|
| 6 | import string |
---|
| 7 | |
---|
| 8 | def convert_unit(power, unit): |
---|
| 9 | """ |
---|
| 10 | Convert units to strings that can be displayed |
---|
| 11 | """ |
---|
| 12 | if power != 0: |
---|
| 13 | if string.find(unit, "^") != -1: # if the unit contains a powerer ^ |
---|
| 14 | toks = re.split("\^", unit) |
---|
| 15 | if string.find(toks[0], "/") != -1 or \ |
---|
| 16 | string.find(toks[0], "-") != -1: |
---|
| 17 | if power == 1: |
---|
| 18 | unit = unit |
---|
| 19 | else: |
---|
| 20 | unit = "(" + unit + ")" + "^{" + str(power) + "}" |
---|
| 21 | else: |
---|
| 22 | if string.find(toks[1], "{") != -1: # if found a { |
---|
| 23 | find_power_toks = re.split("{", toks[1]) |
---|
| 24 | if string.find(find_power_toks[1], "}") != -1: # found } |
---|
| 25 | unit_toks = re.split("}", find_power_toks[1]) |
---|
| 26 | if string.find(unit_toks[0], ".") != -1: |
---|
| 27 | powerer = float(unit_toks[0]) * power |
---|
| 28 | elif string.find(unit_toks[0], "/") != -1: |
---|
| 29 | power_toks = re.split("/", unit_toks[0]) |
---|
| 30 | powerer = power * int(power_toks[0])\ |
---|
| 31 | / int(power_toks[1]) |
---|
| 32 | else: |
---|
| 33 | powerer = int(unit_toks[0]) * power |
---|
| 34 | |
---|
| 35 | if powerer == 1.0: |
---|
| 36 | unit = toks[0] |
---|
| 37 | elif powerer == 0.5: |
---|
| 38 | unit = toks[0] + "^{1/2}" |
---|
| 39 | elif powerer == -0.5: |
---|
| 40 | unit = toks[0] + "^{-1/2}" |
---|
| 41 | else: |
---|
| 42 | unit = toks[0] + "^{" + str(powerer) + "}" |
---|
| 43 | else: |
---|
| 44 | raise ValueError, "missing } in unit expression" |
---|
| 45 | else: # no powerer |
---|
| 46 | if power != 1: |
---|
| 47 | unit = "(" + unit + ")" + "^{" + str(power) + "}" |
---|
| 48 | else: |
---|
| 49 | raise ValueError, "empty unit ,enter a powerer different from zero" |
---|
| 50 | return unit |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | if __name__ == "__main__": |
---|
| 54 | # pylint: disable=invalid-name |
---|
| 55 | # Input -> new scale -> Output |
---|
| 56 | unit1 = "A^{-1} " # x A^{-1} |
---|
| 57 | unit2 = "A" # x A |
---|
| 58 | unit3 = "A" # x^2 A^{2} |
---|
| 59 | unit4 = "A " # 1/x A^{-1} |
---|
| 60 | unit5 = "A^{0.5} " # x^2 A |
---|
| 61 | unit9 = "m^{1/2}" # x^2 m |
---|
| 62 | |
---|
| 63 | # If you don't recognize the pattern, give up |
---|
| 64 | # and just put some parentheses around the unit and write the transoformation: |
---|
| 65 | |
---|
| 66 | unit6 = "m/s" # x^2 (m/s)^{2} |
---|
| 67 | unit7 = "m/s^{2}" # 1/x (m/s^{2})^{-1} |
---|
| 68 | unit8 = "m/s^{4}" # x^2 (m/s^{4})^{2} |
---|
| 69 | |
---|
| 70 | print "this unit1 %s ,its powerer %s , and value %s" % (unit1, 1, convert_unit(1, unit1)) |
---|
| 71 | print "this unit2 %s ,its powerer %s , and value %s" % (unit2, 1, convert_unit(1, unit2)) |
---|
| 72 | print "this unit3 %s ,its powerer %s , and value %s" % (unit3, 2, convert_unit(2, unit3)) |
---|
| 73 | print "this unit4 %s ,its powerer %s , and value %s" % (unit4, -1, convert_unit(-1, unit4)) |
---|
| 74 | print "this unit5 %s ,its powerer %s , and value %s" % (unit5, 2, convert_unit(2, unit5)) |
---|
| 75 | print "this unit6 %s ,its powerer %s , and value %s" % (unit6, 2, convert_unit(2, unit6)) |
---|
| 76 | print "this unit7 %s ,its powerer %s , and value %s" % (unit7, -1, convert_unit(-1, unit7)) |
---|
| 77 | print "this unit8 %s ,its powerer %s , and value %s" % (unit8, 2, convert_unit(2, unit8)) |
---|
| 78 | print "this unit9 %s ,its powerer %s , and value %s" % (unit9, 2, convert_unit(2, unit9)) |
---|