Skip to content
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

Add get_application_registry (plus prettify) #738

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ def _build_unit(units):
return _APP_REGISTRY.Unit(units)


def get_application_registry():
"""
Get the application registry which is used for unpickling operations.
"""
return _APP_REGISTRY


def set_application_registry(registry):
"""Set the application registry which is used for unpickling operations.

Expand Down
20 changes: 17 additions & 3 deletions pint/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def _iter(self):
# # Allow exception to propagate in case of non-iterable magnitude
it_mag = iter(self.magnitude)
return iter((self.__class__(mag, self._units) for mag in it_mag))

@property
def debug_used(self):
return self.__used
Expand All @@ -162,9 +163,6 @@ def __deepcopy__(self, memo):
def __str__(self):
return format(self)

def __repr__(self):
return "<Quantity({}, '{}')>".format(self._magnitude, self._units)

def __hash__(self):
self_base = self.to_base_units()
if self_base.dimensionless:
Expand Down Expand Up @@ -323,9 +321,25 @@ def check(self, dimension):

@classmethod
def from_tuple(cls, tup):
from . import _APP_REGISTRY, _DEFAULT_REGISTRY
if _APP_REGISTRY == _DEFAULT_REGISTRY:
warnings.warn("It is advised to set the application registry "
"through `pint.set_application_registry` before "
"using unit serialization.\nSee "
"https://pint.readthedocs.io/en/latest/"
"tutorial.html#using-pint-in-your-projects for "
"more details.")
return cls(tup[0], UnitsContainer(tup[1]))

def to_tuple(self):
from . import _APP_REGISTRY, _DEFAULT_REGISTRY
if _APP_REGISTRY == _DEFAULT_REGISTRY:
warnings.warn("It is advised to set the application registry "
"through `pint.set_application_registry` before "
"using unit serialization.\nSee "
"https://pint.readthedocs.io/en/latest/"
"tutorial.html#using-pint-in-your-projects for "
"more details.")
return self.m, tuple(self._units.items())

def compatible_units(self, *contexts):
Expand Down
3 changes: 0 additions & 3 deletions pint/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ def __deepcopy__(self, memo):
def __str__(self):
return format(self)

def __repr__(self):
return "<Unit('{}')>".format(self._units)

def __format__(self, spec):
spec = spec or self.default_format
# special cases
Expand Down
24 changes: 24 additions & 0 deletions pint/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sys
import re
import operator
import warnings
from numbers import Number
from fractions import Fraction
from collections import Mapping
Expand Down Expand Up @@ -305,9 +306,25 @@ def __hash__(self):
return self._hash

def __getstate__(self):
from . import _APP_REGISTRY, _DEFAULT_REGISTRY
if _APP_REGISTRY == _DEFAULT_REGISTRY:
warnings.warn("It is advised to set the application registry "
"through `pint.set_application_registry` before "
"using unit serialization.\nSee "
"https://pint.readthedocs.io/en/latest/"
"tutorial.html#using-pint-in-your-projects for "
"more details.")
return {'_d': self._d, '_hash': self._hash}

def __setstate__(self, state):
from . import _APP_REGISTRY, _DEFAULT_REGISTRY
if _APP_REGISTRY == _DEFAULT_REGISTRY:
warnings.warn("It is advised to set the application registry "
"through `pint.set_application_registry` before "
"using unit serialization.\nSee "
"https://pint.readthedocs.io/en/latest/"
"tutorial.html#using-pint-in-your-projects for "
"more details.")
self._d = state['_d']
self._hash = state['_hash']

Expand Down Expand Up @@ -647,6 +664,13 @@ def _repr_pretty_(self, p, cycle):
else:
p.text("{:P}".format(self))

def __repr__(self):
if hasattr(self, "_magnitude"):
return "<{}({}, '{}')>".format(self.__class__.__name__,
self._magnitude, self._units)
else:
return "<{}('{}')>".format(self.__class__.__name__, self._units)


def to_units_container(unit_like, registry=None):
""" Convert a unit compatible type to a UnitsContainer.
Expand Down