-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from CarliJoy/staging
Define unit register central and allow unit_choice propagation
- Loading branch information
Showing
15 changed files
with
185 additions
and
67 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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,14 +1,12 @@ | ||
__version__ = "0.4" | ||
|
||
from django.utils.deconstruct import deconstructible | ||
|
||
from pint import UnitRegistry | ||
|
||
|
||
@deconstructible | ||
class DeconstructibleUnitRegistry(UnitRegistry): | ||
"""Make UnitRegistry compatible with Django migrations by implementing the | ||
deconstruct() method.""" | ||
|
||
|
||
ureg = DeconstructibleUnitRegistry() | ||
from pkg_resources import DistributionNotFound, get_distribution | ||
|
||
try: | ||
# Change here if project is renamed and does not equal the package name | ||
dist_name = "django-pint" | ||
__version__ = get_distribution(dist_name).version | ||
except DistributionNotFound: # pragma: no cover | ||
# We don't expect this to be executed, as this would mean the configuration | ||
# for the python module is wrong | ||
__version__ = "unknown" | ||
finally: | ||
del get_distribution, DistributionNotFound |
This file contains 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 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,19 @@ | ||
from pint import DimensionalityError, UnitRegistry | ||
from typing import List | ||
|
||
|
||
def check_matching_unit_dimension( | ||
ureg: UnitRegistry, base_units: str, units_to_check: List[str] | ||
) -> None: | ||
""" | ||
Check if all units_to_check have the same Dimension like the base_units | ||
If not | ||
:raise DimensionalityError | ||
""" | ||
|
||
base_unit = getattr(ureg, base_units) | ||
|
||
for unit_string in units_to_check: | ||
unit = getattr(ureg, unit_string) | ||
if unit.dimensionality != base_unit.dimensionality: | ||
raise DimensionalityError(base_unit, unit) |
This file contains 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,11 @@ | ||
__version__ = "0.4" | ||
from django.conf import settings | ||
|
||
from pint import UnitRegistry, set_application_registry | ||
|
||
# Define default unit register | ||
DJANGO_PINT_UNIT_REGISTER = getattr( | ||
settings, "DJANGO_PINT_UNIT_REGISTER", UnitRegistry() | ||
) | ||
# Set as default application registry for i.e. for pickle | ||
set_application_registry(DJANGO_PINT_UNIT_REGISTER) |
This file contains 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,4 @@ | ||
from .settings import DJANGO_PINT_UNIT_REGISTER | ||
|
||
# The unit register that was defined in the settings (shortcurt) | ||
ureg = DJANGO_PINT_UNIT_REGISTER |
This file contains 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 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 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 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
Oops, something went wrong.