|
| 1 | +""" |
| 2 | +Conversion of pressure units. |
| 3 | +Available Units:- Pascal,Bar,Kilopascal,Megapascal,psi(pound per square inch), |
| 4 | +inHg(in mercury column),torr,atm |
| 5 | +USAGE : |
| 6 | +-> Import this file into their respective project. |
| 7 | +-> Use the function pressure_conversion() for conversion of pressure units. |
| 8 | +-> Parameters : |
| 9 | + -> value : The number of from units you want to convert |
| 10 | + -> from_type : From which type you want to convert |
| 11 | + -> to_type : To which type you want to convert |
| 12 | +REFERENCES : |
| 13 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Pascal_(unit) |
| 14 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Pound_per_square_inch |
| 15 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Inch_of_mercury |
| 16 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Torr |
| 17 | +-> https://en.wikipedia.org/wiki/Standard_atmosphere_(unit) |
| 18 | +-> https://msestudent.com/what-are-the-units-of-pressure/ |
| 19 | +-> https://www.unitconverters.net/pressure-converter.html |
| 20 | +""" |
| 21 | + |
| 22 | +from collections import namedtuple |
| 23 | + |
| 24 | +from_to = namedtuple("from_to", "from_ to") |
| 25 | + |
| 26 | +PRESSURE_CONVERSION = { |
| 27 | + "atm": from_to(1, 1), |
| 28 | + "pascal": from_to(0.0000098, 101325), |
| 29 | + "bar": from_to(0.986923, 1.01325), |
| 30 | + "kilopascal": from_to(0.00986923, 101.325), |
| 31 | + "megapascal": from_to(9.86923, 0.101325), |
| 32 | + "psi": from_to(0.068046, 14.6959), |
| 33 | + "inHg": from_to(0.0334211, 29.9213), |
| 34 | + "torr": from_to(0.00131579, 760), |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +def pressure_conversion(value: float, from_type: str, to_type: str) -> float: |
| 39 | + """ |
| 40 | + Conversion between pressure units. |
| 41 | + >>> pressure_conversion(4, "atm", "pascal") |
| 42 | + 405300 |
| 43 | + >>> pressure_conversion(1, "pascal", "psi") |
| 44 | + 0.00014401981999999998 |
| 45 | + >>> pressure_conversion(1, "bar", "atm") |
| 46 | + 0.986923 |
| 47 | + >>> pressure_conversion(3, "kilopascal", "bar") |
| 48 | + 0.029999991892499998 |
| 49 | + >>> pressure_conversion(2, "megapascal", "psi") |
| 50 | + 290.074434314 |
| 51 | + >>> pressure_conversion(4, "psi", "torr") |
| 52 | + 206.85984 |
| 53 | + >>> pressure_conversion(1, "inHg", "atm") |
| 54 | + 0.0334211 |
| 55 | + >>> pressure_conversion(1, "torr", "psi") |
| 56 | + 0.019336718261000002 |
| 57 | + >>> pressure_conversion(4, "wrongUnit", "atm") |
| 58 | + Traceback (most recent call last): |
| 59 | + File "/usr/lib/python3.8/doctest.py", line 1336, in __run |
| 60 | + exec(compile(example.source, filename, "single", |
| 61 | + File "<doctest __main__.pressure_conversion[8]>", line 1, in <module> |
| 62 | + pressure_conversion(4, "wrongUnit", "atm") |
| 63 | + File "<string>", line 67, in pressure_conversion |
| 64 | + ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: |
| 65 | + atm, pascal, bar, kilopascal, megapascal, psi, inHg, torr |
| 66 | + """ |
| 67 | + if from_type not in PRESSURE_CONVERSION: |
| 68 | + raise ValueError( |
| 69 | + f"Invalid 'from_type' value: {from_type!r} Supported values are:\n" |
| 70 | + + ", ".join(PRESSURE_CONVERSION) |
| 71 | + ) |
| 72 | + if to_type not in PRESSURE_CONVERSION: |
| 73 | + raise ValueError( |
| 74 | + f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n" |
| 75 | + + ", ".join(PRESSURE_CONVERSION) |
| 76 | + ) |
| 77 | + return ( |
| 78 | + value * PRESSURE_CONVERSION[from_type].from_ * PRESSURE_CONVERSION[to_type].to |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + import doctest |
| 84 | + |
| 85 | + doctest.testmod() |
0 commit comments