- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathind_reactance.py
69 lines (52 loc) · 1.94 KB
/
ind_reactance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# https://en.wikipedia.org/wiki/Electrical_reactance#Inductive_reactance
from __future__ importannotations
frommathimportpi
defind_reactance(
inductance: float, frequency: float, reactance: float
) ->dict[str, float]:
"""
Calculate inductive reactance, frequency or inductance from two given electrical
properties then return name/value pair of the zero value in a Python dict.
Parameters
----------
inductance : float with units in Henries
frequency : float with units in Hertz
reactance : float with units in Ohms
>>> ind_reactance(-35e-6, 1e3, 0)
Traceback (most recent call last):
...
ValueError: Inductance cannot be negative
>>> ind_reactance(35e-6, -1e3, 0)
Traceback (most recent call last):
...
ValueError: Frequency cannot be negative
>>> ind_reactance(35e-6, 0, -1)
Traceback (most recent call last):
...
ValueError: Inductive reactance cannot be negative
>>> ind_reactance(0, 10e3, 50)
{'inductance': 0.0007957747154594767}
>>> ind_reactance(35e-3, 0, 50)
{'frequency': 227.36420441699332}
>>> ind_reactance(35e-6, 1e3, 0)
{'reactance': 0.2199114857512855}
"""
if (inductance, frequency, reactance).count(0) !=1:
raiseValueError("One and only one argument must be 0")
ifinductance<0:
raiseValueError("Inductance cannot be negative")
iffrequency<0:
raiseValueError("Frequency cannot be negative")
ifreactance<0:
raiseValueError("Inductive reactance cannot be negative")
ifinductance==0:
return {"inductance": reactance/ (2*pi*frequency)}
eliffrequency==0:
return {"frequency": reactance/ (2*pi*inductance)}
elifreactance==0:
return {"reactance": 2*pi*frequency*inductance}
else:
raiseValueError("Exactly one argument must be 0")
if__name__=="__main__":
importdoctest
doctest.testmod()