Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Commit

Permalink
Remove _isinstance and optimize check_type for ndarrays
Browse files Browse the repository at this point in the history
  • Loading branch information
paulromano committed Jun 5, 2016
1 parent 908e568 commit 395cfb0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 35 deletions.
41 changes: 11 additions & 30 deletions openmc/checkvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,6 @@

import numpy as np

def _isinstance(value, expected_type):
"""A Numpy-aware replacement for isinstance
This function will be obsolete when Numpy v. >= 1.9 is established.
"""

# Declare numpy numeric types.
np_ints = (np.int_, np.intc, np.intp, np.int8, np.int16, np.int32, np.int64,
np.uint8, np.uint16, np.uint32, np.uint64)
np_floats = (np.float_, np.float16, np.float32, np.float64)

# Include numpy integers, if necessary.
if type(expected_type) is tuple:
if Integral in expected_type:
expected_type = expected_type + np_ints
elif expected_type is Integral:
expected_type = (Integral, ) + np_ints

# Include numpy floats, if necessary.
if type(expected_type) is tuple:
if Real in expected_type:
expected_type = expected_type + np_floats
elif expected_type is Real:
expected_type = (Real, ) + np_floats

# Now, make the instance check.
return isinstance(value, expected_type)

def check_type(name, value, expected_type, expected_iter_type=None):
"""Ensure that an object is of an expected type. Optionally, if the object is
Expand All @@ -50,7 +23,7 @@ def check_type(name, value, expected_type, expected_iter_type=None):
"""

if not _isinstance(value, expected_type):
if not isinstance(value, expected_type):
if isinstance(expected_type, Iterable):
msg = 'Unable to set "{0}" to "{1}" which is not one of the ' \
'following types: "{2}"'.format(name, value, ', '.join(
Expand All @@ -61,8 +34,16 @@ def check_type(name, value, expected_type, expected_iter_type=None):
raise TypeError(msg)

if expected_iter_type:
if isinstance(value, np.ndarray):
if not issubclass(value.dtype.type, expected_iter_type):
msg = 'Unable to set "{0}" to "{1}" since each item must be ' \
'of type "{2}"'.format(name, value,
expected_iter_type.__name__)
else:
return

for item in value:
if not _isinstance(item, expected_iter_type):
if not isinstance(item, expected_iter_type):
if isinstance(expected_iter_type, Iterable):
msg = 'Unable to set "{0}" to "{1}" since each item must be ' \
'one of the following types: "{2}"'.format(
Expand Down Expand Up @@ -118,7 +99,7 @@ def check_iterable_type(name, value, expected_type, min_depth=1, max_depth=1):

# If this item is of the expected type, then we've reached the bottom
# level of this branch.
if _isinstance(current_item, expected_type):
if isinstance(current_item, expected_type):
# Is this deep enough?
if len(tree) < min_depth:
msg = 'Error setting "{0}": The item at {1} does not meet the '\
Expand Down
4 changes: 2 additions & 2 deletions openmc/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def bins(self, bins):

elif self.type in ['energy', 'energyout']:
for edge in bins:
if not cv._isinstance(edge, Real):
if not isinstance(edge, Real):
msg = 'Unable to add bin edge "{0}" to a "{1}" Filter ' \
'since it is a non-integer or floating point ' \
'value'.format(edge, self.type)
Expand All @@ -220,7 +220,7 @@ def bins(self, bins):
msg = 'Unable to add bins "{0}" to a mesh Filter since ' \
'only a single mesh can be used per tally'.format(bins)
raise ValueError(msg)
elif not cv._isinstance(bins[0], Integral):
elif not isinstance(bins[0], Integral):
msg = 'Unable to add bin "{0}" to mesh Filter since it ' \
'is a non-integer'.format(bins[0])
raise ValueError(msg)
Expand Down
2 changes: 1 addition & 1 deletion openmc/mgxs/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def get_mgxs(self, domain, mgxs_type):
cv.check_type('domain', domain, (openmc.Universe, Integral))

# Check that requested domain is included in library
if cv._isinstance(domain, Integral):
if isinstance(domain, Integral):
domain_id = domain
for domain in self.domains:
if domain_id == domain.id:
Expand Down
4 changes: 2 additions & 2 deletions openmc/stats/univariate.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ def p(self):

@x.setter
def x(self, x):
if cv._isinstance(x, Real):
if isinstance(x, Real):
x = [x]
cv.check_type('discrete values', x, Iterable, Real)
self._x = x

@p.setter
def p(self, p):
if cv._isinstance(p, Real):
if isinstance(p, Real):
p = [p]
cv.check_type('discrete probabilities', p, Iterable, Real)
for pk in p:
Expand Down

0 comments on commit 395cfb0

Please sign in to comment.