-
Couldn't load subscription status.
- Fork 4
Add initial code imported from the SDK #7
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
37f13e0
Import implementation from the SDK v1.0.0rc601
llucax 367b6b0
Add an introduction and example to the package documentation
llucax 29841af
Include the package documentation in the website home
llucax 097e7b6
Add a link to the documentation in the README
llucax 7e87ee8
mkdocs: Update edit URL to v1.x.x
llucax 33b38dc
Update release notes
llucax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| --8<-- "README.md" | ||
| # Frequenz Quantities Library | ||
|
|
||
| ::: frequenz.quantities | ||
| options: | ||
| members: [] | ||
| show_bases: false | ||
| show_root_heading: false | ||
| show_root_toc_entry: false | ||
| show_source: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| # License: MIT | ||
| # Copyright © 2022 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Types for holding quantities with units.""" | ||
|
|
||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Self, overload | ||
|
|
||
| from ._quantity import NoDefaultConstructible, Quantity | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ._percentage import Percentage | ||
| from ._power import Power | ||
| from ._voltage import Voltage | ||
|
|
||
|
|
||
| class Current( | ||
| Quantity, | ||
| metaclass=NoDefaultConstructible, | ||
| exponent_unit_map={ | ||
| -3: "mA", | ||
| 0: "A", | ||
| }, | ||
| ): | ||
| """A current quantity. | ||
|
|
||
| Objects of this type are wrappers around `float` values and are immutable. | ||
|
|
||
| The constructors accept a single `float` value, the `as_*()` methods return a | ||
| `float` value, and each of the arithmetic operators supported by this type are | ||
| actually implemented using floating-point arithmetic. | ||
|
|
||
| So all considerations about floating-point arithmetic apply to this type as well. | ||
| """ | ||
|
|
||
| @classmethod | ||
| def from_amperes(cls, amperes: float) -> Self: | ||
| """Initialize a new current quantity. | ||
|
|
||
| Args: | ||
| amperes: The current in amperes. | ||
|
|
||
| Returns: | ||
| A new current quantity. | ||
| """ | ||
| return cls._new(amperes) | ||
|
|
||
| @classmethod | ||
| def from_milliamperes(cls, milliamperes: float) -> Self: | ||
| """Initialize a new current quantity. | ||
|
|
||
| Args: | ||
| milliamperes: The current in milliamperes. | ||
|
|
||
| Returns: | ||
| A new current quantity. | ||
| """ | ||
| return cls._new(milliamperes, exponent=-3) | ||
|
|
||
| def as_amperes(self) -> float: | ||
| """Return the current in amperes. | ||
|
|
||
| Returns: | ||
| The current in amperes. | ||
| """ | ||
| return self._base_value | ||
|
|
||
| def as_milliamperes(self) -> float: | ||
| """Return the current in milliamperes. | ||
|
|
||
| Returns: | ||
| The current in milliamperes. | ||
| """ | ||
| return self._base_value * 1e3 | ||
|
|
||
| # See comment for Power.__mul__ for why we need the ignore here. | ||
| @overload # type: ignore[override] | ||
| def __mul__(self, scalar: float, /) -> Self: | ||
| """Scale this current by a scalar. | ||
|
|
||
| Args: | ||
| scalar: The scalar by which to scale this current. | ||
|
|
||
| Returns: | ||
| The scaled current. | ||
| """ | ||
|
|
||
| @overload | ||
| def __mul__(self, percent: Percentage, /) -> Self: | ||
| """Scale this current by a percentage. | ||
|
|
||
| Args: | ||
| percent: The percentage by which to scale this current. | ||
|
|
||
| Returns: | ||
| The scaled current. | ||
| """ | ||
|
|
||
| @overload | ||
| def __mul__(self, other: Voltage, /) -> Power: | ||
| """Multiply the current by a voltage to get a power. | ||
|
|
||
| Args: | ||
| other: The voltage. | ||
|
|
||
| Returns: | ||
| The calculated power. | ||
| """ | ||
|
|
||
| def __mul__(self, other: float | Percentage | Voltage, /) -> Self | Power: | ||
| """Return a current or power from multiplying this current by the given value. | ||
|
|
||
| Args: | ||
| other: The scalar, percentage or voltage to multiply by. | ||
|
|
||
| Returns: | ||
| A current or power. | ||
| """ | ||
| from ._percentage import Percentage # pylint: disable=import-outside-toplevel | ||
| from ._power import Power # pylint: disable=import-outside-toplevel | ||
| from ._voltage import Voltage # pylint: disable=import-outside-toplevel | ||
|
|
||
| match other: | ||
| case float() | Percentage(): | ||
| return super().__mul__(other) | ||
| case Voltage(): | ||
| return Power._new(self._base_value * other._base_value) | ||
| case _: | ||
| return NotImplemented |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe remove the first sentence as it is equal to the headline
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.
This is a title/summary, like with git commits. There are some options or places where only the summary line is shown.