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