|
| 1 | +""" |
| 2 | +Conversion of length units. |
| 3 | +Available Units: |
| 4 | +Metre, Kilometre, Megametre, Gigametre, |
| 5 | +Terametre, Petametre, Exametre, Zettametre, Yottametre |
| 6 | +
|
| 7 | +USAGE : |
| 8 | +-> Import this file into their respective project. |
| 9 | +-> Use the function length_conversion() for conversion of length units. |
| 10 | +-> Parameters : |
| 11 | + -> value : The number of from units you want to convert |
| 12 | + -> from_type : From which type you want to convert |
| 13 | + -> to_type : To which type you want to convert |
| 14 | +
|
| 15 | +REFERENCES : |
| 16 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Meter |
| 17 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Kilometer |
| 18 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Orders_of_magnitude_(length) |
| 19 | +""" |
| 20 | + |
| 21 | +UNIT_SYMBOL = { |
| 22 | + "meter": "m", |
| 23 | + "kilometer": "km", |
| 24 | + "megametre": "Mm", |
| 25 | + "gigametre": "Gm", |
| 26 | + "terametre": "Tm", |
| 27 | + "petametre": "Pm", |
| 28 | + "exametre": "Em", |
| 29 | + "zettametre": "Zm", |
| 30 | + "yottametre": "Ym", |
| 31 | +} |
| 32 | +# Exponent of the factor(meter) |
| 33 | +METRIC_CONVERSION = { |
| 34 | + "m": 0, |
| 35 | + "km": 3, |
| 36 | + "Mm": 6, |
| 37 | + "Gm": 9, |
| 38 | + "Tm": 12, |
| 39 | + "Pm": 15, |
| 40 | + "Em": 18, |
| 41 | + "Zm": 21, |
| 42 | + "Ym": 24, |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +def length_conversion(value: float, from_type: str, to_type: str) -> float: |
| 47 | + """ |
| 48 | + Conversion between astronomical length units. |
| 49 | +
|
| 50 | + >>> length_conversion(1, "meter", "kilometer") |
| 51 | + 0.001 |
| 52 | + >>> length_conversion(1, "meter", "megametre") |
| 53 | + 1e-06 |
| 54 | + >>> length_conversion(1, "gigametre", "meter") |
| 55 | + 1000000000 |
| 56 | + >>> length_conversion(1, "gigametre", "terametre") |
| 57 | + 0.001 |
| 58 | + >>> length_conversion(1, "petametre", "terametre") |
| 59 | + 1000 |
| 60 | + >>> length_conversion(1, "petametre", "exametre") |
| 61 | + 0.001 |
| 62 | + >>> length_conversion(1, "terametre", "zettametre") |
| 63 | + 1e-09 |
| 64 | + >>> length_conversion(1, "yottametre", "zettametre") |
| 65 | + 1000 |
| 66 | + >>> length_conversion(4, "wrongUnit", "inch") |
| 67 | + Traceback (most recent call last): |
| 68 | + ... |
| 69 | + ValueError: Invalid 'from_type' value: 'wrongUnit'. |
| 70 | + Conversion abbreviations are: m, km, Mm, Gm, Tm, Pm, Em, Zm, Ym |
| 71 | + """ |
| 72 | + |
| 73 | + from_sanitized = from_type.lower().strip("s") |
| 74 | + to_sanitized = to_type.lower().strip("s") |
| 75 | + |
| 76 | + from_sanitized = UNIT_SYMBOL.get(from_sanitized, from_sanitized) |
| 77 | + to_sanitized = UNIT_SYMBOL.get(to_sanitized, to_sanitized) |
| 78 | + |
| 79 | + if from_sanitized not in METRIC_CONVERSION: |
| 80 | + raise ValueError( |
| 81 | + f"Invalid 'from_type' value: {from_type!r}.\n" |
| 82 | + f"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}" |
| 83 | + ) |
| 84 | + if to_sanitized not in METRIC_CONVERSION: |
| 85 | + raise ValueError( |
| 86 | + f"Invalid 'to_type' value: {to_type!r}.\n" |
| 87 | + f"Conversion abbreviations are: {', '.join(METRIC_CONVERSION)}" |
| 88 | + ) |
| 89 | + from_exponent = METRIC_CONVERSION[from_sanitized] |
| 90 | + to_exponent = METRIC_CONVERSION[to_sanitized] |
| 91 | + exponent = 1 |
| 92 | + |
| 93 | + if from_exponent > to_exponent: |
| 94 | + exponent = from_exponent - to_exponent |
| 95 | + else: |
| 96 | + exponent = -(to_exponent - from_exponent) |
| 97 | + |
| 98 | + return value * pow(10, exponent) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + from doctest import testmod |
| 103 | + |
| 104 | + testmod() |
0 commit comments