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