-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Electric power #3976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Electric power #3976
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||||||||
# https://en.m.wikipedia.org/wiki/Electric_power | ||||||||||||
|
||||||||||||
|
||||||||||||
def electric_power(voltage: float, current: float, power: float) -> float: | ||||||||||||
""" | ||||||||||||
This function can calculate any one of the three (voltage, current, power), | ||||||||||||
fundamental value of electrical system. | ||||||||||||
examples are below: | ||||||||||||
>>> electric_power(voltage=0, current=2, power=5) | ||||||||||||
{'voltage': 2.5} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
>>> electric_power(voltage=2, current=2, power=0) | ||||||||||||
{'power': 4.0} | ||||||||||||
>>> electric_power(voltage=-2, current=3, power=0) | ||||||||||||
{'power': 6.0} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to test:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, should i also do this on ohms_law as well?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's get this PR landed before you modify Ohm's law... Coincidentally, @rhettinger (who wrote the namedtuple) just tweeted about Ohm's law... |
||||||||||||
""" | ||||||||||||
if (voltage, current, power).count(0) != 1: | ||||||||||||
raise ValueError("Only one argument must be 0") | ||||||||||||
elif power < 0: | ||||||||||||
raise ValueError( | ||||||||||||
"Power cannot be negative in any electrical/electronics system" | ||||||||||||
) | ||||||||||||
elif voltage == 0: | ||||||||||||
return {"voltage": power / current} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
elif current == 0: | ||||||||||||
return {"current": power / voltage} | ||||||||||||
elif power == 0: | ||||||||||||
return {"power": float(abs(voltage * current))} | ||||||||||||
|
||||||||||||
|
||||||||||||
if __name__ == "__main__": | ||||||||||||
import doctest | ||||||||||||
|
||||||||||||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.