conversion of pressure units available units pascal bar kilopascal megapascal psipound per square inch inhgin mercury column torr atm usage import this file into their respective project use the function pressureconversion for conversion of pressure units parameters value the number of from units you want to convert fromtype from which type you want to convert totype to which type you want to convert references wikipedia reference https en wikipedia orgwikipascalunit wikipedia reference https en wikipedia orgwikipoundpersquareinch wikipedia reference https en wikipedia orgwikiinchofmercury wikipedia reference https en wikipedia orgwikitorr https en wikipedia orgwikistandardatmosphereunit https msestudent comwhataretheunitsofpressure https www unitconverters netpressureconverter html conversion between pressure units pressureconversion4 atm pascal 405300 pressureconversion1 pascal psi 0 00014401981999999998 pressureconversion1 bar atm 0 986923 pressureconversion3 kilopascal bar 0 029999991892499998 pressureconversion2 megapascal psi 290 074434314 pressureconversion4 psi torr 206 85984 pressureconversion1 inhg atm 0 0334211 pressureconversion1 torr psi 0 019336718261000002 pressureconversion4 wrongunit atm traceback most recent call last valueerror invalid fromtype value wrongunit supported values are atm pascal bar kilopascal megapascal psi inhg torr conversion between pressure units pressure_conversion 4 atm pascal 405300 pressure_conversion 1 pascal psi 0 00014401981999999998 pressure_conversion 1 bar atm 0 986923 pressure_conversion 3 kilopascal bar 0 029999991892499998 pressure_conversion 2 megapascal psi 290 074434314 pressure_conversion 4 psi torr 206 85984 pressure_conversion 1 inhg atm 0 0334211 pressure_conversion 1 torr psi 0 019336718261000002 pressure_conversion 4 wrongunit atm traceback most recent call last valueerror invalid from_type value wrongunit supported values are atm pascal bar kilopascal megapascal psi inhg torr
from typing import NamedTuple


class FromTo(NamedTuple):
    from_factor: float
    to_factor: float


PRESSURE_CONVERSION = {
    "atm": FromTo(1, 1),
    "pascal": FromTo(0.0000098, 101325),
    "bar": FromTo(0.986923, 1.01325),
    "kilopascal": FromTo(0.00986923, 101.325),
    "megapascal": FromTo(9.86923, 0.101325),
    "psi": FromTo(0.068046, 14.6959),
    "inHg": FromTo(0.0334211, 29.9213),
    "torr": FromTo(0.00131579, 760),
}


def pressure_conversion(value: float, from_type: str, to_type: str) -> float:
    
    if from_type not in PRESSURE_CONVERSION:
        raise ValueError(
            f"Invalid 'from_type' value: {from_type!r}  Supported values are:\n"
            + ", ".join(PRESSURE_CONVERSION)
        )
    if to_type not in PRESSURE_CONVERSION:
        raise ValueError(
            f"Invalid 'to_type' value: {to_type!r}.  Supported values are:\n"
            + ", ".join(PRESSURE_CONVERSION)
        )
    return (
        value
        * PRESSURE_CONVERSION[from_type].from_factor
        * PRESSURE_CONVERSION[to_type].to_factor
    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()