Skip to content

Commit f54a966

Browse files
jlhuhnShivaDahal99CaedenPH
authored
Energy conversions (#8801)
* Create TestShiva * Delete TestShiva * Create energy_conversions.py * Update conversions/energy_conversions.py Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com> --------- Co-authored-by: ShivaDahal99 <130563462+ShivaDahal99@users.noreply.github.com> Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
1 parent 5ffe601 commit f54a966

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

conversions/energy_conversions.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
Conversion of energy units.
3+
4+
Available units: joule, kilojoule, megajoule, gigajoule,\
5+
wattsecond, watthour, kilowatthour, newtonmeter, calorie_nutr,\
6+
kilocalorie_nutr, electronvolt, britishthermalunit_it, footpound
7+
8+
USAGE :
9+
-> Import this file into their respective project.
10+
-> Use the function energy_conversion() for conversion of energy units.
11+
-> Parameters :
12+
-> from_type : From which type you want to convert
13+
-> to_type : To which type you want to convert
14+
-> value : the value which you want to convert
15+
16+
REFERENCES :
17+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Units_of_energy
18+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Joule
19+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Kilowatt-hour
20+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Newton-metre
21+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Calorie
22+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Electronvolt
23+
-> Wikipedia reference: https://en.wikipedia.org/wiki/British_thermal_unit
24+
-> Wikipedia reference: https://en.wikipedia.org/wiki/Foot-pound_(energy)
25+
-> Unit converter reference: https://www.unitconverters.net/energy-converter.html
26+
"""
27+
28+
ENERGY_CONVERSION: dict[str, float] = {
29+
"joule": 1.0,
30+
"kilojoule": 1_000,
31+
"megajoule": 1_000_000,
32+
"gigajoule": 1_000_000_000,
33+
"wattsecond": 1.0,
34+
"watthour": 3_600,
35+
"kilowatthour": 3_600_000,
36+
"newtonmeter": 1.0,
37+
"calorie_nutr": 4_186.8,
38+
"kilocalorie_nutr": 4_186_800.00,
39+
"electronvolt": 1.602_176_634e-19,
40+
"britishthermalunit_it": 1_055.055_85,
41+
"footpound": 1.355_818,
42+
}
43+
44+
45+
def energy_conversion(from_type: str, to_type: str, value: float) -> float:
46+
"""
47+
Conversion of energy units.
48+
>>> energy_conversion("joule", "joule", 1)
49+
1.0
50+
>>> energy_conversion("joule", "kilojoule", 1)
51+
0.001
52+
>>> energy_conversion("joule", "megajoule", 1)
53+
1e-06
54+
>>> energy_conversion("joule", "gigajoule", 1)
55+
1e-09
56+
>>> energy_conversion("joule", "wattsecond", 1)
57+
1.0
58+
>>> energy_conversion("joule", "watthour", 1)
59+
0.0002777777777777778
60+
>>> energy_conversion("joule", "kilowatthour", 1)
61+
2.7777777777777776e-07
62+
>>> energy_conversion("joule", "newtonmeter", 1)
63+
1.0
64+
>>> energy_conversion("joule", "calorie_nutr", 1)
65+
0.00023884589662749592
66+
>>> energy_conversion("joule", "kilocalorie_nutr", 1)
67+
2.388458966274959e-07
68+
>>> energy_conversion("joule", "electronvolt", 1)
69+
6.241509074460763e+18
70+
>>> energy_conversion("joule", "britishthermalunit_it", 1)
71+
0.0009478171226670134
72+
>>> energy_conversion("joule", "footpound", 1)
73+
0.7375621211696556
74+
>>> energy_conversion("joule", "megajoule", 1000)
75+
0.001
76+
>>> energy_conversion("calorie_nutr", "kilocalorie_nutr", 1000)
77+
1.0
78+
>>> energy_conversion("kilowatthour", "joule", 10)
79+
36000000.0
80+
>>> energy_conversion("britishthermalunit_it", "footpound", 1)
81+
778.1692306784539
82+
>>> energy_conversion("watthour", "joule", "a") # doctest: +ELLIPSIS
83+
Traceback (most recent call last):
84+
...
85+
TypeError: unsupported operand type(s) for /: 'str' and 'float'
86+
>>> energy_conversion("wrongunit", "joule", 1) # doctest: +ELLIPSIS
87+
Traceback (most recent call last):
88+
...
89+
ValueError: Incorrect 'from_type' or 'to_type' value: 'wrongunit', 'joule'
90+
Valid values are: joule, ... footpound
91+
>>> energy_conversion("joule", "wrongunit", 1) # doctest: +ELLIPSIS
92+
Traceback (most recent call last):
93+
...
94+
ValueError: Incorrect 'from_type' or 'to_type' value: 'joule', 'wrongunit'
95+
Valid values are: joule, ... footpound
96+
>>> energy_conversion("123", "abc", 1) # doctest: +ELLIPSIS
97+
Traceback (most recent call last):
98+
...
99+
ValueError: Incorrect 'from_type' or 'to_type' value: '123', 'abc'
100+
Valid values are: joule, ... footpound
101+
"""
102+
if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION:
103+
msg = (
104+
f"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n"
105+
f"Valid values are: {', '.join(ENERGY_CONVERSION)}"
106+
)
107+
raise ValueError(msg)
108+
return value * ENERGY_CONVERSION[from_type] / ENERGY_CONVERSION[to_type]
109+
110+
111+
if __name__ == "__main__":
112+
import doctest
113+
114+
doctest.testmod()

0 commit comments

Comments
 (0)