|
| 1 | +""" |
| 2 | +Conversion of weight units. |
| 3 | +
|
| 4 | +__author__ = "Anubhav Solanki" |
| 5 | +__license__ = "MIT" |
| 6 | +__version__ = "1.0.0" |
| 7 | +__maintainer__ = "Anubhav Solanki" |
| 8 | + |
| 9 | +
|
| 10 | +USAGE : |
| 11 | +-> Import this file into their respective project. |
| 12 | +-> Use the function weight_conversion() for conversion of weight units. |
| 13 | +-> Parameters : |
| 14 | + -> from_type : From which type you want to convert |
| 15 | + -> to_type : To which type you want to convert |
| 16 | + -> value : the value which you want to convert |
| 17 | +
|
| 18 | +REFERENCES : |
| 19 | +
|
| 20 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Kilogram |
| 21 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Gram |
| 22 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Millimetre |
| 23 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Tonne |
| 24 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Long_ton |
| 25 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Short_ton |
| 26 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Pound |
| 27 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Ounce |
| 28 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Fineness#Karat |
| 29 | +-> Wikipedia reference: https://en.wikipedia.org/wiki/Dalton_(unit) |
| 30 | +""" |
| 31 | + |
| 32 | +KILOGRAM_CHART = { |
| 33 | + "kilogram": 1, |
| 34 | + "gram": pow(10, 3), |
| 35 | + "milligram": pow(10, 6), |
| 36 | + "metric-ton": pow(10, -3), |
| 37 | + "long-ton": 0.0009842073, |
| 38 | + "short-ton": 0.0011023122, |
| 39 | + "pound": 2.2046244202, |
| 40 | + "ounce": 35.273990723, |
| 41 | + "carrat": 5000, |
| 42 | + "atomic-mass-unit": 6.022136652e26, |
| 43 | +} |
| 44 | + |
| 45 | +WEIGHT_TYPE_CHART = { |
| 46 | + "kilogram": 1, |
| 47 | + "gram": pow(10, -3), |
| 48 | + "milligram": pow(10, -6), |
| 49 | + "metric-ton": pow(10, 3), |
| 50 | + "long-ton": 1016.04608, |
| 51 | + "short-ton": 907.184, |
| 52 | + "pound": 0.453592, |
| 53 | + "ounce": 0.0283495, |
| 54 | + "carrat": 0.0002, |
| 55 | + "atomic-mass-unit": 1.660540199e-27, |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +def weight_conversion(from_type: str, to_type: str, value: float) -> float: |
| 60 | + """ |
| 61 | + Conversion of weight unit with the help of KILOGRAM_CHART |
| 62 | +
|
| 63 | + "kilogram" : 1, |
| 64 | + "gram" : pow(10, 3), |
| 65 | + "milligram" : pow(10, 6), |
| 66 | + "metric-ton" : pow(10, -3), |
| 67 | + "long-ton" : 0.0009842073, |
| 68 | + "short-ton" : 0.0011023122, |
| 69 | + "pound" : 2.2046244202, |
| 70 | + "ounce" : 35.273990723, |
| 71 | + "carrat" : 5000, |
| 72 | + "atomic-mass-unit" : 6.022136652E+26 |
| 73 | +
|
| 74 | + >>> weight_conversion("kilogram","kilogram",4) |
| 75 | + 4 |
| 76 | + >>> weight_conversion("kilogram","gram",1) |
| 77 | + 1000 |
| 78 | + >>> weight_conversion("kilogram","milligram",4) |
| 79 | + 4000000 |
| 80 | + >>> weight_conversion("kilogram","metric-ton",4) |
| 81 | + 0.004 |
| 82 | + >>> weight_conversion("kilogram","long-ton",3) |
| 83 | + 0.0029526219 |
| 84 | + >>> weight_conversion("kilogram","short-ton",1) |
| 85 | + 0.0011023122 |
| 86 | + >>> weight_conversion("kilogram","pound",4) |
| 87 | + 8.8184976808 |
| 88 | + >>> weight_conversion("kilogram","ounce",4) |
| 89 | + 141.095962892 |
| 90 | + >>> weight_conversion("kilogram","carrat",3) |
| 91 | + 15000 |
| 92 | + >>> weight_conversion("kilogram","atomic-mass-unit",1) |
| 93 | + 6.022136652e+26 |
| 94 | + >>> weight_conversion("gram","kilogram",1) |
| 95 | + 0.001 |
| 96 | + >>> weight_conversion("gram","gram",3) |
| 97 | + 3.0 |
| 98 | + >>> weight_conversion("gram","milligram",2) |
| 99 | + 2000.0 |
| 100 | + >>> weight_conversion("gram","metric-ton",4) |
| 101 | + 4e-06 |
| 102 | + >>> weight_conversion("gram","long-ton",3) |
| 103 | + 2.9526219e-06 |
| 104 | + >>> weight_conversion("gram","short-ton",3) |
| 105 | + 3.3069366000000003e-06 |
| 106 | + >>> weight_conversion("gram","pound",3) |
| 107 | + 0.0066138732606 |
| 108 | + >>> weight_conversion("gram","ounce",1) |
| 109 | + 0.035273990723 |
| 110 | + >>> weight_conversion("gram","carrat",2) |
| 111 | + 10.0 |
| 112 | + >>> weight_conversion("gram","atomic-mass-unit",1) |
| 113 | + 6.022136652e+23 |
| 114 | + >>> weight_conversion("milligram","kilogram",1) |
| 115 | + 1e-06 |
| 116 | + >>> weight_conversion("milligram","gram",2) |
| 117 | + 0.002 |
| 118 | + >>> weight_conversion("milligram","milligram",3) |
| 119 | + 3.0 |
| 120 | + >>> weight_conversion("milligram","metric-ton",3) |
| 121 | + 3e-09 |
| 122 | + >>> weight_conversion("milligram","long-ton",3) |
| 123 | + 2.9526219e-09 |
| 124 | + >>> weight_conversion("milligram","short-ton",1) |
| 125 | + 1.1023122e-09 |
| 126 | + >>> weight_conversion("milligram","pound",3) |
| 127 | + 6.6138732605999995e-06 |
| 128 | + >>> weight_conversion("milligram","ounce",2) |
| 129 | + 7.054798144599999e-05 |
| 130 | + >>> weight_conversion("milligram","carrat",1) |
| 131 | + 0.005 |
| 132 | + >>> weight_conversion("milligram","atomic-mass-unit",1) |
| 133 | + 6.022136652e+20 |
| 134 | + >>> weight_conversion("metric-ton","kilogram",2) |
| 135 | + 2000 |
| 136 | + >>> weight_conversion("metric-ton","gram",2) |
| 137 | + 2000000 |
| 138 | + >>> weight_conversion("metric-ton","milligram",3) |
| 139 | + 3000000000 |
| 140 | + >>> weight_conversion("metric-ton","metric-ton",2) |
| 141 | + 2.0 |
| 142 | + >>> weight_conversion("metric-ton","long-ton",3) |
| 143 | + 2.9526219 |
| 144 | + >>> weight_conversion("metric-ton","short-ton",2) |
| 145 | + 2.2046244 |
| 146 | + >>> weight_conversion("metric-ton","pound",3) |
| 147 | + 6613.8732606 |
| 148 | + >>> weight_conversion("metric-ton","ounce",4) |
| 149 | + 141095.96289199998 |
| 150 | + >>> weight_conversion("metric-ton","carrat",4) |
| 151 | + 20000000 |
| 152 | + >>> weight_conversion("metric-ton","atomic-mass-unit",1) |
| 153 | + 6.022136652e+29 |
| 154 | + >>> weight_conversion("long-ton","kilogram",4) |
| 155 | + 4064.18432 |
| 156 | + >>> weight_conversion("long-ton","gram",4) |
| 157 | + 4064184.32 |
| 158 | + >>> weight_conversion("long-ton","milligram",3) |
| 159 | + 3048138240.0 |
| 160 | + >>> weight_conversion("long-ton","metric-ton",4) |
| 161 | + 4.06418432 |
| 162 | + >>> weight_conversion("long-ton","long-ton",3) |
| 163 | + 2.999999907217152 |
| 164 | + >>> weight_conversion("long-ton","short-ton",1) |
| 165 | + 1.119999989746176 |
| 166 | + >>> weight_conversion("long-ton","pound",3) |
| 167 | + 6720.000000049448 |
| 168 | + >>> weight_conversion("long-ton","ounce",1) |
| 169 | + 35840.000000060514 |
| 170 | + >>> weight_conversion("long-ton","carrat",4) |
| 171 | + 20320921.599999998 |
| 172 | + >>> weight_conversion("long-ton","atomic-mass-unit",4) |
| 173 | + 2.4475073353955697e+30 |
| 174 | + >>> weight_conversion("short-ton","kilogram",3) |
| 175 | + 2721.5519999999997 |
| 176 | + >>> weight_conversion("short-ton","gram",3) |
| 177 | + 2721552.0 |
| 178 | + >>> weight_conversion("short-ton","milligram",1) |
| 179 | + 907184000.0 |
| 180 | + >>> weight_conversion("short-ton","metric-ton",4) |
| 181 | + 3.628736 |
| 182 | + >>> weight_conversion("short-ton","long-ton",3) |
| 183 | + 2.6785713457296 |
| 184 | + >>> weight_conversion("short-ton","short-ton",3) |
| 185 | + 2.9999999725344 |
| 186 | + >>> weight_conversion("short-ton","pound",2) |
| 187 | + 4000.0000000294335 |
| 188 | + >>> weight_conversion("short-ton","ounce",4) |
| 189 | + 128000.00000021611 |
| 190 | + >>> weight_conversion("short-ton","carrat",4) |
| 191 | + 18143680.0 |
| 192 | + >>> weight_conversion("short-ton","atomic-mass-unit",1) |
| 193 | + 5.463186016507968e+29 |
| 194 | + >>> weight_conversion("pound","kilogram",4) |
| 195 | + 1.814368 |
| 196 | + >>> weight_conversion("pound","gram",2) |
| 197 | + 907.184 |
| 198 | + >>> weight_conversion("pound","milligram",3) |
| 199 | + 1360776.0 |
| 200 | + >>> weight_conversion("pound","metric-ton",3) |
| 201 | + 0.001360776 |
| 202 | + >>> weight_conversion("pound","long-ton",2) |
| 203 | + 0.0008928571152432 |
| 204 | + >>> weight_conversion("pound","short-ton",1) |
| 205 | + 0.0004999999954224 |
| 206 | + >>> weight_conversion("pound","pound",3) |
| 207 | + 3.0000000000220752 |
| 208 | + >>> weight_conversion("pound","ounce",1) |
| 209 | + 16.000000000027015 |
| 210 | + >>> weight_conversion("pound","carrat",1) |
| 211 | + 2267.96 |
| 212 | + >>> weight_conversion("pound","atomic-mass-unit",4) |
| 213 | + 1.0926372033015936e+27 |
| 214 | + >>> weight_conversion("ounce","kilogram",3) |
| 215 | + 0.0850485 |
| 216 | + >>> weight_conversion("ounce","gram",3) |
| 217 | + 85.0485 |
| 218 | + >>> weight_conversion("ounce","milligram",4) |
| 219 | + 113398.0 |
| 220 | + >>> weight_conversion("ounce","metric-ton",4) |
| 221 | + 0.000113398 |
| 222 | + >>> weight_conversion("ounce","long-ton",4) |
| 223 | + 0.0001116071394054 |
| 224 | + >>> weight_conversion("ounce","short-ton",4) |
| 225 | + 0.0001249999988556 |
| 226 | + >>> weight_conversion("ounce","pound",1) |
| 227 | + 0.0625000000004599 |
| 228 | + >>> weight_conversion("ounce","ounce",2) |
| 229 | + 2.000000000003377 |
| 230 | + >>> weight_conversion("ounce","carrat",1) |
| 231 | + 141.7475 |
| 232 | + >>> weight_conversion("ounce","atomic-mass-unit",1) |
| 233 | + 1.70724563015874e+25 |
| 234 | + >>> weight_conversion("carrat","kilogram",1) |
| 235 | + 0.0002 |
| 236 | + >>> weight_conversion("carrat","gram",4) |
| 237 | + 0.8 |
| 238 | + >>> weight_conversion("carrat","milligram",2) |
| 239 | + 400.0 |
| 240 | + >>> weight_conversion("carrat","metric-ton",2) |
| 241 | + 4.0000000000000003e-07 |
| 242 | + >>> weight_conversion("carrat","long-ton",3) |
| 243 | + 5.9052438e-07 |
| 244 | + >>> weight_conversion("carrat","short-ton",4) |
| 245 | + 8.818497600000002e-07 |
| 246 | + >>> weight_conversion("carrat","pound",1) |
| 247 | + 0.00044092488404000004 |
| 248 | + >>> weight_conversion("carrat","ounce",2) |
| 249 | + 0.0141095962892 |
| 250 | + >>> weight_conversion("carrat","carrat",4) |
| 251 | + 4.0 |
| 252 | + >>> weight_conversion("carrat","atomic-mass-unit",4) |
| 253 | + 4.8177093216e+23 |
| 254 | + >>> weight_conversion("atomic-mass-unit","kilogram",4) |
| 255 | + 6.642160796e-27 |
| 256 | + >>> weight_conversion("atomic-mass-unit","gram",2) |
| 257 | + 3.321080398e-24 |
| 258 | + >>> weight_conversion("atomic-mass-unit","milligram",2) |
| 259 | + 3.3210803980000002e-21 |
| 260 | + >>> weight_conversion("atomic-mass-unit","metric-ton",3) |
| 261 | + 4.9816205970000004e-30 |
| 262 | + >>> weight_conversion("atomic-mass-unit","long-ton",3) |
| 263 | + 4.9029473573977584e-30 |
| 264 | + >>> weight_conversion("atomic-mass-unit","short-ton",1) |
| 265 | + 1.830433719948128e-30 |
| 266 | + >>> weight_conversion("atomic-mass-unit","pound",3) |
| 267 | + 1.0982602420317504e-26 |
| 268 | + >>> weight_conversion("atomic-mass-unit","ounce",2) |
| 269 | + 1.1714775914938915e-25 |
| 270 | + >>> weight_conversion("atomic-mass-unit","carrat",2) |
| 271 | + 1.660540199e-23 |
| 272 | + >>> weight_conversion("atomic-mass-unit","atomic-mass-unit",2) |
| 273 | + 1.999999998903455 |
| 274 | + """ |
| 275 | + if to_type not in KILOGRAM_CHART or from_type not in WEIGHT_TYPE_CHART: |
| 276 | + raise ValueError( |
| 277 | + f"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" |
| 278 | + f"Supported values are: {', '.join(WEIGHT_TYPE_CHART)}" |
| 279 | + ) |
| 280 | + return value * KILOGRAM_CHART[to_type] * WEIGHT_TYPE_CHART[from_type] |
| 281 | + |
| 282 | + |
| 283 | +if __name__ == "__main__": |
| 284 | + |
| 285 | + import doctest |
| 286 | + |
| 287 | + doctest.testmod() |
0 commit comments