From 76d8b93246dade3a30ce21957a4b51c77781b5b6 Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Tue, 26 Oct 2021 21:36:01 +0530 Subject: [PATCH 1/5] Added physical pressure units This uses tuple pair which stores units required to be converted to respective other units as mentioned. Available Units:- Pascal,Bar,Kilopascal,Megapascal,psi(pound per square inch),inHg(in mercury column),torr,atm --- conversions/pressure_conversions.py | 80 +++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 conversions/pressure_conversions.py diff --git a/conversions/pressure_conversions.py b/conversions/pressure_conversions.py new file mode 100644 index 000000000000..076ec618eb42 --- /dev/null +++ b/conversions/pressure_conversions.py @@ -0,0 +1,80 @@ +""" +Conversion of pressure units. +Available Units:- Pascal,Bar,Kilopascal,Megapascal,psi(pound per square inch),inHg(in mercury column),torr,atm +USAGE : +-> Import this file into their respective project. +-> Use the function pressure_conversion() for conversion of pressure units. +-> Parameters : + -> value : The number of from units you want to convert + -> from_type : From which type you want to convert + -> to_type : To which type you want to convert +REFERENCES : +-> Wikipedia reference: https://en.wikipedia.org/wiki/Pascal_(unit) +-> Wikipedia reference: https://en.wikipedia.org/wiki/Pound_per_square_inch +-> Wikipedia reference: https://en.wikipedia.org/wiki/Inch_of_mercury +-> Wikipedia reference: https://en.wikipedia.org/wiki/Torr +-> https://en.wikipedia.org/wiki/Standard_atmosphere_(unit) +""" + +from collections import namedtuple + +from_to = namedtuple("from_to", "from_ to") + +PRESSURE_CONVERSION = { + "atm": from_to(1, 1), + "pascal": from_to(0.0000098, 101325), + "bar": from_to(0.986923, 1.01325), + "kilopascal": from_to(0.00986923, 101.325), + "megapascal": from_to(9.86923, 0.101325), + "psi": from_to(0.068046, 14.6959), + "inHg": from_to(0.0334211, 29.9213), + "torr": from_to(0.00131579, 760) +} + + +def pressure_conversion(value: float, from_type: str, to_type: str) -> float: + """ + 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): + File "/usr/lib/python3.8/doctest.py", line 1336, in __run + exec(compile(example.source, filename, "single", + File "", line 1, in + pressure_conversion(4, "wrongUnit", "atm") + File "", line 67, in pressure_conversion + ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: + atm, pascal, bar, kilopascal, megapascal, psi, inHg, torr + """ + 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_ * PRESSURE_CONVERSION[to_type].to + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 338f95a957cb1b8832c41287847b1c9664c9e468 Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Tue, 26 Oct 2021 21:41:28 +0530 Subject: [PATCH 2/5] Formatted file File was formatted as per repo rules --- conversions/pressure_conversions.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/conversions/pressure_conversions.py b/conversions/pressure_conversions.py index 076ec618eb42..a733fc368a79 100644 --- a/conversions/pressure_conversions.py +++ b/conversions/pressure_conversions.py @@ -28,7 +28,7 @@ "megapascal": from_to(9.86923, 0.101325), "psi": from_to(0.068046, 14.6959), "inHg": from_to(0.0334211, 29.9213), - "torr": from_to(0.00131579, 760) + "torr": from_to(0.00131579, 760), } @@ -71,7 +71,9 @@ def pressure_conversion(value: float, from_type: str, to_type: str) -> float: f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n" + ", ".join(PRESSURE_CONVERSION) ) - return value * PRESSURE_CONVERSION[from_type].from_ * PRESSURE_CONVERSION[to_type].to + return ( + value * PRESSURE_CONVERSION[from_type].from_ * PRESSURE_CONVERSION[to_type].to + ) if __name__ == "__main__": From 6750fbc24708ea281943b789679f63eb373c8605 Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Tue, 26 Oct 2021 21:46:40 +0530 Subject: [PATCH 3/5] Reformatted file :) --- conversions/pressure_conversions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/conversions/pressure_conversions.py b/conversions/pressure_conversions.py index a733fc368a79..cf6a380f99be 100644 --- a/conversions/pressure_conversions.py +++ b/conversions/pressure_conversions.py @@ -1,6 +1,7 @@ """ Conversion of pressure units. -Available Units:- Pascal,Bar,Kilopascal,Megapascal,psi(pound per square inch),inHg(in mercury column),torr,atm +Available Units:- Pascal,Bar,Kilopascal,Megapascal,psi(pound per square inch), +inHg(in mercury column),torr,atm USAGE : -> Import this file into their respective project. -> Use the function pressure_conversion() for conversion of pressure units. From f6c3209dfa33e6e82a3e55bd45a8196621703d50 Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Tue, 26 Oct 2021 22:19:32 +0530 Subject: [PATCH 4/5] Added more reference --- conversions/pressure_conversions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conversions/pressure_conversions.py b/conversions/pressure_conversions.py index cf6a380f99be..4d6c806cfa83 100644 --- a/conversions/pressure_conversions.py +++ b/conversions/pressure_conversions.py @@ -15,6 +15,7 @@ -> Wikipedia reference: https://en.wikipedia.org/wiki/Inch_of_mercury -> Wikipedia reference: https://en.wikipedia.org/wiki/Torr -> https://en.wikipedia.org/wiki/Standard_atmosphere_(unit) +-> https://msestudent.com/what-are-the-units-of-pressure/ """ from collections import namedtuple From 4b9973050df8584369fb47a8df970aa650f33d0f Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Tue, 26 Oct 2021 22:59:37 +0530 Subject: [PATCH 5/5] More reference added --- conversions/pressure_conversions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conversions/pressure_conversions.py b/conversions/pressure_conversions.py index 4d6c806cfa83..2018080b9327 100644 --- a/conversions/pressure_conversions.py +++ b/conversions/pressure_conversions.py @@ -16,6 +16,7 @@ -> Wikipedia reference: https://en.wikipedia.org/wiki/Torr -> https://en.wikipedia.org/wiki/Standard_atmosphere_(unit) -> https://msestudent.com/what-are-the-units-of-pressure/ +-> https://www.unitconverters.net/pressure-converter.html """ from collections import namedtuple