|
| 1 | +""" |
| 2 | +Conversion of length units. |
| 3 | +Available Units:- Metre,Kilometre,Feet,Inch,Centimeter,Yard,Foot,Mile,Millimeter |
| 4 | +
|
| 5 | +USAGE : |
| 6 | +-> Import this file into their respective project. |
| 7 | +-> Use the function length_conversion() for conversion of length 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 | +
|
| 13 | +REFERENCES : |
| 14 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Meter |
| 15 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Kilometer |
| 16 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Feet |
| 17 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Inch |
| 18 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Centimeter |
| 19 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Yard |
| 20 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Foot |
| 21 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Mile |
| 22 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Millimeter |
| 23 | +""" |
| 24 | + |
| 25 | +from collections import namedtuple |
| 26 | + |
| 27 | +from_to = namedtuple("from_to", "from_ to") |
| 28 | + |
| 29 | +METRIC_CONVERSION = { |
| 30 | + "meter": from_to(1, 1), |
| 31 | + "kilometer": from_to(1000, 0.001), |
| 32 | + "feet": from_to(0.3048, 3.28084), |
| 33 | + "inch": from_to(0.0254, 39.3701), |
| 34 | + "centimeter": from_to(0.01, 100), |
| 35 | + "yard": from_to(0.9144, 1.09361), |
| 36 | + "foot": from_to(0.3048, 3.28084), |
| 37 | + "mile": from_to(1609.34, 0.000621371), |
| 38 | + "millimeter": from_to(0.001, 1000), |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +def length_conversion(value: float, from_type: str, to_type: str) -> float: |
| 43 | + """ |
| 44 | + Conversion between length units. |
| 45 | +
|
| 46 | + >>> length_conversion(4, "meter", "feet") |
| 47 | + 13.12336 |
| 48 | + >>> length_conversion(1, "meter", "kilometer") |
| 49 | + 0.001 |
| 50 | + >>> length_conversion(1, "kilometer", "inch") |
| 51 | + 39370.1 |
| 52 | + >>> length_conversion(3, "kilometer", "mile") |
| 53 | + 1.8641130000000001 |
| 54 | + >>> length_conversion(2, "feet", "meter") |
| 55 | + 0.6096 |
| 56 | + >>> length_conversion(4, "feet", "yard") |
| 57 | + 1.333329312 |
| 58 | + >>> length_conversion(1, "inch", "meter") |
| 59 | + 0.0254 |
| 60 | + >>> length_conversion(2, "inch", "mile") |
| 61 | + 3.15656468e-05 |
| 62 | + >>> length_conversion(2, "centimeter", "millimeter") |
| 63 | + 20.0 |
| 64 | + >>> length_conversion(2, "centimeter", "yard") |
| 65 | + 0.0218722 |
| 66 | + >>> length_conversion(4, "yard", "meter") |
| 67 | + 3.6576 |
| 68 | + >>> length_conversion(4, "yard", "kilometer") |
| 69 | + 0.0036576 |
| 70 | + >>> length_conversion(3, "foot", "meter") |
| 71 | + 0.9144000000000001 |
| 72 | + >>> length_conversion(3, "foot", "inch") |
| 73 | + 36.00001944 |
| 74 | + >>> length_conversion(4, "mile", "kilometer") |
| 75 | + 6.43736 |
| 76 | + >>> length_conversion(2, "mile", "inch") |
| 77 | + 126719.753468 |
| 78 | + >>> length_conversion(3, "millimeter", "centimeter") |
| 79 | + 0.3 |
| 80 | + >>> length_conversion(3, "millimeter", "inch") |
| 81 | + 0.1181103 |
| 82 | + >>> length_conversion(4, "wrongUnit", "inch") |
| 83 | + Traceback (most recent call last): |
| 84 | + File "/usr/lib/python3.8/doctest.py", line 1336, in __run |
| 85 | + exec(compile(example.source, filename, "single", |
| 86 | + File "<doctest __main__.length_conversion[18]>", line 1, in <module> |
| 87 | + length_conversion(4, "wrongUnit", "inch") |
| 88 | + File "<string>", line 85, in length_conversion |
| 89 | + ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are: |
| 90 | + meter, kilometer, feet, inch, centimeter, yard, foot, mile, millimeter |
| 91 | + """ |
| 92 | + if from_type not in METRIC_CONVERSION: |
| 93 | + raise ValueError( |
| 94 | + f"Invalid 'from_type' value: {from_type!r} Supported values are:\n" |
| 95 | + + ", ".join(METRIC_CONVERSION) |
| 96 | + ) |
| 97 | + if to_type not in METRIC_CONVERSION: |
| 98 | + raise ValueError( |
| 99 | + f"Invalid 'to_type' value: {to_type!r}. Supported values are:\n" |
| 100 | + + ", ".join(METRIC_CONVERSION) |
| 101 | + ) |
| 102 | + return value * METRIC_CONVERSION[from_type].from_ * METRIC_CONVERSION[to_type].to |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + import doctest |
| 107 | + |
| 108 | + doctest.testmod() |
0 commit comments