[a9d5684] | 1 | import re |
---|
| 2 | import string |
---|
| 3 | #Input -> new scale -> Output |
---|
| 4 | |
---|
| 5 | unit1 = "A^{-1} " # x A^{-1} |
---|
| 6 | unit2 = "A" # x A |
---|
| 7 | unit3 = "A" # x^2 A^{2} |
---|
| 8 | unit4 = "A " # 1/x A^{-1} |
---|
| 9 | unit5 = "A^{0.5} " # x^2 A |
---|
| 10 | unit9 = "m^{1/2}" # x^2 m |
---|
| 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} |
---|
| 16 | unit7 = "m/s^{2}" # 1/x (m/s^{2})^{-1} |
---|
| 17 | unit8 = "m/s^{4}" # x^2 (m/s^{4})^{2} |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | def UnitConvertion(pow, unit): |
---|
| 21 | """ |
---|
| 22 | """ |
---|
| 23 | if pow != 0: |
---|
| 24 | if string.find(unit, "^") != -1: # if the unit contains a power ^ |
---|
| 25 | unitSplitted = re.split("\^", unit) |
---|
| 26 | if string.find(unitSplitted[0], "/") != -1 or\ |
---|
| 27 | string.find(unitSplitted[0], "-") != -1 : # find slash / |
---|
| 28 | if pow == 1: |
---|
| 29 | unit = unit |
---|
| 30 | else: |
---|
| 31 | unit = "("+unit+")" + "^{"+ str(pow) + "}" |
---|
| 32 | else: |
---|
| 33 | if string.find(unitSplitted[1], "{") != -1: # if found a { |
---|
| 34 | findPower = re.split("{", unitSplitted[1]) |
---|
| 35 | if string.find(findPower[1], "}") != -1: # found } |
---|
| 36 | unitPower = re.split("}", findPower[1]) |
---|
| 37 | if(string.find(unitPower[0], ".") != -1): |
---|
| 38 | power = float(unitPower[0]) * pow |
---|
| 39 | elif(string.find(unitPower[0], "/") != -1): |
---|
| 40 | #power is a float |
---|
| 41 | poweSplitted = re.split("/", unitPower[0]) |
---|
| 42 | power = pow * int(poweSplitted[0])\ |
---|
| 43 | /int(poweSplitted[1]) |
---|
| 44 | else: |
---|
| 45 | power = int(unitPower[0]) * pow |
---|
| 46 | |
---|
| 47 | if power == 1.0: |
---|
| 48 | unit = unitSplitted[0] |
---|
| 49 | elif power == 0.5: |
---|
| 50 | unit = unitSplitted[0] + "^{1/2}" |
---|
| 51 | elif power == -0.5: |
---|
| 52 | unit = unitSplitted[0] + "^{-1/2}" |
---|
| 53 | else: |
---|
| 54 | unit = unitSplitted[0] + "^{" + str(power) + "}" |
---|
| 55 | else: |
---|
| 56 | raise ValueError, "missing } in unit expression" |
---|
| 57 | else: # no power |
---|
| 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 |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | if __name__ == "__main__": |
---|
| 66 | print "this unit1 %s ,its power %s , and value %s" % (unit1, |
---|
| 67 | 1, UnitConvertion(1, unit1)) |
---|
| 68 | print "this unit2 %s ,its power %s , and value %s" % (unit2, 1, |
---|
| 69 | UnitConvertion(1, unit2)) |
---|
| 70 | print "this unit3 %s ,its power %s , and value %s" % (unit3, 2, |
---|
| 71 | UnitConvertion(2, unit3)) |
---|
| 72 | print "this unit4 %s ,its power %s , and value %s" % (unit4, -1, |
---|
| 73 | UnitConvertion(-1, unit4)) |
---|
| 74 | print "this unit5 %s ,its power %s , and value %s" % (unit5, 2, |
---|
| 75 | UnitConvertion(2, unit5)) |
---|
| 76 | print "this unit6 %s ,its power %s , and value %s" % (unit6, 2, |
---|
| 77 | UnitConvertion(2, unit6)) |
---|
| 78 | print "this unit7 %s ,its power %s , and value %s" % (unit7, -1, |
---|
| 79 | UnitConvertion(-1, unit7)) |
---|
| 80 | print "this unit8 %s ,its power %s , and value %s" % (unit8, 2, |
---|
| 81 | UnitConvertion(2, unit8)) |
---|
| 82 | print "this unit9 %s ,its power %s , and value %s" % (unit9, 2, |
---|
| 83 | UnitConvertion(2, unit9)) |
---|