-
Notifications
You must be signed in to change notification settings - Fork 58
Quantity (RealwithUnit) unit converter using pint module #734
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 all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
83b167a
Quantity (RealwithUnit) unit converter using pint module
hpohekar efa94cf
Fixed a typo
hpohekar 2105242
Changed pint version
hpohekar 63c0ba9
Restricted conversions is generated as a list using standard list of …
hpohekar 2ee93b9
Checked test cases for restricted unit conversions
hpohekar e5e6d87
Added standard prefix list and generated combination of Hz and hertz …
hpohekar 432f5d2
Index operator replaced with convert method and added more test cases…
hpohekar 6c277cd
Removed a comment
hpohekar e097763
Added test cases for remaining units
hpohekar 6847d6a
Fixed a typo
hpohekar 8e09456
Defined new units and convertTo replaced with to
hpohekar 538f36c
Replaced units with base_units
hpohekar 99f10c5
Removed non-std units, added check for dimensionality and added descr…
hpohekar 2c79c7e
Renamed base_units to ureg
hpohekar 47208e9
Quantity is derived from float
hpohekar 5465b74
Description added about pint functionalities
hpohekar ce077a0
Removed restricted code, related test cases and updated the documenta…
hpohekar 01d8331
Added equal check method and related test case
hpohekar ee80774
Added self.value,updated test cases and updated the __eq__ method
hpohekar c93cd70
Replaced self.__float__() with self.value in all methods
hpohekar 10affdc
Updated the documentation
hpohekar 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| "appdirs>=1.4.0", | ||
| "pandas>=1.1.5", | ||
| "h5py>=3.7.0", | ||
| "pint>=0.18", | ||
| ] | ||
|
|
||
|
|
||
|
|
||
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,137 @@ | ||
| import pint | ||
| from pint import Unit | ||
|
|
||
| ureg = pint.UnitRegistry(autoconvert_offset_to_baseunit=True) | ||
| ureg.default_system = "SI" | ||
|
|
||
| quantity = ureg.Quantity | ||
|
|
||
|
|
||
| class Quantity(float): | ||
| """This class instantiates physical quantities using their real values and | ||
| units. Attributes of every instance of this class are used to construct a | ||
| new quantity instance supported by unit registry of pint module. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| value: Real value | ||
| Value of quantity is stored as float. | ||
|
|
||
| unit: Unit string | ||
| Unit of quantity is stored as string. | ||
|
|
||
| Methods | ||
| ------- | ||
| to(to_unit) | ||
| Converts to given unit string. | ||
|
|
||
| Returns | ||
| ------- | ||
| Quantity instance. | ||
|
|
||
| The pint module supports methods for unit conversions, unit compatibility and | ||
| dimensionality check. | ||
|
|
||
| All the instances of this class are converted to base SI units system to have | ||
| consistency in all arithmetic operations. | ||
| """ | ||
|
|
||
| def __new__(self, real_value, units_string): | ||
| return float.__new__(self, real_value) | ||
|
|
||
| def __init__(self, real_value, units_string): | ||
| float.__init__(real_value) | ||
| self.value = self.__float__() | ||
| self.unit = units_string | ||
| self._quantity = quantity(self.value, self.unit) | ||
| self._base_si_quantity = self._quantity.to_base_units() | ||
|
|
||
| def __str__(self): | ||
| return f'({self.value}, "{self.unit}")' | ||
|
|
||
| def __repr__(self): | ||
| return f'(Quantity ({self.value}, "{self.unit}"))' | ||
|
|
||
| def to(self, to_unit): | ||
|
|
||
| """This method checks the compatibility between current instance unit | ||
| and user provided unit, if both of them are compatible, then only it | ||
| performs required conversion otherwise raises a Value Error.""" | ||
|
|
||
| user_unit = Unit(to_unit) | ||
|
|
||
| if not self._quantity.is_compatible_with(user_unit): | ||
| raise ValueError("Units are not compatible.") | ||
|
|
||
| converted = self._quantity.to(to_unit) | ||
|
|
||
| return Quantity(converted.magnitude, to_unit) | ||
|
|
||
| def __mul__(self, other): | ||
|
|
||
| if isinstance(other, Quantity): | ||
| temp = self._base_si_quantity * other._quantity | ||
| elif isinstance(other, int) or isinstance(other, float): | ||
| temp = quantity(self._base_si_quantity * other, self.unit) | ||
| return Quantity(temp.magnitude, temp.units) | ||
|
|
||
| def __rmul__(self, other): | ||
| return self.__mul__(other) | ||
|
|
||
| def __truediv__(self, other): | ||
|
|
||
| if isinstance(other, Quantity): | ||
| temp = self._base_si_quantity / other._quantity | ||
| elif isinstance(other, int) or isinstance(other, float): | ||
| temp = quantity(self._base_si_quantity / other, self.unit) | ||
| return Quantity(temp.magnitude, temp.units) | ||
|
|
||
| def __add__(self, other): | ||
|
|
||
| if isinstance(other, Quantity): | ||
| temp = self._base_si_quantity + other._quantity | ||
| elif self._base_si_quantity.dimensionless and ( | ||
| isinstance(other, int) or isinstance(other, float) | ||
| ): | ||
| temp = quantity( | ||
| self._base_si_quantity.magnitude + other, self._base_si_quantity.units | ||
| ) | ||
| else: | ||
| raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") | ||
| return Quantity(temp.magnitude, temp.units) | ||
|
|
||
| def __radd__(self, other): | ||
| return self.__add__(other) | ||
|
|
||
| def __sub__(self, other): | ||
|
|
||
| if isinstance(other, Quantity): | ||
| temp = self._base_si_quantity - other._quantity | ||
| elif self._base_si_quantity.dimensionless and ( | ||
| isinstance(other, int) or isinstance(other, float) | ||
| ): | ||
| temp = quantity( | ||
| self._base_si_quantity.magnitude - other, self._base_si_quantity.units | ||
| ) | ||
| else: | ||
| raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") | ||
| return Quantity(temp.magnitude, temp.units) | ||
|
|
||
| def __rsub__(self, other): | ||
|
|
||
| if isinstance(other, Quantity): | ||
hpohekar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| temp = other._quantity - self._base_si_quantity | ||
| elif self._base_si_quantity.dimensionless and ( | ||
| isinstance(other, int) or isinstance(other, float) | ||
| ): | ||
| temp = quantity( | ||
| other - self._base_si_quantity.magnitude, self._base_si_quantity.units | ||
| ) | ||
| else: | ||
| raise ValueError(f"Quantity{(self.value, self.unit)} is not dimensionless.") | ||
| return Quantity(temp.magnitude, temp.units) | ||
|
|
||
| def __eq__(self, other): | ||
| return ( | ||
| other._quantity == self._quantity if isinstance(other, Quantity) else False | ||
| ) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.