[3dd29e3] | 1 | import re, math,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 and just put some parentheses around the unit and write the transoformation: |
---|
| 12 | |
---|
| 13 | unit6= "m/s" # x^2 (m/s)^{2} |
---|
| 14 | unit7= "m/s^{2}" # 1/x (m/s^{2})^{-1} |
---|
| 15 | unit8= "m/s^{4}" # x^2 (m/s^{4})^{2} |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | def UnitConvertion(pow,unit): |
---|
| 19 | if pow!=0: |
---|
| 20 | if string.find(unit, "^") !=-1:# if the unit contains a power ^ |
---|
| 21 | unitSplitted= re.split("\^",unit) |
---|
| 22 | if string.find(unitSplitted[0],"/")!=-1 or\ |
---|
| 23 | string.find(unitSplitted[0],"-")!=-1 : # find slash / |
---|
| 24 | if pow==1: |
---|
| 25 | unit = unit |
---|
| 26 | else: |
---|
| 27 | unit= "("+unit+")"+"^{"+str(pow)+"}" |
---|
| 28 | else: |
---|
| 29 | if string.find(unitSplitted[1],"{")!=-1:# if found a { |
---|
| 30 | findPower= re.split("{",unitSplitted[1]) |
---|
| 31 | if string.find(findPower[1],"}")!=-1:# found } |
---|
| 32 | unitPower= re.split("}",findPower[1]) |
---|
| 33 | if (string.find(unitPower[0],".")!=-1): |
---|
| 34 | power= float(unitPower[0])*pow |
---|
| 35 | elif (string.find(unitPower[0],"/") !=-1 ): #power is a float |
---|
| 36 | poweSplitted= re.split("/",unitPower[0]) |
---|
| 37 | power=pow*int(poweSplitted[0])/int(poweSplitted[1]) |
---|
| 38 | else: |
---|
| 39 | power= int(unitPower[0])*pow |
---|
| 40 | |
---|
| 41 | if power==1.0: |
---|
| 42 | unit = unitSplitted[0] |
---|
[43bf807] | 43 | elif power==0.5: |
---|
| 44 | unit = unitSplitted[0]+"^{1/2}" |
---|
| 45 | elif power==-0.5: |
---|
| 46 | unit = unitSplitted[0]+"^{-1/2}" |
---|
[3dd29e3] | 47 | else: |
---|
| 48 | unit= unitSplitted[0]+"^{"+str(power)+"}" |
---|
| 49 | else: |
---|
| 50 | raise ValueError, "missing } in unit expression" |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | else:#no power |
---|
| 55 | if pow !=1: |
---|
| 56 | unit="("+unit+")"+"^{"+str(pow)+"}" |
---|
| 57 | else: |
---|
| 58 | raise ValueError, "empty unit ,enter a power different from zero" |
---|
| 59 | return unit |
---|
| 60 | if __name__ == "__main__": |
---|
| 61 | print "this unit1 %s ,its power %s , and value %s"%(unit1,1,UnitConvertion(1,unit1)) |
---|
| 62 | print "this unit2 %s ,its power %s , and value %s"%(unit2,1,UnitConvertion(1,unit2)) |
---|
| 63 | print "this unit3 %s ,its power %s , and value %s"%(unit3,2,UnitConvertion(2,unit3)) |
---|
| 64 | print "this unit4 %s ,its power %s , and value %s"%(unit4,-1,UnitConvertion(-1,unit4)) |
---|
| 65 | print "this unit5 %s ,its power %s , and value %s"%(unit5,2,UnitConvertion(2,unit5)) |
---|
| 66 | print "this unit6 %s ,its power %s , and value %s"%(unit6,2,UnitConvertion(2,unit6)) |
---|
| 67 | print "this unit7 %s ,its power %s , and value %s"%(unit7,-1,UnitConvertion(-1,unit7)) |
---|
| 68 | print "this unit8 %s ,its power %s , and value %s"%(unit8,2,UnitConvertion(2,unit8)) |
---|
| 69 | print "this unit9 %s ,its power %s , and value %s"%(unit9,2,UnitConvertion(2,unit9)) |
---|
| 70 | |
---|