From 77c0cc993a76dc30b759619c4c5ce4de23e12f5a Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 3 Oct 2022 21:08:34 +0300 Subject: [PATCH 1/6] Adding inductive reactance calculation --- electronics/ind_reactance.py | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 electronics/ind_reactance.py diff --git a/electronics/ind_reactance.py b/electronics/ind_reactance.py new file mode 100644 index 000000000000..86c84952cdb6 --- /dev/null +++ b/electronics/ind_reactance.py @@ -0,0 +1,65 @@ +# https://en.wikipedia.org/wiki/Electrical_reactance#Inductive_reactance +from __future__ import annotations + +PI = 3.14159 + +def ind_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.0007957753876221914} + + >>> ind_reactance(35e-3, 0, 50) + {'frequency': 227.36439646348322} + + >>> ind_reactance(35e-6, 1e3, 0) + {'reactance': 0.21991129999999995} + + """ + + if (inductance, frequency, reactance).count(0) != 1: + raise ValueError("One and only one argument must be 0") + if inductance < 0: + raise ValueError("Inductance cannot be negative") + if frequency < 0: + raise ValueError("Frequency cannot be negative") + if reactance < 0: + raise ValueError("Inductive reactance cannot be negative") + if inductance == 0: + return {"inductance": reactance / (2*PI*frequency)} + elif frequency == 0: + return {"frequency": reactance / (2*PI*inductance)} + elif reactance == 0: + return {"reactance": 2*PI*frequency*inductance} + else: + raise ValueError("Exactly one argument must be 0") + + +if __name__ == "__main__": + import doctest + doctest.testmod() From 768c0c2a4f54ef993d990e76ec9ce2733b423927 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Oct 2022 18:15:41 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- electronics/ind_reactance.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/electronics/ind_reactance.py b/electronics/ind_reactance.py index 86c84952cdb6..fe7580e88e19 100644 --- a/electronics/ind_reactance.py +++ b/electronics/ind_reactance.py @@ -3,11 +3,14 @@ PI = 3.14159 -def ind_reactance(inductance: float, frequency: float, reactance: float) -> dict[str, float]: + +def ind_reactance( + inductance: float, frequency: float, reactance: float +) -> dict[str, float]: """ - Calculate inductive reactance, frequency or inductance from two given electrical + 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 @@ -41,7 +44,7 @@ def ind_reactance(inductance: float, frequency: float, reactance: float) -> dict {'reactance': 0.21991129999999995} """ - + if (inductance, frequency, reactance).count(0) != 1: raise ValueError("One and only one argument must be 0") if inductance < 0: @@ -51,15 +54,16 @@ def ind_reactance(inductance: float, frequency: float, reactance: float) -> dict if reactance < 0: raise ValueError("Inductive reactance cannot be negative") if inductance == 0: - return {"inductance": reactance / (2*PI*frequency)} + return {"inductance": reactance / (2 * PI * frequency)} elif frequency == 0: - return {"frequency": reactance / (2*PI*inductance)} + return {"frequency": reactance / (2 * PI * inductance)} elif reactance == 0: - return {"reactance": 2*PI*frequency*inductance} + return {"reactance": 2 * PI * frequency * inductance} else: raise ValueError("Exactly one argument must be 0") if __name__ == "__main__": import doctest + doctest.testmod() From 5f4c71ebf8123871efebfd51d4c0827744176d93 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Oct 2022 22:56:48 +0100 Subject: [PATCH 3/6] from math import pi --- electronics/ind_reactance.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/electronics/ind_reactance.py b/electronics/ind_reactance.py index fe7580e88e19..770ed8d55305 100644 --- a/electronics/ind_reactance.py +++ b/electronics/ind_reactance.py @@ -1,7 +1,7 @@ # https://en.wikipedia.org/wiki/Electrical_reactance#Inductive_reactance from __future__ import annotations -PI = 3.14159 +from math import pi def ind_reactance( @@ -54,11 +54,11 @@ def ind_reactance( if reactance < 0: raise ValueError("Inductive reactance cannot be negative") if inductance == 0: - return {"inductance": reactance / (2 * PI * frequency)} + return {"inductance": reactance / (2 * pi * frequency)} elif frequency == 0: - return {"frequency": reactance / (2 * PI * inductance)} + return {"frequency": reactance / (2 * pi * inductance)} elif reactance == 0: - return {"reactance": 2 * PI * frequency * inductance} + return {"reactance": 2 * pi * frequency * inductance} else: raise ValueError("Exactly one argument must be 0") From b43362b788ed6622c9b598b645b0b63b701445e7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 31 Oct 2022 12:04:02 +0100 Subject: [PATCH 4/6] 0007957747154594767 --- electronics/ind_reactance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/electronics/ind_reactance.py b/electronics/ind_reactance.py index 770ed8d55305..3ac94d62370c 100644 --- a/electronics/ind_reactance.py +++ b/electronics/ind_reactance.py @@ -35,7 +35,7 @@ def ind_reactance( ValueError: Inductive reactance cannot be negative >>> ind_reactance(0, 10e3, 50) - {'inductance': 0.0007957753876221914} + {'inductance': 0.0007957747154594767} >>> ind_reactance(35e-3, 0, 50) {'frequency': 227.36439646348322} From 8f798a78d29e1477eea0f728a4bfab577103bf0f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 31 Oct 2022 12:08:36 +0100 Subject: [PATCH 5/6] 36420441699332 --- electronics/ind_reactance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/electronics/ind_reactance.py b/electronics/ind_reactance.py index 3ac94d62370c..4a7386ea86a0 100644 --- a/electronics/ind_reactance.py +++ b/electronics/ind_reactance.py @@ -38,7 +38,7 @@ def ind_reactance( {'inductance': 0.0007957747154594767} >>> ind_reactance(35e-3, 0, 50) - {'frequency': 227.36439646348322} + {'frequency': 227.36420441699332} >>> ind_reactance(35e-6, 1e3, 0) {'reactance': 0.21991129999999995} From eefbe0d86dfdeacf59098f73a14c0e4fb0778290 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 31 Oct 2022 12:24:15 +0100 Subject: [PATCH 6/6] 2199114857512855 --- electronics/ind_reactance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/electronics/ind_reactance.py b/electronics/ind_reactance.py index 4a7386ea86a0..3f77ef628203 100644 --- a/electronics/ind_reactance.py +++ b/electronics/ind_reactance.py @@ -41,7 +41,7 @@ def ind_reactance( {'frequency': 227.36420441699332} >>> ind_reactance(35e-6, 1e3, 0) - {'reactance': 0.21991129999999995} + {'reactance': 0.2199114857512855} """