Hướng dẫn python unit conversion library
Python Engineering Unit Conversion Library is a current work in progress. The aim of the library is to provide users with easy management of engineering units and to avoid mistakes when working with and converting between different units of measurement. Custom units can be added by copying and pasting one of the generic classes and changing the data. This library will continue to be updated to include additional engineering units. This library is compatible with Python 3 and does not use any external dependencies. or or from EngUnitConversion import * Creating an EngUnit ObjectAll EngUnit objects are instantiated using a float value and a initial unit of measurement. The following code creates a Temperature with a value of 100 and unit of degree Fahrenheit. t1 = Temperature(100, Temperature.Unit.F) Units of MeasurementAll available unit conversions are stored in the EngUnit.Unit sub-class or can be called directly using the associated string. f1 = Force(10.5, Force.Unit.kN) is equivalent to Getting Value & Unitf1 = Force(10.5, Force.Unit.kN) print(f1.value) print(f1.unit) yields the following console output: Changing Units of MeasurementThe objects unit of measure can be changed by calling the changeUnit function. This returns the new value and stores the new value and engineering unit in the object. t1 = Temperature(100, Temperature.Unit.F) print(t1) print(t1.changeUnit('C')) print(t1) yields the following output: 100 F 37.77777777777783 37.77777777777783 C Notice how the function changeUnit() both returns the value as a float and changes the current instance of the object the new value unit. Working with UnitsWhen using EngUnit objects to perform operations, a new EngUnit object will be returned with the first object's unit by default. Additionp1 = Power(100, Power.Unit.kW) print(Power) p2 = Power(15000, Power.Unit.BTU_min) print(p2) p1 += p2 print(p1) yields the following output: 100 kW 15000 BTU/min 363.764089398442 kW Subtractiond1 = Length(100, Length.Unit.m) print(d1) d2 = Length(10, Length.Unit.ft) print(d2) d3 = d1 - d2 print(d3) d3.changeUnit(Length.Unit.inch) print(d3) yields the following output: 100 m 10 ft 96.952000097536 m 3817.0099390400023 inch Multiplication & Divisiont1 = Temperature(400, 'K') print(t1) t2 = 1.5 * t1 print(t2) t3 = t2.changeUnit('F') print(t3) t4 = t3 / 2 print(t4) output: 400 K 600.0 K 620.3299999999999 310.16499999999996 Note: Reverse Division is not defined. The following example will yield an error: If reverse division is required EngUnit.value should be used. For example: x = 2 / t3.value t4 = Temperature(x, 'F') License |