Skip to content

Commit

Permalink
First attempt at non-NumPy compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jthielen committed Dec 4, 2019
1 parent e302d39 commit d3c3d2a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 3 additions & 0 deletions pint/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def __array_function__(self, *args, **kwargs):
if HAS_NUMPY_ARRAY_FUNCTION:
warnings.warn(_msg, BehaviorChangeWarning)

NP_NO_VALUE = np._NoValue

except ImportError:

np = None
Expand All @@ -133,6 +135,7 @@ class ndarray(object):
NUMPY_VER = '0'
NUMERIC_TYPES = (Number, Decimal)
HAS_NUMPY_ARRAY_FUNCTION = False
NP_NO_VALUE = None

def _to_magnitude(value, force_ndarray=False):
if isinstance(value, (dict, bool)) or value is None:
Expand Down
18 changes: 11 additions & 7 deletions pint/numpy_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
:license: BSD, see LICENSE for more details.
"""

from .compat import is_upcast_type, np, string_types, eq
from .compat import NP_NO_VALUE, is_upcast_type, np, string_types, eq
from .errors import DimensionalityError
from .util import iterable, sized

Expand Down Expand Up @@ -140,6 +140,10 @@ def decorator(func):

def implement_func(func_type, func_str, input_units=None, output_unit=None):
"""TODO"""
# If NumPy is not available, do not attempt implement that which does not exist
if np is None:
return

func = getattr(np, func_str)

@implements(func_str, func_type)
Expand Down Expand Up @@ -418,9 +422,9 @@ def _unwrap(p, discont=None, axis=-1):


@implements('amin', 'function')
def _amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
where=np._NoValue):
if initial == np._NoValue:
def _amin(a, axis=None, out=None, keepdims=NP_NO_VALUE, initial=NP_NO_VALUE,
where=NP_NO_VALUE):
if initial == NP_NO_VALUE:
(a,), output_wrap = unwrap_and_wrap_consistent_units(a)
else:
(a, initial), output_wrap = unwrap_and_wrap_consistent_units(a, initial)
Expand All @@ -429,9 +433,9 @@ def _amin(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,


@implements('amax', 'function')
def _amax(a, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,
where=np._NoValue):
if initial == np._NoValue:
def _amax(a, axis=None, out=None, keepdims=NP_NO_VALUE, initial=NP_NO_VALUE,
where=NP_NO_VALUE):
if initial == NP_NO_VALUE:
(a,), output_wrap = unwrap_and_wrap_consistent_units(a)
else:
(a, initial), output_wrap = unwrap_and_wrap_consistent_units(a, initial)
Expand Down

0 comments on commit d3c3d2a

Please sign in to comment.