Mechpy Tutorials

a mechanical engineering toolbox

source code - https://github.com/nagordon/mechpy
documentation - https://nagordon.github.io/mechpy/


Neal Gordon
2017-02-20


Units

Neal Gordon
2017-2-20

Not a lot of effort was put towards make a new units module because there are already some good ones out there. This is bascially a demo of them, and can be bundled into an executable so that python isn't required. This demo of the units module is a few different attempts at creating a fast, converter for units. unitconvert1 function or uc1 uses the sympy.physics package. The uc2 package uses the pint module. And uc3 uses the python-quantities package .

In [3]:
from units import uc1, uc3
# uc1 uses sympy
In [4]:
uc1(1.0,'psi','kPa')
1.00 psi = 6.89  kPa 
In [5]:
uc1(1.0,'newton','pound')
1.00 newton = 0.22  pound 
In [6]:
help(uc3)
Help on function uc3 in module units:

uc3(numin, frm, to)
    quantities
    uc3 - unit convert 3
    https://github.com/python-quantities/python-quantities
    https://pythonhosted.org/quantities/user/tutorial.html
    
    see here for details on all units 
    https://github.com/python-quantities/python-quantities/tree/master/quantities/units
    
    uc3(1,'inch','ft')
    
    angle
       radian/rad, turn/cycle, deg/degree
      
    distance
       inch/in, feet/ft, m/meter, mil/thou, mile/mi,, yard/yd, fathom, light_year, kilometer/km, nautical_mile/nmi
    
    temperature
        celsius/degC, fahrenheit/degF, kelvin/degK
    
    force
        newton/N, ozf/fource_ounce, lbf/pound_force
    
    pressure
        pascal/Pa, kilopascal/kPa, psi, mmHg
    
    energy
        cal/calorie, J/joule, btu, ton_TNT, kWh/kilowatthour
    
    power
        watt/W, horsepower/hp

In [7]:
for d in ['ft', 'meter', 'mil', 'mile','inch', 'yard', 'fathom', 'light_year']:
    uc3(1,'ft',d)
1.0000 ft = 1.0000  ft 
1.0000 ft = 0.3048  meter 
1.0000 ft = 12000.0000  mil 
1.0000 ft = 0.0002  mile 
1.0000 ft = 12.0000  inch 
1.0000 ft = 0.3333  yard 
1.0000 ft = 0.1667  fathom 
1.0000 ft = 0.0000  light_year 
In [8]:
uc3(1,'psi','kPa')
1.0000 psi = 6.8948  kPa 
In [9]:
from units import in_mm
in_mm()
     0 in - 0.000000 in - 0.000000 mm 
  1/16 in - 0.062500 in - 1.587500 mm 
   1/8 in - 0.125000 in - 3.175000 mm 
  3/16 in - 0.187500 in - 4.762500 mm 
   1/4 in - 0.250000 in - 6.350000 mm 
  5/16 in - 0.312500 in - 7.937500 mm 
   3/8 in - 0.375000 in - 9.525000 mm 
  7/16 in - 0.437500 in - 11.112500 mm 
   1/2 in - 0.500000 in - 12.700000 mm 
  9/16 in - 0.562500 in - 14.287500 mm 
   5/8 in - 0.625000 in - 15.875000 mm 
 11/16 in - 0.687500 in - 17.462500 mm 
   3/4 in - 0.750000 in - 19.050000 mm 
 13/16 in - 0.812500 in - 20.637500 mm 
   7/8 in - 0.875000 in - 22.225000 mm 
 15/16 in - 0.937500 in - 23.812500 mm 
     1 in - 1.000000 in - 25.400000 mm 
In [ ]: