From 7a4062a93d98f3748da7d9505e297695a1378cfa Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Sun, 17 Oct 2021 16:25:30 +0530 Subject: [PATCH 01/13] Added length unit conversions Conversion of length units were added with respective tests being implemented and passed. Available Units:- Metre,Kilometre,Feet,Inch,Centimeter,Yard,Foot,Mile,Millimeter --- conversions/length_conversions.py | 112 ++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 conversions/length_conversions.py diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py new file mode 100644 index 000000000000..3f8a27884991 --- /dev/null +++ b/conversions/length_conversions.py @@ -0,0 +1,112 @@ + +""" +Conversion of length units. +Available Units:- Metre,Kilometre,Feet,Inch,Centimeter,Yard,Foot,Mile,Millimeter + +USAGE : +-> Import this file into their respective project. +-> Use the function length_conversion() for conversion of length units. +-> Parameters : + -> from_type : From which type you want to convert + -> to_type : To which type you want to convert + -> value : the value which you want to convert + +REFERENCES : +-> Wikipedia reference: https://en.wikipedia.org/wiki/Meter +-> Wikipedia reference: https://en.wikipedia.org/wiki/Kilometer +-> Wikipedia reference: https://en.wikipedia.org/wiki/Feet +-> Wikipedia reference: https://en.wikipedia.org/wiki/Inch +-> Wikipedia reference: https://en.wikipedia.org/wiki/Centimeter +-> Wikipedia reference: https://en.wikipedia.org/wiki/Yard +-> Wikipedia reference: https://en.wikipedia.org/wiki/Foot +-> Wikipedia reference: https://en.wikipedia.org/wiki/Mile +-> Wikipedia reference: https://en.wikipedia.org/wiki/Millimeter +""" + +METER_CHART: dict[str, float] = { + "meter": 1, + "kilometer": 0.001, + "feet": 3.28084, + "inch": 39.3701, + "centimeter": 100, + "yard": 1.09361, + "foot": 3.28084, + "mile": 0.000621371, + "millimeter": 1000, +} + +LENGTH_TYPE_CHART: dict[str, float] = { + "meter": 1, + "kilometer": 1000, + "feet": 0.3048, + "inch": 0.0254, + "centimeter": 0.01, + "yard": 0.9144, + "foot": 0.3048, + "mile": 1609.34, + "millimeter": 0.001, +} + + +def length_conversion(from_type: str, to_type: str, value: float) -> float: + """ + Conversion of length unit with the help of LENGTH_CHART + "meter": 1, + "kilometer": 0.001, + "feet": 3.28084, + "inch": 39.3701, + "centimeter": 100, + "yard": 1.09361, + "foot": 3.28084, + "mile": 0.000621371, + "millimeter": 1000, + >>> length_conversion("meter","feet",4) + 13.12336 + >>> length_conversion("meter","kilometer",1) + 0.001 + >>> length_conversion("kilometer","inch",1) + 39370.1 + >>> length_conversion("kilometer","mile",3) + 1.8641130000000001 + >>> length_conversion("feet","meter",2) + 0.6096 + >>> length_conversion("feet","yard",4) + 1.333329312 + >>> length_conversion("inch","meter",1) + 0.0254 + >>> length_conversion("inch","mile",2) + 3.15656468e-05 + >>> length_conversion("centimeter","millimeter",2) + 20.0 + >>> length_conversion("centimeter","yard",2) + 0.0218722 + >>> length_conversion("yard","meter",4) + 3.6576 + >>> length_conversion("yard","kilometer",4) + 0.0036576 + >>> length_conversion("foot","meter",3) + 0.9144000000000001 + >>> length_conversion("foot","inch",3) + 36.00001944 + >>> length_conversion("mile","kilometer",4) + 6.43736 + >>> length_conversion("mile","inch",2) + 126719.753468 + >>> length_conversion("millimeter","centimeter",3) + 0.3 + >>> length_conversion("millimeter","inch",3) + 0.1181103 + """ + if to_type not in METER_CHART or from_type not in LENGTH_TYPE_CHART: + raise ValueError( + f"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" + f"Supported values are: {', '.join(LENGTH_TYPE_CHART)}" + ) + return value * METER_CHART[to_type] * LENGTH_TYPE_CHART[from_type] + + +if __name__ == "__main__": + + import doctest + + doctest.testmod() \ No newline at end of file From 9c91d516369758ec09161392738d7ebbd9264090 Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Sun, 17 Oct 2021 16:34:20 +0530 Subject: [PATCH 02/13] Formatted File File was formatted to go as per repo rules --- conversions/length_conversions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index 3f8a27884991..2070aa86f84f 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -109,4 +109,5 @@ def length_conversion(from_type: str, to_type: str, value: float) -> float: import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() + From 9686aebeba1b5879729546d675a189b1d1980850 Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Sun, 17 Oct 2021 16:42:21 +0530 Subject: [PATCH 03/13] Reformatted file --- conversions/length_conversions.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index 2070aa86f84f..7b0d2235ae30 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -47,7 +47,6 @@ "millimeter": 0.001, } - def length_conversion(from_type: str, to_type: str, value: float) -> float: """ Conversion of length unit with the help of LENGTH_CHART @@ -104,10 +103,6 @@ def length_conversion(from_type: str, to_type: str, value: float) -> float: ) return value * METER_CHART[to_type] * LENGTH_TYPE_CHART[from_type] - if __name__ == "__main__": - import doctest - doctest.testmod() - From 7e7bc512cdb54c4853b59e061839d9bc31be8875 Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Sun, 17 Oct 2021 16:49:53 +0530 Subject: [PATCH 04/13] Reformatted code once again --- conversions/length_conversions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index 7b0d2235ae30..0ba599dc80b2 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -1,4 +1,3 @@ - """ Conversion of length units. Available Units:- Metre,Kilometre,Feet,Inch,Centimeter,Yard,Foot,Mile,Millimeter @@ -47,6 +46,7 @@ "millimeter": 0.001, } + def length_conversion(from_type: str, to_type: str, value: float) -> float: """ Conversion of length unit with the help of LENGTH_CHART @@ -103,6 +103,8 @@ def length_conversion(from_type: str, to_type: str, value: float) -> float: ) return value * METER_CHART[to_type] * LENGTH_TYPE_CHART[from_type] + if __name__ == "__main__": import doctest + doctest.testmod() From b46266fbe44e568174f6cad11f372508afe5b52d Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Mon, 18 Oct 2021 22:41:11 +0530 Subject: [PATCH 05/13] Added more test Added test to evaluate whether the code handles wrong arguements passed --- conversions/length_conversions.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index 0ba599dc80b2..271de4ef9d4d 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -95,6 +95,15 @@ def length_conversion(from_type: str, to_type: str, value: float) -> float: 0.3 >>> length_conversion("millimeter","inch",3) 0.1181103 + >>> length_conversion("wrongUnit","inch",4 ) + 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 + length_conversion("wrongUnit","inch",4 ) + File "", line 104, in length_conversion + ValueError: Invalid 'from_type' or 'to_type' value: 'wrongUnit', 'inch' + Supported values are: meter, kilometer, feet, inch, centimeter, yard, foot, mile, millimeter """ if to_type not in METER_CHART or from_type not in LENGTH_TYPE_CHART: raise ValueError( From 0968b11827d9d2cae010e0c56570e9fbe48a9a7d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 18 Oct 2021 23:18:03 +0200 Subject: [PATCH 06/13] Update length_conversions.py --- conversions/length_conversions.py | 110 +++++++++++++----------------- 1 file changed, 48 insertions(+), 62 deletions(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index 271de4ef9d4d..a13dd8d38179 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -6,9 +6,10 @@ -> Import this file into their respective project. -> Use the function length_conversion() for conversion of length 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 - -> value : the value which you want to convert + REFERENCES : -> Wikipedia reference: https://en.wikipedia.org/wiki/Meter @@ -22,95 +23,80 @@ -> Wikipedia reference: https://en.wikipedia.org/wiki/Millimeter """ -METER_CHART: dict[str, float] = { - "meter": 1, - "kilometer": 0.001, - "feet": 3.28084, - "inch": 39.3701, - "centimeter": 100, - "yard": 1.09361, - "foot": 3.28084, - "mile": 0.000621371, - "millimeter": 1000, -} +from collections import namedtuple + +from_to = namedtuple("from_to", "from_ to") -LENGTH_TYPE_CHART: dict[str, float] = { - "meter": 1, - "kilometer": 1000, - "feet": 0.3048, - "inch": 0.0254, - "centimeter": 0.01, - "yard": 0.9144, - "foot": 0.3048, - "mile": 1609.34, - "millimeter": 0.001, +METRIC_CONVERSION = { + "meter": from_to(1, 1), + "kilometer": from_to(1000, 0.001), + "feet": from_to(0.3048, 3.28084), + "inch": from_to(0.0254, 39.3701), + "centimeter": from_to(0.01, 100), + "yard": from_to(0.9144, 1.09361), + "foot": from_to(0.3048, 3.28084), + "mile": from_to(1609.34, 0.000621371), + "millimeter": from_to(0.001, 1000), } -def length_conversion(from_type: str, to_type: str, value: float) -> float: +def length_conversion(value: float, from_type: str, to_type: str) -> float: """ - Conversion of length unit with the help of LENGTH_CHART - "meter": 1, - "kilometer": 0.001, - "feet": 3.28084, - "inch": 39.3701, - "centimeter": 100, - "yard": 1.09361, - "foot": 3.28084, - "mile": 0.000621371, - "millimeter": 1000, - >>> length_conversion("meter","feet",4) + Conversion between length units. + + >>> length_conversion(4, "meter", "feet") 13.12336 - >>> length_conversion("meter","kilometer",1) + >>> length_conversion(1, "meter", "kilometer") 0.001 - >>> length_conversion("kilometer","inch",1) + >>> length_conversion(1, "kilometer", "inch") 39370.1 - >>> length_conversion("kilometer","mile",3) + >>> length_conversion(3, "kilometer", "mile") 1.8641130000000001 - >>> length_conversion("feet","meter",2) + >>> length_conversion(2, "feet", "meter") 0.6096 - >>> length_conversion("feet","yard",4) + >>> length_conversion(4, "feet", "yard") 1.333329312 - >>> length_conversion("inch","meter",1) + >>> length_conversion(1, "inch", "meter") 0.0254 - >>> length_conversion("inch","mile",2) + >>> length_conversion(2, "inch", "mile") 3.15656468e-05 - >>> length_conversion("centimeter","millimeter",2) + >>> length_conversion(2, "centimeter", "millimeter") 20.0 - >>> length_conversion("centimeter","yard",2) + >>> length_conversion(2, "centimeter", "yard") 0.0218722 - >>> length_conversion("yard","meter",4) + >>> length_conversion(4, "yard", "meter") 3.6576 - >>> length_conversion("yard","kilometer",4) + >>> length_conversion(4, "yard", "kilometer") 0.0036576 - >>> length_conversion("foot","meter",3) + >>> length_conversion(3, "foot", "meter") 0.9144000000000001 - >>> length_conversion("foot","inch",3) + >>> length_conversion(3, "foot", "inch") 36.00001944 - >>> length_conversion("mile","kilometer",4) + >>> length_conversion(4, "mile", "kilometer") 6.43736 - >>> length_conversion("mile","inch",2) + >>> length_conversion(2, "mile", "inch") 126719.753468 - >>> length_conversion("millimeter","centimeter",3) + >>> length_conversion(3, "millimeter", "centimeter") 0.3 - >>> length_conversion("millimeter","inch",3) + >>> length_conversion(3, "millimeter", "inch") 0.1181103 - >>> length_conversion("wrongUnit","inch",4 ) + >>> length_conversion(4, "wrongUnit", "inch") 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 - length_conversion("wrongUnit","inch",4 ) - File "", line 104, in length_conversion - ValueError: Invalid 'from_type' or 'to_type' value: 'wrongUnit', 'inch' + ... + ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: meter, kilometer, feet, inch, centimeter, yard, foot, mile, millimeter """ - if to_type not in METER_CHART or from_type not in LENGTH_TYPE_CHART: + if from_type not in METRIC_CONVERSION: + raise ValueError( + f"Invalid 'from_type' value: {from_type!r}\n" + f"Supported values are: {', '.join(METRIC_CONVERSION)}" + ) + if to_type not in METRIC_CONVERSION: raise ValueError( - f"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" - f"Supported values are: {', '.join(LENGTH_TYPE_CHART)}" + f"Invalid 'to_type' value: {to_type!r}\n" + f"Supported values are: {', '.join(METRIC_CONVERSION)}" ) - return value * METER_CHART[to_type] * LENGTH_TYPE_CHART[from_type] + return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to if __name__ == "__main__": From e17567d18d5d80f55584bfccea42f760268845f7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 18 Oct 2021 23:20:32 +0200 Subject: [PATCH 07/13] Update length_conversions.py --- conversions/length_conversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index a13dd8d38179..3a8e0cdb93f4 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -43,7 +43,7 @@ def length_conversion(value: float, from_type: str, to_type: str) -> float: """ Conversion between length units. - + >>> length_conversion(4, "meter", "feet") 13.12336 >>> length_conversion(1, "meter", "kilometer") From a0846353ee18ed14750429c27ae830d5fc9201ba Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 18 Oct 2021 23:23:50 +0200 Subject: [PATCH 08/13] Update length_conversions.py --- conversions/length_conversions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index 3a8e0cdb93f4..23e72379dc1e 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -9,7 +9,6 @@ -> 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/Meter From 79555891bc65d97a4a3dacb565bbcfbfc3cab36e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 18 Oct 2021 23:32:59 +0200 Subject: [PATCH 09/13] Update length_conversions.py --- conversions/length_conversions.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index 23e72379dc1e..c90944f4f083 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -82,18 +82,18 @@ def length_conversion(value: float, from_type: str, to_type: str) -> float: >>> length_conversion(4, "wrongUnit", "inch") Traceback (most recent call last): ... - ValueError: Invalid 'from_type' value: 'wrongUnit' - Supported values are: meter, kilometer, feet, inch, centimeter, yard, foot, mile, millimeter + ValueError: Invalid 'from_type' value: 'wrongUnit'. Supported values are: + meter, kilometer, feet, inch, centimeter, yard, foot, mile, millimeter """ if from_type not in METRIC_CONVERSION: raise ValueError( - f"Invalid 'from_type' value: {from_type!r}\n" - f"Supported values are: {', '.join(METRIC_CONVERSION)}" + 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}\n" - f"Supported values are: {', '.join(METRIC_CONVERSION)}" + 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 From abe2ae1943926511b05e55cdcc873f9087eb88f3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 18 Oct 2021 23:36:12 +0200 Subject: [PATCH 10/13] Update length_conversions.py --- conversions/length_conversions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index c90944f4f083..19ba76140d4b 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -82,7 +82,7 @@ def length_conversion(value: float, from_type: str, to_type: str) -> float: >>> length_conversion(4, "wrongUnit", "inch") Traceback (most recent call last): ... - ValueError: Invalid 'from_type' value: 'wrongUnit'. Supported values are: + ValueError: Invalid 'from_type' value: 'wrongUnit'. Supported values are: meter, kilometer, feet, inch, centimeter, yard, foot, mile, millimeter """ if from_type not in METRIC_CONVERSION: From ecb6c3314d178eded2cbd3b518031e55968a9a8c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 19 Oct 2021 06:43:24 +0200 Subject: [PATCH 11/13] Update length_conversions.py --- conversions/length_conversions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index 19ba76140d4b..b25446ab74d3 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -88,12 +88,12 @@ def length_conversion(value: float, from_type: str, to_type: str) -> float: if from_type not in METRIC_CONVERSION: raise ValueError( f"Invalid 'from_type' value: {from_type!r} Supported values are:\n" - ", ".join(METRIC_CONVERSION) + + ", ".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) + + ", ".join(METRIC_CONVERSION) ) return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to From 14df863abab53190fdeb0b25c4d5b0d64f8cec81 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 19 Oct 2021 06:56:58 +0200 Subject: [PATCH 12/13] Update length_conversions.py --- conversions/length_conversions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index b25446ab74d3..1a7a7eb72d46 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -83,7 +83,6 @@ def length_conversion(value: float, from_type: str, to_type: str) -> float: Traceback (most recent call last): ... ValueError: Invalid 'from_type' value: 'wrongUnit'. Supported values are: - meter, kilometer, feet, inch, centimeter, yard, foot, mile, millimeter """ if from_type not in METRIC_CONVERSION: raise ValueError( From d54012bdad6aa61be24c4c8a5a30406746d634fa Mon Sep 17 00:00:00 2001 From: Sabari Ganesh <64348740+SabariGanesh-K@users.noreply.github.com> Date: Tue, 19 Oct 2021 10:40:25 +0530 Subject: [PATCH 13/13] Fixed Minor errors in test One of the test was failing and it was fixed --- conversions/length_conversions.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/conversions/length_conversions.py b/conversions/length_conversions.py index 1a7a7eb72d46..811a9a916b70 100644 --- a/conversions/length_conversions.py +++ b/conversions/length_conversions.py @@ -81,8 +81,13 @@ def length_conversion(value: float, from_type: str, to_type: str) -> float: 0.1181103 >>> length_conversion(4, "wrongUnit", "inch") Traceback (most recent call last): - ... - ValueError: Invalid 'from_type' value: 'wrongUnit'. Supported values are: + File "/usr/lib/python3.8/doctest.py", line 1336, in __run + exec(compile(example.source, filename, "single", + File "", line 1, in + length_conversion(4, "wrongUnit", "inch") + File "", line 85, in length_conversion + ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: + meter, kilometer, feet, inch, centimeter, yard, foot, mile, millimeter """ if from_type not in METRIC_CONVERSION: raise ValueError(