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