From 61a36b897836eaad45d242ad8df8069ee7a88134 Mon Sep 17 00:00:00 2001 From: R3hankhan123 <1ms20ec080@msrit.edu> Date: Sun, 2 Oct 2022 12:01:23 +0530 Subject: [PATCH 1/2] Added Hopkinson's law --- electronics/hopkinson's_law.py | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 electronics/hopkinson's_law.py diff --git a/electronics/hopkinson's_law.py b/electronics/hopkinson's_law.py new file mode 100644 index 000000000000..e5a93bd93124 --- /dev/null +++ b/electronics/hopkinson's_law.py @@ -0,0 +1,39 @@ +#https://en.wikipedia.org/wiki/Magnetic_circuit#Hopkinson's_law +from __future__ import annotations +def hopkinsons_law(magnetomotive_force:float, current:float, reluctance:float) -> dict[str, float]: + """ + Apply Hopkinson's Law, on any two given electrical values, which can be magnetomotive force, current, + and reluctance, and then in a Python dict return name/value pair of the zero value. + + >>> hopkinsons_law(magnetomotive_force=10, reluctance=5, current=0) + {'current': 2.0} + >>> hopkinsons_law(magnetomotive_force=0, current=0, reluctance=10) + Traceback (most recent call last): + ... + ValueError: One and only one argument must be 0 + >>> hopkinsons_law(magnetomotive_force=0, current=1, reluctance=-2) + Traceback (most recent call last): + ... + ValueError: Reluctance cannot be negative + >>> hopkinsons_law(reluctance=0, magnetomotive_force=-10, current=1) + {'reluctance': -10.0} + >>> hopkinsons_law(magnetomotive_force=0, current=-1.5, reluctance=2) + {'magnetomotive_force': -3.0} + """ + if (magnetomotive_force, current, reluctance).count(0) != 1: + raise ValueError("One and only one argument must be 0") + if reluctance < 0: + raise ValueError("Reluctance cannot be negative") + if magnetomotive_force == 0: + return {"magnetomotive_force": float(current * reluctance)} + elif current == 0: + return {"current": magnetomotive_force / reluctance} + elif reluctance == 0: + return {"reluctance": magnetomotive_force / current} + else: + raise ValueError("Exactly one argument must be 0") + + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From f2ac276be54f2ce12fb8daefe073c9f217563cb7 Mon Sep 17 00:00:00 2001 From: R3hankhan123 <1ms20ec080@msrit.edu> Date: Sun, 2 Oct 2022 19:55:17 +0530 Subject: [PATCH 2/2] Made the recommended changes --- electronics/hopkinson's_law.py | 39 ------------------------------ electronics/hopkinsons_law.py | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 39 deletions(-) delete mode 100644 electronics/hopkinson's_law.py create mode 100644 electronics/hopkinsons_law.py diff --git a/electronics/hopkinson's_law.py b/electronics/hopkinson's_law.py deleted file mode 100644 index e5a93bd93124..000000000000 --- a/electronics/hopkinson's_law.py +++ /dev/null @@ -1,39 +0,0 @@ -#https://en.wikipedia.org/wiki/Magnetic_circuit#Hopkinson's_law -from __future__ import annotations -def hopkinsons_law(magnetomotive_force:float, current:float, reluctance:float) -> dict[str, float]: - """ - Apply Hopkinson's Law, on any two given electrical values, which can be magnetomotive force, current, - and reluctance, and then in a Python dict return name/value pair of the zero value. - - >>> hopkinsons_law(magnetomotive_force=10, reluctance=5, current=0) - {'current': 2.0} - >>> hopkinsons_law(magnetomotive_force=0, current=0, reluctance=10) - Traceback (most recent call last): - ... - ValueError: One and only one argument must be 0 - >>> hopkinsons_law(magnetomotive_force=0, current=1, reluctance=-2) - Traceback (most recent call last): - ... - ValueError: Reluctance cannot be negative - >>> hopkinsons_law(reluctance=0, magnetomotive_force=-10, current=1) - {'reluctance': -10.0} - >>> hopkinsons_law(magnetomotive_force=0, current=-1.5, reluctance=2) - {'magnetomotive_force': -3.0} - """ - if (magnetomotive_force, current, reluctance).count(0) != 1: - raise ValueError("One and only one argument must be 0") - if reluctance < 0: - raise ValueError("Reluctance cannot be negative") - if magnetomotive_force == 0: - return {"magnetomotive_force": float(current * reluctance)} - elif current == 0: - return {"current": magnetomotive_force / reluctance} - elif reluctance == 0: - return {"reluctance": magnetomotive_force / current} - else: - raise ValueError("Exactly one argument must be 0") - - -if __name__ == "__main__": - import doctest - doctest.testmod() \ No newline at end of file diff --git a/electronics/hopkinsons_law.py b/electronics/hopkinsons_law.py new file mode 100644 index 000000000000..c74c0c5a4603 --- /dev/null +++ b/electronics/hopkinsons_law.py @@ -0,0 +1,44 @@ +# https://en.wikipedia.org/wiki/Magnetic_circuit#Hopkinson's_law +from __future__ import annotations + + +def hopkinsons_law( + magnetomotive_force: float, flux: float, reluctance: float +) -> dict[str, float]: + """ + Apply Hopkinson's Law, on any two given electrical values, which can be magnetomotive force, flux, + and reluctance, and then in a Python dict return name/value pair of the zero value. + + >>> hopkinsons_law(magnetomotive_force=10, reluctance=5, flux=0) + {'flux': 2.0} + >>> hopkinsons_law(magnetomotive_force=0, flux=0, reluctance=10) + Traceback (most recent call last): + ... + ValueError: One and only one argument must be 0 + >>> hopkinsons_law(magnetomotive_force=0, flux=1, reluctance=-2) + Traceback (most recent call last): + ... + ValueError: Reluctance cannot be negative + >>> hopkinsons_law(reluctance=0, magnetomotive_force=-10, flux=1) + {'reluctance': -10.0} + >>> hopkinsons_law(magnetomotive_force=0, flux=-1.5, reluctance=2) + {'magnetomotive_force': -3.0} + """ + if (magnetomotive_force, flux, reluctance).count(0) != 1: + raise ValueError("One and only one argument must be 0") + if reluctance < 0: + raise ValueError("Reluctance cannot be negative") + if magnetomotive_force == 0: + return {"magnetomotive_force": float(flux * reluctance)} + elif flux == 0: + return {"flux": magnetomotive_force / reluctance} + elif reluctance == 0: + return {"reluctance": magnetomotive_force / flux} + else: + raise ValueError("Exactly one argument must be 0") + + +if __name__ == "__main__": + import doctest + + doctest.testmod()