From b8536dbd319b556d4269c0269cf506a6c7a20045 Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Tue, 26 Oct 2021 19:10:30 +0530 Subject: [PATCH 1/3] Added volume conversions This is a file which has relevant function which helps in conversion between volume units. Available Units:- Cubic metre,Litre,KiloLitre,Gallon,Cubic yard,Cubic foot,cup The file is also written in a way that , adding a new unit can easily be done by modifying tuple available in the source code --- conversions/volume_conversions.py | 80 +++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 conversions/volume_conversions.py diff --git a/conversions/volume_conversions.py b/conversions/volume_conversions.py new file mode 100644 index 000000000000..43676b6b72e1 --- /dev/null +++ b/conversions/volume_conversions.py @@ -0,0 +1,80 @@ +""" +Conversion of volume units. +Available Units:- Cubic metre,Litre,KiloLitre,Gallon,Cubic yard,Cubic foot,cup +USAGE : +-> Import this file into their respective project. +-> Use the function length_conversion() for conversion of volume 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/Cubic_metre +-> Wikipedia reference: https://en.wikipedia.org/wiki/Litre +-> Wikipedia reference: https://en.wiktionary.org/wiki/kilolitre +-> Wikipedia reference: https://en.wikipedia.org/wiki/Gallon +-> Wikipedia reference: https://en.wikipedia.org/wiki/Cubic_yard +-> Wikipedia reference: https://en.wikipedia.org/wiki/Cubic_foot +-> Wikipedia reference: https://en.wikipedia.org/wiki/Cup_(unit) +""" + +from collections import namedtuple + +from_to = namedtuple("from_to", "from_ to") + +METRIC_CONVERSION = { + + "cubicmeter": from_to(1, 1), + "litre": from_to(0.001, 1000), + "kilolitre": from_to(1, 1), + "gallon": from_to(0.00454, 264.172), + "cubicyard": from_to(0.76455, 1.30795), + "cubicfoot": from_to(0.028, 35.3147), + "cup": from_to(0.000236588, 4226.75) +} + + +def volume_conversion(value: float, from_type: str, to_type: str) -> float: + """ + Conversion between volume units. + >>> volume_conversion(4, "cubicmeter", "litre") + 4000 + >>> volume_conversion(1, "litre", "gallon") + 0.264172 + >>> volume_conversion(1, "kilolitre", "cubicmeter") + 1 + >>> volume_conversion(3, "gallon", "cubicyard") + 0.017814279 + >>> volume_conversion(2, "cubicyard", "litre") + 1529.1 + >>> volume_conversion(4, "cubicfoot", "cup") + 473.396 + >>> volume_conversion(1, "cup", "kilolitre") + 0.000236588 + >>> volume_conversion(4, "wrongUnit", "litre") + 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 + volume_conversion(4, "wrongUnit", "litre") + File "", line 62, in volume_conversion + ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: + cubicmeter, litre, kilolitre, gallon, cubicyard, cubicfoot, cup + """ + if from_type not in METRIC_CONVERSION: + raise ValueError( + f"Invalid 'from_type' value: {from_type!r} Supported values are:\n" + + ", ".join(METRIC_CONVERSION) + ) + if to_type not in METRIC_CONVERSION: + raise ValueError( + f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n" + + ", ".join(METRIC_CONVERSION) + ) + return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From f4fe0dc462de8acf72976b25762e4505f5b0c1de Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Tue, 26 Oct 2021 19:18:35 +0530 Subject: [PATCH 2/3] Formatted file The file was formatted to follow the syntax formatting rules of the repo --- conversions/volume_conversions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/volume_conversions.py b/conversions/volume_conversions.py index 43676b6b72e1..a3beb120954f 100644 --- a/conversions/volume_conversions.py +++ b/conversions/volume_conversions.py @@ -23,20 +23,20 @@ from_to = namedtuple("from_to", "from_ to") METRIC_CONVERSION = { - "cubicmeter": from_to(1, 1), "litre": from_to(0.001, 1000), "kilolitre": from_to(1, 1), "gallon": from_to(0.00454, 264.172), "cubicyard": from_to(0.76455, 1.30795), "cubicfoot": from_to(0.028, 35.3147), - "cup": from_to(0.000236588, 4226.75) + "cup": from_to(0.000236588, 4226.75), } def volume_conversion(value: float, from_type: str, to_type: str) -> float: """ Conversion between volume units. + >>> volume_conversion(4, "cubicmeter", "litre") 4000 >>> volume_conversion(1, "litre", "gallon") From 087bf9502fa2425b386149173cf283e0676ae96a Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Tue, 26 Oct 2021 19:21:55 +0530 Subject: [PATCH 3/3] Formatted file further --- conversions/volume_conversions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/conversions/volume_conversions.py b/conversions/volume_conversions.py index a3beb120954f..de2290196fc2 100644 --- a/conversions/volume_conversions.py +++ b/conversions/volume_conversions.py @@ -36,7 +36,6 @@ def volume_conversion(value: float, from_type: str, to_type: str) -> float: """ Conversion between volume units. - >>> volume_conversion(4, "cubicmeter", "litre") 4000 >>> volume_conversion(1, "litre", "gallon")