Easy Engineering Unit Conversion

Easy Engineering Unit Conversion

Working with and converting between engineering units can be a daunting task. Calculations and conversions between the US Customary and Metric systems can lead to errors and frustration. To make life simpler I have created a Python library to make conversions and operations simple and less error-prone. The code is available for free under GNU 3 Public License at https://github.com/fjpereny/Engineering-Units-Conversion/.

Description

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.

Supported Units

  • Temperature
  • Pressure
  • Mass
  • Power
  • Time
  • Force
  • Electric Current
  • Length

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.

Installation & Dependencies

This library is compatible with Python 3 and does not use any external dependencies.

Usage

Adding EngUnitConversion to your project:

import EngUnitConversion

or

import EngUnitConversion as EUC

or

from EngUnitConversion import *

Creating an EngUnit Object

All EngUnit objects are instantiated using a float value and an 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 Measurement

All 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

f1 = Force(10.5, 'kN')

Getting Value & Unit

f1 = Force(10.5, Force.Unit.kN)
print(f1.value)
print(f1.unit)

yields the following console output:

10.5
kN

Changing Units of Measurement

The 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 changes the current instance of the object the new value unit.

Working with Units

When using EngUnit objects to perform operations, a new EngUnit object will be returned with the first object's unit by default.

Addition Example

p1 = 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

Subtraction Example

d1 = 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 & Division Example

t1 = 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:

t4 = 2 / t3

If reverse division is required EngUnit.value should be used. For example:

x = 2 / t3.value
t4 = Temperature(x, 'F')


To view or add a comment, sign in

More articles by Franklin Pereny, PMP, MSME

Explore content categories