Engfmt(Engineering Format) Documentation

Link : https://pypi.python.org/pypi/engfmt/1.1.0

engfmt – It is a package used to read and write numbers in engineering format.

In engineering format a number generally includes the units if available and uses SI scale factors to indicate the magnitude of the number.

For example:

1ns

1.4204GHz

This package is designed to convert quantities between the various ways in which they are represented. Those ways are:

As a tuple:

For Eg: “1ns” – would be represented as (1e-9, ‘s’).

As a string in conventional formats:

For Eg: “1ns” – would be represented as ‘1e-9 s’ or as ‘0.000000001s’.

– This form is difficult to read for people.

– engfmt treats it more as a format meant for machines rather than people.

As a string in engineering format:

For Eg: “1ns” – would be represented as ‘1ns’.

– This form is difficult to read for machines

– engfmt treats it more as a human readable format.

The Quantity class is provided for converting between these various forms.

  • A quantity is the pairing of a real number and units, though the units may be empty.

1.It takes one or two arguments.

Eg: Quantity(1e-9) or Quantity(1e-9, ‘s’)

  • The first is taken to be the value, and the second, if given, is taken to be the units.
  • The value may be given as a float or as a string.
  • The string may be in floating point notation, in scientific notation, or in engineering format and may include the units.

Some examples of the conversions are given below:

For example, any of the following ways can be used to specify 1ns:

As a tuple:

>>> from engfmt import Quantity

>>> period = Quantity(1e-9, ‘s’)

>>> print(period)

1ns

As a string in conventional formats:

>>> period = Quantity(‘0.000000001 s’)

>>> print(period)

1ns

>>> period = Quantity(‘1e-9s’)

>>> print(period)

1ns

As a string in engineering format:

>>> period = Quantity(‘1ns’)

>>> print(period)

       1ns

Note: In all cases, the giving the units is optional.

Conversions – Using quantity object you are able to convert it to any representations:

>>> h_line = Quantity(‘1420.405751786 MHz’)

# to converting to tuple with units

>>> h_line.to_tuple()

       (1420405751.786, ‘Hz’)

# to converting to string with engineering format

>>> h_line.to_eng()

       ‘1.4204GHz’

# to converting to string with conventional format

>>> h_line.to_str()

        ‘1420.405751786e6Hz’

You can also access the value without the units:

# convertig to float

>>> h_line.to_float()

       1420405751.786

# converting to a string with conventional format – without units

>>> h_line.to_unitless_eng()

       ‘1.4204G’

# converting to string with engineering format – without units

>>> h_line.to_unitless_str()

       ‘1420.405751786e6’

# Get the units only

>>> h_line.units

        ‘Hz’

There are many other functions also in this package, which is available in the link given.

Some of the functions are given below:

  • Quantities As Reals – You can use a quantity in the same way that you can use a real number, meaning that you can use it in expressions and it will evaluate to its real value.

For Example:

     >>> period = Quantity(‘1us’)

     >> print(period) 

         1us

  • Quantity Class – Quantity class can be used directly, like shortcut functions.

      For Example:

      >>> from engfmt import Quantity

      >>> h_line = Quantity(‘1420.405751786 MHz’)

      >>> str(h_line)

           ‘1.4204GHz’

      >>> float(h_line)

            1420405751.786

  • Physical Constants – The Quantity class also supports a some physical constants.

      For Example:

     Plank’s constant:

      >>> plank = Quantity(‘h’)

      >>> print(plank)

            662.61e-36J-s

  • Exceptions:

The best thing in this is it also gives Exceptions:

Eg: A ValueError is raised if engfmt cannot convert a string it into a number:

For Example:
			>>> try:
			...     value, units = quant_to_tuple('xxx')
			... except ValueError as err:
			...     print(err)
			xxx: not a valid number.

Installation / Usage :

Requires Python2.7 or Python3.3 or better.

1. Use ‘pip install engfmt’ in the cmd prompt to install.

2. By importing the .py file:

  • Download the package from the link.
  • Copy the engfmt.py from the package and put in the working directory of the python.
  • Import the file by from engfmt import* / from engfmt import Quantity.