Skip to main content

Android Termux Script for complex Engineering Units

·1 min

An advance engineering unit converter with special benefit in converting complex compounded units as shown in the video.

Try to organise scripts in adequately named folders

Steps required #

graph TD; A[πŸ“± Get Android] --> B[πŸ“¦ Install Termux] B --> C[🐍 Install Python] C --> D[βš™οΈ Install Python's Library] D --> E[πŸƒβ€β™‚οΈ Run Script]

Calculator script #

import pint

## Initialize the unit registry
unt = pint.UnitRegistry(autoconvert_offset_to_baseunit=True, auto_reduce_dimensions=True)

print('Usage: quantity<space>fromUnit<space>toUnit', 'Type end to stop loop', sep='\n')

while True:
    try:
        tmp = input('Input quantity & units: ')
        if tmp == 'end':
            break
        tmp = tmp.split()
        if len(tmp) != 3:
            raise ValueError("Input must be in the format: quantity fromUnit toUnit")

        quantity = float(tmp[0])
        from_unit = unt(tmp[1])
        to_unit = unt(tmp[2])

        result = (quantity * from_unit).to(to_unit)
        print(result)

    except ValueError as ve:
        print(f"ValueError: {ve}")
    except pint.UndefinedUnitError as ue:
        print(f"UndefinedUnitError: {ue}")
    except Exception as e:
        print(f"An error occurred: {e}")

    # Reset the input loop if an error occurs
    print("Please try again.")

Screencast #

Author
Usama Khan
Chemical Engineer by day & Coder at night