MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / pressure_conversion

Function pressure_conversion

conversions/pressure_conversions.py:42–81  ·  view source on GitHub ↗

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.0

(value: float, from_type: str, to_type: str)

Source from the content-addressed store, hash-verified

40
41
42def pressure_conversion(value: float, from_type: str, to_type: str) -> float:
43 """
44 Conversion between pressure units.
45 >>> pressure_conversion(4, "atm", "pascal")
46 405300
47 >>> pressure_conversion(1, "pascal", "psi")
48 0.00014401981999999998
49 >>> pressure_conversion(1, "bar", "atm")
50 0.986923
51 >>> pressure_conversion(3, "kilopascal", "bar")
52 0.029999991892499998
53 >>> pressure_conversion(2, "megapascal", "psi")
54 290.074434314
55 >>> pressure_conversion(4, "psi", "torr")
56 206.85984
57 >>> pressure_conversion(1, "inHg", "atm")
58 0.0334211
59 >>> pressure_conversion(1, "torr", "psi")
60 0.019336718261000002
61 >>> pressure_conversion(4, "wrongUnit", "atm")
62 Traceback (most recent call last):
63 ...
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
79 * PRESSURE_CONVERSION[from_type].from_factor
80 * PRESSURE_CONVERSION[to_type].to_factor
81 )
82
83
84if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected