From 5aaf9974e92ad8d74c66a60bb5456ae42e338ed3 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Sun, 28 Jun 2015 08:46:50 +0700 Subject: [PATCH] Use collections.Sequence, numbers.Real, and numbers.Integral for checking for appropriate types. This eliminates the checkvalue module. --- openmc/checkvalue.py | 13 ---- openmc/cmfd.py | 113 +++++++++++++++++----------------- openmc/element.py | 8 ++- openmc/executor.py | 16 ++--- openmc/filter.py | 21 ++++--- openmc/material.py | 23 ++++--- openmc/mesh.py | 49 ++++++++------- openmc/nuclide.py | 11 ++-- openmc/plots.py | 74 +++++++++++----------- openmc/settings.py | 142 ++++++++++++++++++++++--------------------- openmc/surface.py | 55 +++++++++-------- openmc/tallies.py | 29 +++++---- openmc/trigger.py | 9 ++- openmc/universe.py | 118 ++++++++++++++++------------------- 14 files changed, 340 insertions(+), 341 deletions(-) delete mode 100644 openmc/checkvalue.py diff --git a/openmc/checkvalue.py b/openmc/checkvalue.py deleted file mode 100644 index 503e7e24d..000000000 --- a/openmc/checkvalue.py +++ /dev/null @@ -1,13 +0,0 @@ -import numpy as np - - -def is_integer(val): - return isinstance(val, (int, np.int32, np.int64)) - - -def is_float(val): - return isinstance(val, (float, np.float32, np.float64)) - - -def is_string(val): - return isinstance(val, (str, np.str)) diff --git a/openmc/cmfd.py b/openmc/cmfd.py index 7fdde85be..573ade588 100644 --- a/openmc/cmfd.py +++ b/openmc/cmfd.py @@ -10,13 +10,18 @@ References """ +from collections import Sequence +from numbers import Real, Integral from xml.etree import ElementTree as ET +import sys import numpy as np -from openmc.checkvalue import * from openmc.clean_xml import * +if sys.version_info[0] >= 3: + basestring = str + class CMFDMesh(object): """A structured Cartesian mesh used for Coarse Mesh Finite Difference (CMFD) @@ -24,26 +29,26 @@ class CMFDMesh(object): Attributes ---------- - lower_left : tuple or list or ndarray + lower_left : Sequence of float The lower-left corner of the structured mesh. If only two coordinates are given, it is assumed that the mesh is an x-y mesh. - upper_right : tuple or list or ndarray + upper_right : Sequence of float The upper-right corner of the structrued mesh. If only two coordinates are given, it is assumed that the mesh is an x-y mesh. - dimension : tuple or list or ndarray + dimension : Sequence of int The number of mesh cells in each direction. - width : tuple or list or ndarray + width : Sequence of float The width of mesh cells in each direction. - energy : tuple or list or ndarray + energy : Sequence of float Energy bins in MeV, listed in ascending order (e.g. [0.0, 0.625e-7, 20.0]) for CMFD tallies and acceleration. If no energy bins are listed, OpenMC automatically assumes a one energy group calculation over the entire energy range. - albedo : tuple or list or ndarray + albedo : Sequence of float Surface ratio of incoming to outgoing partial currents on global boundary conditions. They are listed in the following order: -x +x -y +y -z +z. - map : tuple or list or ndarray + map : Sequence of int An optional acceleration map can be specified to overlay on the coarse mesh spatial grid. If this option is used, a ``1`` is used for a non-accelerated region and a ``2`` is used for an accelerated region. @@ -103,9 +108,9 @@ class CMFDMesh(object): @lower_left.setter def lower_left(self, lower_left): - if not isinstance(lower_left, (tuple, list, np.ndarray)): + if not isinstance(lower_left, Sequence): msg = 'Unable to set CMFD Mesh with lower_left {0} which is ' \ - 'not a Python list, tuple or NumPy array'.format(lower_left) + 'not a sequence'.format(lower_left) raise ValueError(msg) elif len(lower_left) != 2 and len(lower_left) != 3: @@ -114,9 +119,9 @@ class CMFDMesh(object): raise ValueError(msg) for coord in lower_left: - if not is_integer(coord) and not is_float(coord): + if not isinstance(coord, Real): msg = 'Unable to set CMFD Mesh with lower_left {0} which is ' \ - 'not an integer or a floating point value'.format(coord) + 'not a real number'.format(coord) raise ValueError(msg) self._lower_left = lower_left @@ -124,9 +129,9 @@ class CMFDMesh(object): @upper_right.setter def upper_right(self, upper_right): - if not isinstance(upper_right, (tuple, list, np.ndarray)): + if not isinstance(upper_right, Sequence): msg = 'Unable to set CMFD Mesh with upper_right {0} which is ' \ - 'not a Python list, tuple or NumPy array'.format(upper_right) + 'not a sequence'.format(upper_right) raise ValueError(msg) if len(upper_right) != 2 and len(upper_right) != 3: @@ -135,18 +140,18 @@ class CMFDMesh(object): raise ValueError(msg) for coord in upper_right: - if not is_integer(coord) and not is_float(coord): + if not isinstance(coord, Real): msg = 'Unable to set CMFD Mesh with upper_right {0} which ' \ - 'is not an integer or floating point value'.format(coord) + 'is not a real number'.format(coord) raise ValueError(msg) self._upper_right = upper_right @dimension.setter def dimension(self, dimension): - if not isinstance(dimension, (tuple, list, np.ndarray)): + if not isinstance(dimension, Sequence): msg = 'Unable to set CMFD Mesh with dimension {0} which is ' \ - 'not a Python list, tuple or NumPy array'.format(dimension) + 'not a sequence'.format(dimension) raise ValueError(msg) elif len(dimension) != 2 and len(dimension) != 3: @@ -155,7 +160,7 @@ class CMFDMesh(object): raise ValueError(msg) for dim in dimension: - if not is_integer(dim): + if not isinstance(dim, Integral): msg = 'Unable to set CMFD Mesh with dimension {0} which ' \ 'is a non-integer'.format(dim) raise ValueError(msg) @@ -165,9 +170,9 @@ class CMFDMesh(object): @width.setter def width(self, width): if width is not None: - if not isinstance(width, (tuple, list, np.ndarray)): + if not isinstance(width, Sequence): msg = 'Unable to set CMFD Mesh with width {0} which ' \ - 'is not a Python list, tuple or NumPy array'.format(width) + 'is not a sequence'.format(width) raise ValueError(msg) if len(width) != 2 and len(width) != 3: @@ -176,24 +181,24 @@ class CMFDMesh(object): raise ValueError(msg) for dim in width: - if not is_integer(dim) and not is_float(dim): + if not isinstance(dim, Real): msg = 'Unable to set CMFD Mesh with width {0} which is ' \ - 'not an integer or floating point value'.format(width) + 'not a real number'.format(width) raise ValueError(msg) self._width = width @energy.setter def energy(self, energy): - if not isinstance(energy, (tuple, list, np.ndarray)): + if not isinstance(energy, Sequence): msg = 'Unable to set CMFD Mesh energy to {0} which is not ' \ - 'a Python tuple/list or NumPy array'.format(energy) + 'a sequence'.format(energy) raise ValueError(msg) for e in energy: - if not is_integer(e) and not is_float(e): + if not isinstance(e, Real): msg = 'Unable to set CMFD Mesh energy to {0} which is not ' \ - 'an integer or floating point value'.format(e) + 'a real number'.format(e) raise ValueError(msg) elif e < 0: msg = 'Unable to set CMFD Mesh energy to {0} which is ' \ @@ -204,9 +209,9 @@ class CMFDMesh(object): @albedo.setter def albedo(self, albedo): - if not isinstance(albedo, (tuple, list, np.ndarray)): + if not isinstance(albedo, Sequence): msg = 'Unable to set CMFD Mesh albedo to {0} which is not ' \ - 'a Python tuple/list or NumPy array'.format(albedo) + 'a sequence'.format(albedo) raise ValueError(msg) if not len(albedo) == 6: @@ -215,9 +220,9 @@ class CMFDMesh(object): raise ValueError(msg) for a in albedo: - if not is_integer(a) and not is_float(a): + if not isinstance(a, Real): msg = 'Unable to set CMFD Mesh albedo to {0} which is not ' \ - 'an integer or floating point value'.format(a) + 'a real number'.format(a) raise ValueError(msg) elif a < 0 or a > 1: msg = 'Unable to set CMFD Mesh albedo to {0} which is ' \ @@ -229,9 +234,9 @@ class CMFDMesh(object): @map.setter def map(self, map): - if not isinstance(map, (tuple, list, np.ndarray)): + if not isinstance(map, Sequence): msg = 'Unable to set CMFD Mesh map to {0} which is not ' \ - 'a Python tuple/list or NumPy array'.format(map) + 'a sequence'.format(map) raise ValueError(msg) for m in map: @@ -301,7 +306,7 @@ class CMFDFile(object): feedback : bool Indicate or not the CMFD diffusion result is used to adjust the weight of fission source neutrons on the next OpenMC batch. Defaults to False. - gauss_seidel_tolerance : tuple or list or ndarray of float + gauss_seidel_tolerance : Sequence of float Two parameters specifying the absolute inner tolerance and the relative inner tolerance for Gauss-Seidel iterations when performing CMFD. ktol : float @@ -417,7 +422,7 @@ class CMFDFile(object): @begin.setter def begin(self, begin): - if not is_integer(begin): + if not isinstance(begin, Integral): msg = 'Unable to set CMFD begin batch to a non-integer ' \ 'value {0}'.format(begin) raise ValueError(msg) @@ -440,7 +445,7 @@ class CMFDFile(object): @display.setter def display(self, display): - if not is_string(display): + if not isinstance(basestring): msg = 'Unable to set CMFD display to a non-string ' \ 'value'.format(display) raise ValueError(msg) @@ -472,10 +477,9 @@ class CMFDFile(object): @gauss_seidel_tolerance.setter def gauss_seidel_tolerance(self, gauss_seidel_tolerance): - if not isinstance(gauss_seidel_tolerance, (float, list, np.ndarray)): + if not isinstance(gauss_seidel_tolerance, Sequence): msg = 'Unable to set Gauss-Seidel tolerance to {0} which is ' \ - 'not a Python tuple/list or NumPy array'.format( - gauss_seidel_tolerance) + 'not a sequence'.format(gauss_seidel_tolerance) raise ValueError(msg) if len(gauss_seidel_tolerance) != 2: @@ -484,18 +488,18 @@ class CMFDFile(object): raise ValueError(msg) for t in gauss_seidel_tolerance: - if not is_integer(t) and not is_float(t): + if not isinstance(t, Real): msg = 'Unable to set Gauss-Seidel tolerance with {0} which ' \ - 'is not an integer or floating point value'.format(t) + 'is not a real number'.format(t) raise ValueError(msg) self._gauss_seidel_tolerance = gauss_seidel_tolerance @ktol.setter def ktol(self, ktol): - if not is_integer(ktol) and not is_float(ktol): + if not isinstance(ktol, Real): msg = 'Unable to set the eigenvalue tolerance to {0} which is ' \ - 'not an integer or floating point value'.format(ktol) + 'not a real number'.format(ktol) raise ValueError(msg) self._ktol = ktol @@ -511,9 +515,9 @@ class CMFDFile(object): @norm.setter def norm(self, norm): - if not is_integer(norm) and not is_float(norm): + if not isinstance(norm, Real): msg = 'Unable to set the CMFD norm to {0} which is not ' \ - 'an integer or floating point value'.format(norm) + 'a real number'.format(norm) raise ValueError(msg) self._norm = norm @@ -538,41 +542,40 @@ class CMFDFile(object): @shift.setter def shift(self, shift): - if not is_integer(shift) and not is_float(shift): + if not isinstance(shift, Real): msg = 'Unable to set the Wielandt shift to {0} which is ' \ - 'not an integer or floating point value'.format(shift) + 'not a real number'.format(shift) raise ValueError(msg) self._shift = shift @spectral.setter def spectral(self, spectral): - if not is_integer(spectral) and not is_float(spectral): + if not isinstance(spectral, Real): msg = 'Unable to set the spectral radius to {0} which is ' \ - 'not an integer or floating point value'.format(spectral) + 'not a real number'.format(spectral) raise ValueError(msg) self._spectral = spectral @stol.setter def stol(self, stol): - if not is_integer(stol) and not is_float(stol): + if not isinstance(stol, Real): msg = 'Unable to set the fission source tolerance to {0} which ' \ - 'is not an integer or floating point value'.format(stol) + 'is not a real number'.format(stol) raise ValueError(msg) self._stol = stol @tally_reset.setter def tally_reset(self, tally_reset): - if not isinstance(tally_reset, (tuple, list, np.ndarray)): + if not isinstance(tally_reset, Sequence): msg = 'Unable to set tally reset batches to {0} which is ' \ - 'not a Python tuple/list or NumPy array'.format( - tally_reset) + 'not a sequence'.format(tally_reset) raise ValueError(msg) for t in tally_reset: - if not is_integer(t): + if not isinstance(t, Integral): msg = 'Unable to set tally reset batch to {0} which ' \ 'is not an integer'.format(t) raise ValueError(msg) diff --git a/openmc/element.py b/openmc/element.py index 9539fd1c5..b7c3c8896 100644 --- a/openmc/element.py +++ b/openmc/element.py @@ -1,4 +1,6 @@ -from openmc.checkvalue import * +import sys +if sys.version_info[0] >= 3: + basestring = str class Element(object): @@ -59,7 +61,7 @@ class Element(object): @xs.setter def xs(self, xs): - if not is_string(xs): + if not isinstance(xs, basestring): msg = 'Unable to set cross-section identifier xs for Element ' \ 'with a non-string value {0}'.format(xs) raise ValueError(msg) @@ -68,7 +70,7 @@ class Element(object): @name.setter def name(self, name): - if not is_string(name): + if not isinstance(name, basestring): msg = 'Unable to set name for Element with a non-string ' \ 'value {0}'.format(name) raise ValueError(msg) diff --git a/openmc/executor.py b/openmc/executor.py index ed3ec14ae..b67d7873e 100644 --- a/openmc/executor.py +++ b/openmc/executor.py @@ -1,8 +1,10 @@ from __future__ import print_function import subprocess +from numbers import Integral import os - -from openmc.checkvalue import * +import sys +if sys.version_info[0] >= 3: + basestring = str class Executor(object): @@ -39,7 +41,7 @@ class Executor(object): @working_directory.setter def working_directory(self, working_directory): - if not is_string(working_directory): + if not isinstance(working_directory, basestring): msg = 'Unable to set Executor\'s working directory to {0} ' \ 'since it is not a string'.format(working_directory) raise ValueError(msg) @@ -83,22 +85,22 @@ class Executor(object): post_args = ' ' pre_args = '' - if is_integer(particles) and particles > 0: + if isinstance(particles, Integral) and particles > 0: post_args += '-n {0} '.format(particles) - if is_integer(threads) and threads > 0: + if isinstance(threads, Integral) and threads > 0: post_args += '-s {0} '.format(threads) if geometry_debug: post_args += '-g ' - if is_string(restart_file): + if isinstance(restart_file, basestring): post_args += '-r {0} '.format(restart_file) if tracks: post_args += '-t' - if is_integer(mpi_procs) and mpi_procs > 1: + if isinstance(mpi_procs, Integral) and mpi_procs > 1: pre_args += 'mpirun -n {0} '.format(mpi_procs) command = pre_args + 'openmc ' + post_args diff --git a/openmc/filter.py b/openmc/filter.py index 0cef6493e..2a92dfbd6 100644 --- a/openmc/filter.py +++ b/openmc/filter.py @@ -1,7 +1,8 @@ +from collections import Sequence import copy +from numbers import Real, Integral from openmc import Mesh -from openmc.checkvalue import * from openmc.constants import * @@ -15,7 +16,7 @@ class Filter(object): The type of the tally filter. Acceptable values are "universe", "material", "cell", "cellborn", "surface", "mesh", "energy", "energyout", and "distribcell". - bins : int or list of int or list of float or ndarray + bins : int or Sequence of int or Sequence of float The bins for the filter. This takes on different meaning for different filters. @@ -23,7 +24,7 @@ class Filter(object): ---------- type : str The type of the tally filter. - bins : int or list of int or list of float or ndarray + bins : int or Sequence of int or Sequence of float The bins for the filter """ @@ -122,7 +123,7 @@ class Filter(object): raise ValueError(msg) # If the bin edge is a single value, it is a Cell, Material, etc. ID - if not isinstance(bins, (tuple, list, np.ndarray)): + if not isinstance(bins, Sequence): bins = [bins] # If the bins are in a collection, convert it to a list @@ -132,7 +133,7 @@ class Filter(object): if self._type in ['cell', 'cellborn', 'surface', 'material', 'universe', 'distribcell']: for edge in bins: - if not is_integer(edge): + if not isinstance(edge, Integral): msg = 'Unable to add bin "{0}" to a {1} Filter since ' \ 'it is a non-integer'.format(edge, self._type) raise ValueError(msg) @@ -143,7 +144,7 @@ class Filter(object): elif self._type in ['energy', 'energyout']: for edge in bins: - if not is_integer(edge) and not is_float(edge): + 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) @@ -167,7 +168,7 @@ class Filter(object): 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 is_integer(bins[0]): + 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) @@ -182,7 +183,7 @@ class Filter(object): # FIXME @num_bins.setter def num_bins(self, num_bins): - if not is_integer(num_bins) or num_bins < 0: + if not isinstance(num_bins, Integral) or num_bins < 0: msg = 'Unable to set the number of bins "{0}" for a {1} Filter ' \ 'since it is not a positive ' \ 'integer'.format(num_bins, self._type) @@ -203,7 +204,7 @@ class Filter(object): @offset.setter def offset(self, offset): - if not is_integer(offset): + if not isinstance(offset, Integral): msg = 'Unable to set offset "{0}" for a {1} Filter since it is a ' \ 'non-integer value'.format(offset, self._type) raise ValueError(msg) @@ -212,7 +213,7 @@ class Filter(object): @stride.setter def stride(self, stride): - if not is_integer(stride): + if not isinstance(stride, Integral): msg = 'Unable to set stride "{0}" for a {1} Filter since it is a ' \ 'non-integer value'.format(stride, self._type) raise ValueError(msg) diff --git a/openmc/material.py b/openmc/material.py index 24ffc2922..56bd2414c 100644 --- a/openmc/material.py +++ b/openmc/material.py @@ -1,10 +1,13 @@ -from collections import MappingView +from collections import Sequence from copy import deepcopy +from numbers import Real, Integral import warnings from xml.etree import ElementTree as ET +import sys +if sys.version_info[0] >= 3: + basestring = str import openmc -from openmc.checkvalue import * from openmc.clean_xml import * @@ -117,7 +120,7 @@ class Material(object): AUTO_MATERIAL_ID += 1 # Check that the ID is an integer and wasn't already used - elif not is_integer(material_id): + elif not isinstance(material_id, Integral): msg = 'Unable to set a non-integer Material ' \ 'ID {0}'.format(material_id) raise ValueError(msg) @@ -138,7 +141,7 @@ class Material(object): @name.setter def name(self, name): - if not is_string(name): + if not isinstance(name, basestring): msg = 'Unable to set name for Material ID={0} with a non-string ' \ 'value {1}'.format(self._id, name) raise ValueError(msg) @@ -183,7 +186,7 @@ class Material(object): warnings.warn('This feature is not yet implemented in a release ' 'version of openmc') - if not is_string(filename) and filename is not None: + if not isinstance(filename, basestring) and filename is not None: msg = 'Unable to add OTF material file to Material ID={0} with a ' \ 'non-string name {1}'.format(self._id, filename) raise ValueError(msg) @@ -315,12 +318,12 @@ class Material(object): """ - if not is_string(name): + if not isinstance(name, basestring): msg = 'Unable to add an S(a,b) table to Material ID={0} with a ' \ 'non-string table name {1}'.format(self._id, name) raise ValueError(msg) - if not is_string(xs): + if not isinstance(xs, basestring): msg = 'Unable to add an S(a,b) table to Material ID={0} with a ' \ 'non-string cross-section identifier {1}'.format(self._id, xs) raise ValueError(msg) @@ -523,7 +526,7 @@ class MaterialsFile(object): @default_xs.setter def default_xs(self, xs): - if not is_string(xs): + if not isinstance(xs, basestring): msg = 'Unable to set default xs to a non-string value'.format(xs) raise ValueError(msg) @@ -556,9 +559,9 @@ class MaterialsFile(object): """ - if not isinstance(materials, (tuple, list, MappingView)): + if not isinstance(materials, Sequence): msg = 'Unable to create OpenMC materials.xml file from {0} which ' \ - 'is not a Python tuple/list'.format(materials) + 'is not a sequence'.format(materials) raise ValueError(msg) for material in materials: diff --git a/openmc/mesh.py b/openmc/mesh.py index 6253835ad..13a9c46db 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -1,8 +1,11 @@ +from collections import Sequence import copy +from numbers import Real, Integral from xml.etree import ElementTree as ET +import sys -from openmc.checkvalue import * - +if sys.version_info[0] >= 3: + basestring = str # "Static" variable for auto-generated and Mesh IDs AUTO_MESH_ID = 10000 @@ -31,15 +34,15 @@ class Mesh(object): Name of the mesh type : str Type of the mesh - dimension : tuple or list or ndarray + dimension : Sequnce of int The number of mesh cells in each direction. - lower_left : tuple or list or ndarray + lower_left : Seuqnce of float The lower-left corner of the structured mesh. If only two coordinate are given, it is assumed that the mesh is an x-y mesh. - upper_right : tuple or list or ndarray + upper_right : Sequence of float The upper-right corner of the structrued mesh. If only two coordinate are given, it is assumed that the mesh is an x-y mesh. - width : tuple or list or ndarray + width : Sequence of float The width of mesh cells in each direction. """ @@ -137,7 +140,7 @@ class Mesh(object): AUTO_MESH_ID += 1 # Check that the ID is an integer and wasn't already used - elif not is_integer(mesh_id): + elif not isinstance(mesh_id, Integral): msg = 'Unable to set a non-integer Mesh ID {0}'.format(mesh_id) raise ValueError(msg) @@ -151,7 +154,7 @@ class Mesh(object): @name.setter def name(self, name): - if not is_string(name): + if not isinstance(name, basestring): msg = 'Unable to set name for Mesh ID={0} with a non-string ' \ 'value {1}'.format(self._id, name) raise ValueError(msg) @@ -160,7 +163,7 @@ class Mesh(object): @type.setter def type(self, type): - if not is_string(type): + if not isinstance(type, basestring): msg = 'Unable to set Mesh ID={0} for type {1} which is not ' \ 'a string'.format(self._id, type) raise ValueError(msg) @@ -174,10 +177,9 @@ class Mesh(object): @dimension.setter def dimension(self, dimension): - if not isinstance(dimension, (tuple, list, np.ndarray)): + if not isinstance(dimension, Sequence): msg = 'Unable to set Mesh ID={0} with dimension {1} which is ' \ - 'not a Python list, tuple or NumPy ' \ - 'array'.format(self._id, dimension) + 'not a sequence'.format(self._id, dimension) raise ValueError(msg) elif len(dimension) != 2 and len(dimension) != 3: msg = 'Unable to set Mesh ID={0} with dimension {1} since it ' \ @@ -185,7 +187,7 @@ class Mesh(object): raise ValueError(msg) for dim in dimension: - if not is_integer(dim): + if not isinstance(dim, Integral): msg = 'Unable to set Mesh ID={0} with dimension {1} which ' \ 'is a non-integer'.format(self._id, dim) raise ValueError(msg) @@ -194,10 +196,9 @@ class Mesh(object): @lower_left.setter def lower_left(self, lower_left): - if not isinstance(lower_left, (tuple, list, np.ndarray)): + if not isinstance(lower_left, Sequence): msg = 'Unable to set Mesh ID={0} with lower_left {1} which is ' \ - 'not a Python list, tuple or NumPy ' \ - 'array'.format(self._id, lower_left) + 'not a sequence'.format(self._id, lower_left) raise ValueError(msg) elif len(lower_left) != 2 and len(lower_left) != 3: @@ -206,7 +207,7 @@ class Mesh(object): raise ValueError(msg) for coord in lower_left: - if not is_integer(coord) and not is_float(coord): + if not isinstance(coord, Real): msg = 'Unable to set Mesh ID={0} with lower_left {1} which ' \ 'is neither neither an integer nor a floating point ' \ 'value'.format(self._id, coord) @@ -216,10 +217,9 @@ class Mesh(object): @upper_right.setter def upper_right(self, upper_right): - if not isinstance(upper_right, (tuple, list, np.ndarray)): + if not isinstance(upper_right, Sequence): msg = 'Unable to set Mesh ID={0} with upper_right {1} which ' \ - 'is not a Python list, tuple or NumPy ' \ - 'array'.format(self._id, upper_right) + 'is not a sequence'.format(self._id, upper_right) raise ValueError(msg) if len(upper_right) != 2 and len(upper_right) != 3: @@ -228,7 +228,7 @@ class Mesh(object): raise ValueError(msg) for coord in upper_right: - if not is_integer(coord) and not is_float(coord): + if not isinstance(coord, Real): msg = 'Unable to set Mesh ID={0} with upper_right {1} which ' \ 'is neither an integer nor a floating point ' \ 'value'.format(self._id, coord) @@ -239,10 +239,9 @@ class Mesh(object): @width.setter def width(self, width): if width is not None: - if not isinstance(width, (tuple, list, np.ndarray)): + if not isinstance(width, Sequence): msg = 'Unable to set Mesh ID={0} with width {1} which ' \ - 'is not a Python list, tuple or NumPy ' \ - 'array'.format(self._id, width) + 'is not a sequence'.format(self._id, width) raise ValueError(msg) if len(width) != 2 and len(width) != 3: @@ -251,7 +250,7 @@ class Mesh(object): raise ValueError(msg) for dim in width: - if not is_integer(dim) and not is_float(dim): + if not isinstance(dim, Real): msg = 'Unable to set Mesh ID={0} with width {1} which is ' \ 'neither an integer nor a floating point ' \ 'value'.format(self._id, width) diff --git a/openmc/nuclide.py b/openmc/nuclide.py index 65a2abfaf..594a3d511 100644 --- a/openmc/nuclide.py +++ b/openmc/nuclide.py @@ -1,4 +1,7 @@ -from openmc.checkvalue import * +from numbers import Integral +import sys +if sys.version_info[0] >= 3: + basestring = str class Nuclide(object): @@ -68,7 +71,7 @@ class Nuclide(object): @name.setter def name(self, name): - if not is_string(name): + if not isinstance(name, basestring): msg = 'Unable to set name for Nuclide with a non-string ' \ 'value {0}'.format(name) raise ValueError(msg) @@ -77,7 +80,7 @@ class Nuclide(object): @xs.setter def xs(self, xs): - if not is_string(xs): + if not isinstance(xs, basestring): msg = 'Unable to set cross-section identifier xs for Nuclide ' \ 'with a non-string value {0}'.format(xs) raise ValueError(msg) @@ -86,7 +89,7 @@ class Nuclide(object): @zaid.setter def zaid(self, zaid): - if not is_integer(zaid): + if not isinstance(zaid, Integral): msg = 'Unable to set zaid for Nuclide ' \ 'with a non-integer {0}'.format(zaid) raise ValueError(msg) diff --git a/openmc/plots.py b/openmc/plots.py index a2365b392..e0c076eda 100644 --- a/openmc/plots.py +++ b/openmc/plots.py @@ -1,10 +1,14 @@ +from collections import Sequence +from numbers import Real, Integral from xml.etree import ElementTree as ET +import sys import numpy as np -from openmc.checkvalue import * from openmc.clean_xml import * +if sys.version_info[0] >= 3: + basestring = str # A static variable for auto-generated Plot IDs AUTO_PLOT_ID = 10000 @@ -35,9 +39,9 @@ class Plot(object): Unique identifier name : str Name of the plot - width : tuple or list or ndarray + width : Sequence of float Width of the plot in each basis direction - pixels : tuple or list or ndarray + pixels : Sequence of int Number of pixels to use in each basis direction origin : tuple or list of ndarray Origin (center) of the plot @@ -51,9 +55,9 @@ class Plot(object): The basis directions for the plot background : tuple or list of ndarray Color of the background defined by RGB - mask_components : tuple or list or ndarray + mask_components : Sequence of int Unique id numbers of the cells or materials to plot - mask_background : tuple or list or ndarray + mask_background : Sequence of int Color to apply to all cells/materials not listed in mask_components defined by RGB col_spec : dict @@ -138,7 +142,7 @@ class Plot(object): AUTO_PLOT_ID += 1 # Check that the ID is an integer and wasn't already used - elif not is_integer(plot_id): + elif not isinstance(plot_id, Integral): msg = 'Unable to set a non-integer Plot ID {0}'.format(plot_id) raise ValueError(msg) @@ -153,7 +157,7 @@ class Plot(object): @name.setter def name(self, name): - if not is_string(name): + if not isinstance(name, basestring): msg = 'Unable to set name for Plot ID={0} with a non-string ' \ 'value {1}'.format(self._id, name) raise ValueError(msg) @@ -163,9 +167,9 @@ class Plot(object): @width.setter def width(self, width): - if not isinstance(width, (tuple, list, np.ndarray)): + if not isinstance(width, Sequence): msg = 'Unable to create Plot ID={0} with width {1} which is not ' \ - 'a Python tuple/list or NumPy array'.format(self._id, width) + 'a sequence'.format(self._id, width) raise ValueError(msg) elif len(width) != 2 and len(width) != 3: @@ -174,7 +178,7 @@ class Plot(object): raise ValueError(msg) for dim in width: - if not is_integer(dim) and not is_float(dim): + if not isinstance(dim, Real): msg = 'Unable to create Plot ID={0} with width {1} since ' \ 'each element must be a floating point value or ' \ 'integer'.format(self._id, width) @@ -184,9 +188,9 @@ class Plot(object): @origin.setter def origin(self, origin): - if not isinstance(origin, (tuple, list, np.ndarray)): + if not isinstance(origin, Sequence): msg = 'Unable to create Plot ID={0} with origin {1} which is not ' \ - 'a Python tuple/list or NumPy array'.format(self._id, origin) + 'a sequnce'.format(self._id, origin) raise ValueError(msg) elif len(origin) != 3: @@ -195,7 +199,7 @@ class Plot(object): raise ValueError(msg) for dim in origin: - if not is_integer(dim) and not is_float(dim): + if not isinstance(dim, Real): msg = 'Unable to create Plot ID={0} with origin {1} since ' \ 'each element must be a floating point value or ' \ 'integer'.format(self._id, origin) @@ -205,9 +209,9 @@ class Plot(object): @pixels.setter def pixels(self, pixels): - if not isinstance(pixels, (tuple, list, np.ndarray)): + if not isinstance(pixels, Sequence): msg = 'Unable to create Plot ID={0} with pixels {1} which is not ' \ - 'a Python tuple/list or NumPy array'.format(self._id, pixels) + 'not a sequence'.format(self._id, pixels) raise ValueError(msg) elif len(pixels) != 2 and len(pixels) != 3: @@ -216,7 +220,7 @@ class Plot(object): raise ValueError(msg) for dim in pixels: - if not is_integer(dim): + if not isinstance(dim, Integral): msg = 'Unable to create Plot ID={0} with pixel value {1} ' \ 'which is not an integer'.format(self._id, dim) raise ValueError(msg) @@ -230,7 +234,7 @@ class Plot(object): @filename.setter def filename(self, filename): - if not is_string(filename): + if not isinstance(filename, basestring): msg = 'Unable to create Plot ID={0} with filename {1} which is ' \ 'not a string'.format(self._id, filename) raise ValueError(msg) @@ -239,7 +243,7 @@ class Plot(object): @color.setter def color(self, color): - if not is_string(color): + if not isinstance(color, basestring): msg = 'Unable to create Plot ID={0} with color {1} which is not ' \ 'a string'.format(self._id, color) raise ValueError(msg) @@ -253,7 +257,7 @@ class Plot(object): @type.setter def type(self, type): - if not is_string(type): + if not isinstance(type, basestring): msg = 'Unable to create Plot ID={0} with type {1} which is not ' \ 'a string'.format(self._id, type) raise ValueError(msg) @@ -267,7 +271,7 @@ class Plot(object): @basis.setter def basis(self, basis): - if not is_string(basis): + if not isinstance(basis, basestring): msg = 'Unable to create Plot ID={0} with basis {1} which is not ' \ 'a string'.format(self._id, basis) raise ValueError(msg) @@ -281,10 +285,9 @@ class Plot(object): @background.setter def background(self, background): - if not isinstance(background, (tuple, list, np.ndarray)): + if not isinstance(background, Sequence): msg = 'Unable to create Plot ID={0} with background {1} ' \ - 'which is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, background) + 'which is not a sequence'.format(self._id, background) raise ValueError(msg) elif len(background) != 3: @@ -294,7 +297,7 @@ class Plot(object): raise ValueError(msg) for rgb in background: - if not is_integer(rgb): + if not isinstance(rgb, Integral): msg = 'Unable to create Plot ID={0} with background RGB ' \ 'value {1} which is not an integer'.format(self._id, rgb) raise ValueError(msg) @@ -315,7 +318,7 @@ class Plot(object): raise ValueError(msg) for key in col_spec: - if not is_integer(key): + if not isinstance(key, Integral): msg = 'Unable to create Plot ID={0} with col_spec ID {1} ' \ 'which is not an integer'.format(self._id, key) raise ValueError(msg) @@ -325,10 +328,9 @@ class Plot(object): 'which is less than 0'.format(self._id, key) raise ValueError(msg) - elif not isinstance(col_spec[key], (tuple, list, np.ndarray)): - msg = 'Unable to create Plot ID={0} with col_spec RGB ' \ - 'values {1} which is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, col_spec[key]) + elif not isinstance(col_spec[key], Sequence): + msg = 'Unable to create Plot ID={0} with col_spec RGB values' \ + ' {1} which is not a sequence'.format(self._id, col_spec[key]) raise ValueError(msg) elif len(col_spec[key]) != 3: @@ -341,14 +343,13 @@ class Plot(object): @mask_componenets.setter def mask_components(self, mask_components): - if not isinstance(mask_components, (list, tuple, np.ndarray)): + if not isinstance(mask_components, Sequence): msg = 'Unable to create Plot ID={0} with mask components {1} ' \ - 'which is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, mask_components) + 'which is not a sequence'.format(self._id, mask_components) raise ValueError(msg) for component in mask_components: - if not is_integer(component): + if not isinstance(component, Integral): msg = 'Unable to create Plot ID={0} with mask component {1} ' \ 'which is not an integer'.format(self._id, component) raise ValueError(msg) @@ -362,10 +363,9 @@ class Plot(object): @mask_background.setter def mask_background(self, mask_background): - if not isinstance(mask_background, (list, tuple, np.ndarray)): + if not isinstance(mask_background, Sequence): msg = 'Unable to create Plot ID={0} with mask background {1} ' \ - 'which is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, mask_background) + 'which is not a sequence'.format(self._id, mask_background) raise ValueError(msg) elif len(mask_background) != 3 and len(mask_background) != 0: @@ -376,7 +376,7 @@ class Plot(object): for rgb in mask_background: - if not is_integer(rgb): + if not isinstance(rgb, Integral): msg = 'Unable to create Plot ID={0} with mask background RGB ' \ 'value {1} which is not an integer'.format(self._id, rgb) raise ValueError(msg) diff --git a/openmc/settings.py b/openmc/settings.py index d3ac7f865..328760625 100644 --- a/openmc/settings.py +++ b/openmc/settings.py @@ -1,13 +1,18 @@ -import collections +from collections import Sequence +from numbers import Real, Integral import warnings from xml.etree import ElementTree as ET +import sys import numpy as np -from openmc.checkvalue import * from openmc.clean_xml import * +if sys.version_info[0] >= 3: + basestring = str + + class SettingsFile(object): """Settings file used for an OpenMC simulation. Corresponds directly to the settings.xml input file. @@ -38,11 +43,11 @@ class SettingsFile(object): Path to write output to verbosity : int Verbosity during simulation between 1 and 10 - statepoint_batches : tuple or list or ndarray + statepoint_batches : Sequence of int List of batches at which to write statepoint files statepoint_interval : int Number of batches after which a new statepoint file should be written - sourcepoint_batches : tuple or list or ndarray + sourcepoint_batches : Sequence of int List of batches at which to write source files sourcepoint_interval : int Number of batches after which a new source file should be written @@ -395,7 +400,7 @@ class SettingsFile(object): @batches.setter def batches(self, batches): - if not is_integer(batches): + if not isinstance(batches, Integral): msg = 'Unable to set batches to a non-integer ' \ 'value {0}'.format(batches) raise ValueError(msg) @@ -409,7 +414,7 @@ class SettingsFile(object): @generations_per_batch.setter def generations_per_batch(self, generations_per_batch): - if not is_integer(generations_per_batch): + if not isinstance(generations_per_batch, Integral): msg = 'Unable to set generations per batch to a non-integer ' \ 'value {0}'.format(generations_per_batch) raise ValueError(msg) @@ -423,7 +428,7 @@ class SettingsFile(object): @inactive.setter def inactive(self, inactive): - if not is_integer(inactive): + if not isinstance(inactive, Integral): msg = 'Unable to set inactive batches to a non-integer ' \ 'value {0}'.format(inactive) raise ValueError(msg) @@ -437,7 +442,7 @@ class SettingsFile(object): @particles.setter def particles(self, particles): - if not is_integer(particles): + if not isinstance(particles, Integral): msg = 'Unable to set particles to a non-integer ' \ 'value {0}'.format(particles) raise ValueError(msg) @@ -471,7 +476,7 @@ class SettingsFile(object): 'does not have a "threshold" key'.format(keff_trigger) raise ValueError(msg) - elif not is_float(keff_trigger['threshold']): + elif not isinstance(keff_trigger['threshold'], Real): msg = 'Unable to set a trigger on keff with ' \ 'threshold {0}'.format(keff_trigger['threshold']) raise ValueError(msg) @@ -480,7 +485,7 @@ class SettingsFile(object): @source_file.setter def source_file(self, source_file): - if not is_string(source_file): + if not isinstance(source_file, basestring): msg = 'Unable to set source file to a non-string ' \ 'value {0}'.format(source_file) raise ValueError(msg) @@ -499,7 +504,7 @@ class SettingsFile(object): distribution samples locations from a "box" distribution but only locations in fissionable materials are accepted. A "point" spatial distribution has coordinates specified by a triplet. - params : tuple or list or ndarray + params : Sequence of float For a "box" or "fission" spatial distribution, ``params`` should be given as six real numbers, the first three of which specify the lower-left corner of a parallelepiped and the last three of which @@ -512,7 +517,7 @@ class SettingsFile(object): """ - if not is_string(stype): + if not isinstance(stype, basestring): msg = 'Unable to set source space type to a non-string ' \ 'value {0}'.format(stype) raise ValueError(msg) @@ -522,9 +527,9 @@ class SettingsFile(object): 'box or point'.format(stype) raise ValueError(msg) - elif not isinstance(params, (tuple, list, np.ndarray)): + elif not isinstance(params, Sequence): msg = 'Unable to set source space parameters to {0} since it is ' \ - 'not a Python tuple, list or NumPy array'.format(params) + 'not a sequence'.format(params) raise ValueError(msg) elif stype in ['box', 'fission'] and len(params) != 6: @@ -539,9 +544,9 @@ class SettingsFile(object): raise ValueError(msg) for param in params: - if not is_integer(param) and not is_float(param): + if not isinstance(param, Real): msg = 'Unable to set source space parameters to {0} since it ' \ - 'is not an integer or floating point value'.format(param) + 'is not a real number'.format(param) raise ValueError(msg) self._source_space_type = stype @@ -558,7 +563,7 @@ class SettingsFile(object): site is isotropic if the "isotropic" option is given. The angle of the particle emitted from a source site is the direction specified in ``params`` if the "monodirectional" option is given. - params : tuple or list or ndarray + params : Sequence of float For an "isotropic" angular distribution, ``params`` should not be specified. @@ -568,7 +573,7 @@ class SettingsFile(object): """ - if not is_string(stype): + if not isinstance(stype, basestring): msg = 'Unable to set source angle type to a non-string ' \ 'value {0}'.format(stype) raise ValueError(msg) @@ -578,9 +583,9 @@ class SettingsFile(object): 'isotropic or monodirectional'.format(stype) raise ValueError(msg) - elif not isinstance(params, (tuple, list, np.ndarray)): + elif not isinstance(params, Sequence): msg = 'Unable to set source angle parameters to {0} since it is ' \ - 'not a Python list/tuple or NumPy array'.format(params) + 'not a sequence'.format(params) raise ValueError(msg) elif stype == 'isotropic' and params is not None: @@ -595,9 +600,9 @@ class SettingsFile(object): raise ValueError(msg) for param in params: - if not is_integer(param) and not is_float(param): + if not isinstance(param, Real): msg = 'Unable to set source angle parameters to {0} since it ' \ - 'is not an integer or floating point value'.format(param) + 'is not a real number'.format(param) raise ValueError(msg) self._source_angle_type = stype @@ -615,7 +620,7 @@ class SettingsFile(object): whose energy is sampled from a Watt fission spectrum. The "maxwell" option produce source sites whose energy is sampled from a Maxwell fission spectrum. - params : tuple or list or ndarray + params : Sequence of float For a "monoenergetic" energy distribution, ``params`` should be given as the energy in MeV of the source sites. @@ -629,7 +634,7 @@ class SettingsFile(object): """ - if not is_string(stype): + if not isinstance(stype, basestring): msg = 'Unable to set source energy type to a non-string ' \ 'value {0}'.format(stype) raise ValueError(msg) @@ -639,9 +644,9 @@ class SettingsFile(object): 'monoenergetic, watt or maxwell'.format(stype) raise ValueError(msg) - elif not isinstance(params, (tuple, list, np.ndarray)): + elif not isinstance(params, Sequence): msg = 'Unable to set source energy params to {0} since it ' \ - 'is not a Python list/tuple or NumPy array'.format(params) + 'is not a sequence'.format(params) raise ValueError(msg) elif stype == 'monoenergetic' and not len(params) != 1: @@ -662,10 +667,9 @@ class SettingsFile(object): raise ValueError(msg) for param in params: - if not is_integer(param) and not is_float(param): + if not isinstance(param, Real): msg = 'Unable to set source energy params to {0} ' \ - 'since it is not an integer or floating point ' \ - 'value'.format(param) + 'since it is not a real number'.format(param) raise ValueError(msg) self._source_energy_type = stype @@ -694,7 +698,7 @@ class SettingsFile(object): @output_path.setter def output_path(self, output_path): - if not is_string(output_path): + if not isinstance(output_path, basestring): msg = 'Unable to set output path to non-string ' \ 'value {0}'.format(output_path) raise ValueError(msg) @@ -703,7 +707,7 @@ class SettingsFile(object): @verbosity.setter def verbosity(self, verbosity): - if not is_integer(verbosity): + if not isinstance(verbosity, Integral): msg = 'Unable to set verbosity to non-integer ' \ 'value {0}'.format(verbosity) raise ValueError(msg) @@ -717,13 +721,13 @@ class SettingsFile(object): @statepoint_batches.setter def statepoint_batches(self, batches): - if not isinstance(batches, (tuple, list, np.ndarray)): - msg = 'Unable to set statepoint batches to {0} which is not a ' \ - 'Python tuple/list or NumPy array'.format(batches) + if not isinstance(batches, Sequence): + msg = 'Unable to set statepoint batches to {0} which is not ' \ + 'a sequnce'.format(batches) raise ValueError(msg) for batch in batches: - if not is_integer(batch): + if not isinstance(batch, Integral): msg = 'Unable to set statepoint batches with non-integer ' \ 'value {0}'.format(batch) raise ValueError(msg) @@ -737,7 +741,7 @@ class SettingsFile(object): @statepoint_interval.setter def statepoint_interval(self, interval): - if not is_integer(interval): + if not isinstance(interval, Integral): msg = 'Unable to set statepoint interval to non-integer ' \ 'value {0}'.format(interval) raise ValueError(msg) @@ -746,13 +750,13 @@ class SettingsFile(object): @sourcepoint_batches.setter def sourcepoint_batches(self, batches): - if not isinstance(batches, (tuple, list, np.ndarray)): + if not isinstance(batches, Sequence): msg = 'Unable to set sourcepoint batches to {0} which is ' \ - 'not a Python tuple/list or NumPy array'.format(batches) + 'not a sequence'.format(batches) raise ValueError(msg) for batch in batches: - if not is_integer(batch): + if not isinstance(batch, Integral): msg = 'Unable to set sourcepoint batches with non-integer ' \ 'value {0}'.format(batch) raise ValueError(msg) @@ -766,7 +770,7 @@ class SettingsFile(object): @sourcepoint_interval.setter def sourcepoint_interval(self, interval): - if not is_integer(interval): + if not isinstance(interval, Integral): msg = 'Unable to set sourcepoint interval to non-integer ' \ 'value {0}'.format(interval) raise ValueError(msg) @@ -811,7 +815,7 @@ class SettingsFile(object): @cross_sections.setter def cross_sections(self, cross_sections): - if not is_string(cross_sections): + if not isinstance(cross_sections, basestring): msg = 'Unable to set cross sections to non-string ' \ 'value {0}'.format(cross_sections) raise ValueError(msg) @@ -847,7 +851,7 @@ class SettingsFile(object): @seed.setter def seed(self, seed): - if not is_integer(seed): + if not isinstance(seed, Integral): msg = 'Unable to set seed to non-integer value {0}'.format(seed) raise ValueError(msg) @@ -868,7 +872,7 @@ class SettingsFile(object): @weight.setter def weight(self, weight): - if not is_float(weight): + if not isinstance(weight, Real): msg = 'Unable to set weight cutoff to non-floating point ' \ 'value {0}'.format(weight) raise ValueError(msg) @@ -882,7 +886,7 @@ class SettingsFile(object): @weight_avg.setter def weight_avg(self, weight_avg): - if not is_float(weight_avg): + if not isinstance(weight_avg, Real): msg = 'Unable to set weight avg. to non-floating point ' \ 'value {0}'.format(weight_avg) raise ValueError(msg) @@ -895,9 +899,9 @@ class SettingsFile(object): @entropy_dimension.setter def entropy_dimension(self, dimension): - if not isinstance(dimension, (tuple, list)): + if not isinstance(dimension, Sequence): msg = 'Unable to set entropy mesh dimension to {0} which is ' \ - 'not a Python tuple or list'.format(dimension) + 'not a sequence'.format(dimension) raise ValueError(msg) elif len(dimension) != 3: @@ -906,7 +910,7 @@ class SettingsFile(object): raise ValueError(msg) for dim in dimension: - if not is_integer(dim) and not is_float(dim): + if not isinstance(dim, Real): msg = 'Unable to set entropy mesh dimension to a ' \ 'non-integer or floating point value {0}'.format(dim) raise ValueError(msg) @@ -915,9 +919,9 @@ class SettingsFile(object): @entropy_lower_left.setter def entropy_lower_left(self, lower_left): - if not isinstance(lower_left, (tuple, list)): + if not isinstance(lower_left, Sequence): msg = 'Unable to set entropy mesh lower left corner to {0} which ' \ - 'is not a Python tuple or list'.format(lower_left) + 'is not a sequence'.format(lower_left) raise ValueError(msg) elif len(lower_left) != 3: @@ -926,7 +930,7 @@ class SettingsFile(object): raise ValueError(msg) for coord in lower_left: - if not is_integer(coord) and not is_float(coord): + if not isinstance(coord, Real): msg = 'Unable to set entropy mesh lower left corner to a ' \ 'non-integer or floating point value {0}'.format(coord) raise ValueError(msg) @@ -935,9 +939,9 @@ class SettingsFile(object): @entropy_upper_right.setter def entropy_upper_right(self, upper_right): - if not isinstance(upper_right, (tuple, list)): + if not isinstance(upper_right, Sequence): msg = 'Unable to set entropy mesh upper right corner to {0} ' \ - 'which is not a Python tuple or list'.format(upper_right) + 'which is not a sequence'.format(upper_right) raise ValueError(msg) elif len(upper_right) < 3 or len(upper_right) > 3: @@ -946,7 +950,7 @@ class SettingsFile(object): raise ValueError(msg) for coord in upper_right: - if not is_integer(coord) and not is_float(coord): + if not isinstance(coord, Real): msg = 'Unable to set entropy mesh upper right corner to a ' \ 'non-integer or floating point value {0}'.format(coord) raise ValueError(msg) @@ -964,7 +968,7 @@ class SettingsFile(object): @trigger_max_batches.setter def trigger_max_batches(self, trigger_max_batches): - if not is_integer(trigger_max_batches): + if not isinstance(trigger_max_batches, Integral): msg = 'Unable to set trigger max batches to a non-integer ' \ 'value {0}'.format(trigger_max_batches) raise ValueError(msg) @@ -978,7 +982,7 @@ class SettingsFile(object): @trigger_batch_interval.setter def trigger_batch_interval(self, trigger_batch_interval): - if not is_integer(trigger_batch_interval): + if not isinstance(trigger_batch_interval, Integral): msg = 'Unable to set trigger batch interval to a non-integer ' \ 'value {0}'.format(trigger_batch_interval) raise ValueError(msg) @@ -1001,7 +1005,7 @@ class SettingsFile(object): @threads.setter def threads(self, threads): - if not is_integer(threads): + if not isinstance(threads, Integral): msg = 'Unable to set the threads to a non-integer ' \ 'value {0}'.format(threads) raise ValueError(msg) @@ -1073,9 +1077,9 @@ class SettingsFile(object): @ufs_dimension.setter def ufs_dimension(self, dimension): - if not isinstance(dimension, (tuple, list)): + if not isinstance(dimension, Sequence): msg = 'Unable to set UFS mesh dimension to {0} which is ' \ - 'not a Python tuple or list'.format(dimension) + 'not a sequence'.format(dimension) raise ValueError(msg) elif len(dimension) != 3: @@ -1084,7 +1088,7 @@ class SettingsFile(object): raise ValueError(msg) for dim in dimension: - if not is_integer(dim): + if not isinstance(dim, Integral): msg = 'Unable to set entropy mesh dimension to a ' \ 'non-integer {0}'.format(dim) raise ValueError(msg) @@ -1097,9 +1101,9 @@ class SettingsFile(object): @ufs_lower_left.setter def ufs_lower_left(self, lower_left): - if not isinstance(lower_left, (tuple, list, np.ndarray)): + if not isinstance(lower_left, Sequence): msg = 'Unable to set UFS mesh lower left corner to {0} which is ' \ - 'not a Python tuple or list'.format(lower_left) + 'not a sequence'.format(lower_left) raise ValueError(msg) elif len(lower_left) != 3: @@ -1111,9 +1115,9 @@ class SettingsFile(object): @ufs_upper_right.setter def ufs_upper_right(self, upper_right): - if not isinstance(upper_right, (tuple, list)): + if not isinstance(upper_right, Sequence): msg = 'Unable to set UFs mesh upper right corner to {0} which is ' \ - 'not a Python tuple or list'.format(upper_right) + 'not a sequence'.format(upper_right) raise ValueError(msg) if len(upper_right) != 3: @@ -1129,9 +1133,9 @@ class SettingsFile(object): warnings.warn('This feature is not yet implemented in a release ' 'version of openmc') - if not isinstance(dimension, (tuple, list)): + if not isinstance(dimension, Sequence): msg = 'Unable to set DD mesh upper right corner to {0} which is ' \ - 'not a Python tuple or list'.format(dimension) + 'not a sequence'.format(dimension) raise ValueError(msg) if len(dimension) != 3: @@ -1147,9 +1151,9 @@ class SettingsFile(object): warnings.warn('This feature is not yet implemented in a release ' 'version of openmc') - if not isinstance(lower_left, (tuple, list, np.ndarray)): + if not isinstance(lower_left, Sequence): msg = 'Unable to set DD mesh lower left corner to {0} which is ' \ - 'not a Python tuple or list'.format(lower_left) + 'not a sequence'.format(lower_left) raise ValueError(msg) elif len(lower_left) < 3 or len(lower_left) > 3: @@ -1184,9 +1188,9 @@ class SettingsFile(object): warnings.warn('This feature is not yet implemented in a release ' 'version of openmc') - if not isinstance(nodemap, (tuple, list)): + if not isinstance(nodemap, Sequence): msg = 'Unable to set DD nodemap {0} which is ' \ - 'not a Python tuple or list'.format(nodemap) + 'not a sequence'.format(nodemap) raise ValueError(msg) nodemap = np.array(nodemap).flatten() diff --git a/openmc/surface.py b/openmc/surface.py index 940590173..e2e11c624 100644 --- a/openmc/surface.py +++ b/openmc/surface.py @@ -1,9 +1,12 @@ from abc import ABCMeta +from numbers import Real, Integral from xml.etree import ElementTree as ET +import sys -from openmc.checkvalue import * from openmc.constants import BC_TYPES +if sys.version_info[0] >= 3: + basestring = str # A static variable for auto-generated Surface IDs AUTO_SURFACE_ID = 10000 @@ -91,7 +94,7 @@ class Surface(object): AUTO_SURFACE_ID += 1 # Check that the ID is an integer and wasn't already used - elif not is_integer(surface_id): + elif not isinstance(surface_id, Integral): msg = 'Unable to set a non-integer Surface ' \ 'ID {0}'.format(surface_id) raise ValueError(msg) @@ -106,7 +109,7 @@ class Surface(object): @name.setter def name(self, name): - if not is_string(name): + if not isinstance(name, basestring): msg = 'Unable to set name for Surface ID={0} with a non-string ' \ 'value {1}'.format(self._id, name) raise ValueError(msg) @@ -116,7 +119,7 @@ class Surface(object): @boundary_type.setter def boundary_type(self, boundary_type): - if not is_string(boundary_type): + if not isinstance(boundary_type, basestring): msg = 'Unable to set boundary type for Surface ID={0} with a ' \ 'non-string value {1}'.format(self._id, boundary_type) raise ValueError(msg) @@ -235,7 +238,7 @@ class Plane(Surface): @a.setter def a(self, A): - if not is_integer(A) and not is_float(A): + if not isinstance(A, Real): msg = 'Unable to set A coefficient for Plane ID={0} to a ' \ 'non-integer value {1}'.format(self._id, A) raise ValueError(msg) @@ -244,7 +247,7 @@ class Plane(Surface): @b.setter def b(self, B): - if not is_integer(B) and not is_float(B): + if not isinstance(B, Real): msg = 'Unable to set B coefficient for Plane ID={0} to a ' \ 'non-integer value {1}'.format(self._id, B) raise ValueError(msg) @@ -253,7 +256,7 @@ class Plane(Surface): @c.setter def c(self, C): - if not is_integer(C) and not is_float(C): + if not isinstance(C, Real): msg = 'Unable to set C coefficient for Plane ID={0} to a ' \ 'non-integer value {1}'.format(self._id, C) raise ValueError(msg) @@ -262,7 +265,7 @@ class Plane(Surface): @d.setter def d(self, D): - if not is_integer(D) and not is_float(D): + if not isinstance(D, Real): msg = 'Unable to set D coefficient for Plane ID={0} to a ' \ 'non-integer value {1}'.format(self._id, D) raise ValueError(msg) @@ -312,7 +315,7 @@ class XPlane(Plane): @x0.setter def x0(self, x0): - if not is_integer(x0) and not is_float(x0): + if not isinstance(x0, Real): msg = 'Unable to set x0 coefficient for XPlane ID={0} to a ' \ 'non-integer value {1}'.format(self._id, x0) raise ValueError(msg) @@ -362,7 +365,7 @@ class YPlane(Plane): @y0.setter def y0(self, y0): - if not is_integer(y0) and not is_float(y0): + if not isinstance(y0, Real): msg = 'Unable to set y0 coefficient for XPlane ID={0} to a ' \ 'non-integer value {1}'.format(self._id, y0) raise ValueError(msg) @@ -412,7 +415,7 @@ class ZPlane(Plane): @z0.setter def z0(self, z0): - if not is_integer(z0) and not is_float(z0): + if not isinstance(z0, Real): msg = 'Unable to set z0 coefficient for ZPlane ID={0} to a ' \ 'non-integer value {1}'.format(self._id, z0) raise ValueError(msg) @@ -463,7 +466,7 @@ class Cylinder(Surface): @r.setter def r(self, R): - if not is_integer(R) and not is_float(R): + if not isinstance(R, Real): msg = 'Unable to set R coefficient for Cylinder ID={0} to a ' \ 'non-integer value {1}'.format(self._id, R) raise ValueError(msg) @@ -527,7 +530,7 @@ class XCylinder(Cylinder): @y0.setter def y0(self, y0): - if not is_integer(y0) and not is_float(y0): + if not isinstance(y0, Real): msg = 'Unable to set y0 coefficient for XCylinder ID={0} to a ' \ 'non-integer value {1}'.format(self._id, y0) raise ValueError(msg) @@ -536,7 +539,7 @@ class XCylinder(Cylinder): @z0.setter def z0(self, z0): - if not is_integer(z0) and not is_float(z0): + if not isinstance(z0, Real): msg = 'Unable to set z0 coefficient for XCylinder ID={0} to a ' \ 'non-integer value {1}'.format(self._id, z0) raise ValueError(msg) @@ -600,7 +603,7 @@ class YCylinder(Cylinder): @x0.setter def x0(self, x0): - if not is_integer(x0) and not is_float(x0): + if not isinstance(x0, Real): msg = 'Unable to set x0 coefficient for YCylinder ID={0} to a ' \ 'non-integer value {1}'.format(self._id, x0) raise ValueError(msg) @@ -609,7 +612,7 @@ class YCylinder(Cylinder): @z0.setter def z0(self, z0): - if not is_integer(z0) and not is_float(z0): + if not isinstance(z0, Real): msg = 'Unable to set z0 coefficient for YCylinder ID={0} to a ' \ 'non-integer value {1}'.format(self._id, z0) raise ValueError(msg) @@ -673,7 +676,7 @@ class ZCylinder(Cylinder): @x0.setter def x0(self, x0): - if not is_integer(x0) and not is_float(x0): + if not isinstance(x0, Real): msg = 'Unable to set x0 coefficient for ZCylinder ID={0} to a ' \ 'non-integer value {1}'.format(self._id, x0) raise ValueError(msg) @@ -682,7 +685,7 @@ class ZCylinder(Cylinder): @y0.setter def y0(self, y0): - if not is_integer(y0) and not is_float(y0): + if not isinstance(y0, Real): msg = 'Unable to set y0 coefficient for ZCylinder ID={0} to a ' \ 'non-integer value {1}'.format(self._id, y0) raise ValueError(msg) @@ -764,7 +767,7 @@ class Sphere(Surface): @x0.setter def x0(self, x0): - if not is_integer(x0) and not is_float(x0): + if not isinstance(x0, Real): msg = 'Unable to set x0 coefficient for Sphere ID={0} to a ' \ 'non-integer value {1}'.format(self._id, x0) raise ValueError(msg) @@ -773,7 +776,7 @@ class Sphere(Surface): @y0.setter def y0(self, y0): - if not is_integer(y0) and not is_float(y0): + if not isinstance(y0, Real): msg = 'Unable to set y0 coefficient for Sphere ID={0} to a ' \ 'non-integer value {1}'.format(self._id, y0) raise ValueError(msg) @@ -782,7 +785,7 @@ class Sphere(Surface): @z0.setter def z0(self, z0): - if not is_integer(z0) and not is_float(z0): + if not isinstance(z0, Real): msg = 'Unable to set z0 coefficient for Sphere ID={0} to a ' \ 'non-integer value {1}'.format(self._id, z0) raise ValueError(msg) @@ -791,7 +794,7 @@ class Sphere(Surface): @r.setter def r(self, R): - if not is_integer(R) and not is_float(R): + if not isinstance(R, Real): msg = 'Unable to set R coefficient for Sphere ID={0} to a ' \ 'non-integer value {1}'.format(self._id, R) raise ValueError(msg) @@ -874,7 +877,7 @@ class Cone(Surface): @x0.setter def x0(self, x0): - if not is_integer(x0) and not is_float(x0): + if not isinstance(x0, Real): msg = 'Unable to set x0 coefficient for Cone ID={0} to a ' \ 'non-integer value {1}'.format(self._id, x0) raise ValueError(msg) @@ -883,7 +886,7 @@ class Cone(Surface): @y0.setter def y0(self, y0): - if not is_integer(y0) and not is_float(y0): + if not isinstance(y0, Real): msg = 'Unable to set y0 coefficient for Cone ID={0} to a ' \ 'non-integer value {1}'.format(self._id, y0) raise ValueError(msg) @@ -892,7 +895,7 @@ class Cone(Surface): @z0.setter def z0(self, z0): - if not is_integer(z0) and not is_float(z0): + if not isinstance(z0, Real): msg = 'Unable to set z0 coefficient for Cone ID={0} to a ' \ 'non-integer value {1}'.format(self._id, z0) raise ValueError(msg) @@ -901,7 +904,7 @@ class Cone(Surface): @r2.setter def r2(self, R2): - if not is_integer(R2) and not is_float(R2): + if not isinstance(R2, Real): msg = 'Unable to set R^2 coefficient for Cone ID={0} to a ' \ 'non-integer value {1}'.format(self._id, R2) raise ValueError(msg) diff --git a/openmc/tallies.py b/openmc/tallies.py index 0c3af950e..78b1e35ce 100644 --- a/openmc/tallies.py +++ b/openmc/tallies.py @@ -1,17 +1,22 @@ +from collections import Sequence import copy import os import pickle import itertools +from numbers import Integral from xml.etree import ElementTree as ET +import sys import numpy as np from openmc import Mesh, Filter, Trigger, Nuclide from openmc.summary import Summary from openmc.clean_xml import * -from openmc.checkvalue import * +if sys.version_info[0] >= 3: + basestring = str + # "Static" variable for auto-generated Tally IDs AUTO_TALLY_ID = 10000 @@ -303,7 +308,7 @@ class Tally(object): AUTO_TALLY_ID += 1 # Check that the ID is an integer and wasn't already used - elif not is_integer(tally_id): + elif not isinstance(tally_id, Integral): msg = 'Unable to set a non-integer Tally ID {0}'.format(tally_id) raise ValueError(msg) @@ -317,7 +322,7 @@ class Tally(object): @name.setter def name(self, name): - if not is_string(name): + if not isinstance(name, basestring): msg = 'Unable to set name for Tally ID={0} with a non-string ' \ 'value "{1}"'.format(self.id, name) raise ValueError(msg) @@ -366,7 +371,7 @@ class Tally(object): """ - if not is_string(score): + if not isinstance(score, basestring): msg = 'Unable to add score "{0}" to Tally ID={1} since it is ' \ 'not a string'.format(score, self.id) raise ValueError(msg) @@ -383,7 +388,7 @@ class Tally(object): @num_realizations.setter def num_realizations(self, num_realizations): - if not is_integer(num_realizations): + if not isinstance(num_realizations, Integral): msg = 'Unable to set the number of realizations to "{0}" for ' \ 'Tally ID={1} since it is not an ' \ 'integer'.format(num_realizations) @@ -408,19 +413,17 @@ class Tally(object): @sum.setter def sum(self, sum): - if not isinstance(sum, (tuple, list, np.ndarray)): + if not isinstance(sum, Sequence): msg = 'Unable to set the sum to "{0}" for Tally ID={1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(sum, self.id) + 'it is not a sequence'.format(sum, self.id) raise ValueError(msg) self._sum = sum @sum_sq.setter def sum_sq(self, sum_sq): - if not isinstance(sum_sq, (tuple, list, np.ndarray)): + if not isinstance(sum_sq, Sequence): msg = 'Unable to set the sum to "{0}" for Tally ID={1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(sum_sq, self.id) + 'it is not a sequence'.format(sum_sq, self.id) raise ValueError(msg) self._sum_sq = sum_sq @@ -1321,13 +1324,13 @@ class Tally(object): 'Tally.export_results(...)'.format(self.id) raise KeyError(msg) - if not is_string(filename): + if not isinstance(filename, basestring): msg = 'Unable to export the results for Tally ID={0} to ' \ 'filename="{1}" since it is not a ' \ 'string'.format(self.id, filename) raise ValueError(msg) - elif not is_string(directory): + elif not isinstance(directory, basestring): msg = 'Unable to export the results for Tally ID={0} to ' \ 'directory="{1}" since it is not a ' \ 'string'.format(self.id, directory) diff --git a/openmc/trigger.py b/openmc/trigger.py index 6f74a4fc0..23893f49d 100644 --- a/openmc/trigger.py +++ b/openmc/trigger.py @@ -1,6 +1,9 @@ +from numbers import Real from xml.etree import ElementTree as ET +import sys -from openmc.checkvalue import * +if sys.version_info[0] >= 3: + basestring = str class Trigger(object): @@ -76,7 +79,7 @@ class Trigger(object): @threshold.setter def threshold(self, threshold): - if not is_float(threshold): + if not isinstance(threshold, Real): msg = 'Unable to set a tally trigger threshold with ' \ 'threshold "{0}"'.format(threshold) raise ValueError(msg) @@ -93,7 +96,7 @@ class Trigger(object): """ - if not is_string(score): + if not isinstance(score, basestring): msg = 'Unable to add score "{0}" to tally trigger since ' \ 'it is not a string'.format(score) raise ValueError(msg) diff --git a/openmc/universe.py b/openmc/universe.py index c410c9f10..2dc6db755 100644 --- a/openmc/universe.py +++ b/openmc/universe.py @@ -1,11 +1,15 @@ import abc -from collections import OrderedDict +from collections import OrderedDict, Sequence +from numbers import Real, Integral from xml.etree import ElementTree as ET +import sys import openmc -from openmc.checkvalue import * +if sys.version_info[0] >= 3: + basestring = str + # A static variable for auto-generated Cell IDs AUTO_CELL_ID = 10000 @@ -106,7 +110,7 @@ class Cell(object): AUTO_CELL_ID += 1 # Check that the ID is an integer and wasn't already used - elif not is_integer(cell_id): + elif not isinstance(cell_id, Integral): msg = 'Unable to set a non-integer Cell ID {0}'.format(cell_id) raise ValueError(msg) @@ -156,10 +160,9 @@ class Cell(object): @rotation.setter def rotation(self, rotation): - if not isinstance(rotation, (tuple, list, np.ndarray)): + if not isinstance(rotation, Sequence): msg = 'Unable to add rotation {0} to Cell ID={1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(rotation, self._id) + 'it is not a sequence'.format(rotation, self._id) raise ValueError(msg) elif len(rotation) != 3: @@ -168,20 +171,18 @@ class Cell(object): raise ValueError(msg) for axis in rotation: - if not is_integer(axis) and not is_float(axis): + if not isinstance(axis, Real): msg = 'Unable to add rotation {0} to Cell ID={1} since ' \ - 'it is not an integer or floating point ' \ - 'value'.format(axis, self._id) + 'it is not a real number'.format(axis, self._id) raise ValueError(msg) self._rotation = rotation @translation.setter def translation(self, translation): - if not isinstance(translation, (tuple, list, np.ndarray)): + if not isinstance(translation, Sequence): msg = 'Unable to add translation {0} to Cell ID={1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(translation, self._id) + 'it is not a sequence'.format(translation, self._id) raise ValueError(msg) elif len(translation) != 3: @@ -190,20 +191,18 @@ class Cell(object): raise ValueError(msg) for axis in translation: - if not is_integer(axis) and not is_float(axis): + if not isinstance(axis, Real): msg = 'Unable to add translation {0} to Cell ID={1} since ' \ - 'it is not an integer or floating point ' \ - 'value'.format(axis, self._id) + 'it is not a real number'.format(axis, self._id) raise ValueError(msg) self._translation = translation @offsets.setter def offsets(self, offsets): - if not isinstance(offsets, (tuple, list, np.ndarray)): + if not isinstance(offsets, Sequence): msg = 'Unable to set offsets {0} to Cell ID={1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(offsets, self._id) + 'it is not a sequence'.format(offsets, self._id) raise ValueError(msg) self._offsets = offsets @@ -478,7 +477,7 @@ class Universe(object): AUTO_UNIVERSE_ID += 1 # Check that the ID is an integer and wasn't already used - elif not is_integer(universe_id): + elif not isinstance(universe_id, Integral): msg = 'Unable to set Universe ID to a non-integer ' \ '{0}'.format(universe_id) raise ValueError(msg) @@ -493,7 +492,7 @@ class Universe(object): @name.setter def name(self, name): - if not is_string(name): + if not isinstance(name, basestring): msg = 'Unable to set name for Universe ID={0} with a non-string ' \ 'value {1}'.format(self._id, name) raise ValueError(msg) @@ -531,13 +530,13 @@ class Universe(object): """ - if not isinstance(cells, (list, tuple, np.ndarray)): - msg = 'Unable to add Cells to Universe ID={0} since {1} is not a ' \ - 'Python tuple/list or NumPy array'.format(self._id, cells) + if not isinstance(cells, Sequence): + msg = 'Unable to add Cells to Universe ID={0} since {1} is not ' \ + 'a sequence'.format(self._id, cells) raise ValueError(msg) - for i in range(len(cells)): - self.add_cell(cells[i]) + for cell in cells: + self.add_cell(cell) def remove_cell(self, cell): """Remove a cell from the universe. @@ -732,7 +731,7 @@ class Lattice(object): AUTO_UNIVERSE_ID += 1 # Check that the ID is an integer and wasn't already used - elif not is_integer(lattice_id): + elif not isinstance(lattice_id, Integral): msg = 'Unable to set non-integer Lattice ID {0}'.format(lattice_id) raise ValueError(msg) @@ -746,7 +745,7 @@ class Lattice(object): @name.setter def name(self, name): - if not is_string(name): + if not isinstance(name, basestring): msg = 'Unable to set name for Lattice ID={0} with a non-string ' \ 'value {1}'.format(self._id, name) raise ValueError(msg) @@ -765,10 +764,9 @@ class Lattice(object): @universes.setter def universes(self, universes): - if not isinstance(universes, (tuple, list, np.ndarray)): + if not isinstance(universes, Sequence): msg = 'Unable to set Lattice ID={0} universes to {1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, universes) + 'it is not a sequence'.format(self._id, universes) raise ValueError(msg) self._universes = np.asarray(universes, dtype=Universe) @@ -908,10 +906,9 @@ class RectLattice(Lattice): @dimension.setter def dimension(self, dimension): - if not isinstance(dimension, (tuple, list, np.ndarray)): + if not isinstance(dimension, Sequence): msg = 'Unable to set RectLattice ID={0} dimension to {1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, dimension) + 'it is not a sequence'.format(self._id, dimension) raise ValueError(msg) elif len(dimension) != 2 and len(dimension) != 3: @@ -921,10 +918,9 @@ class RectLattice(Lattice): raise ValueError(msg) for dim in dimension: - if not is_integer(dim) and not is_float(dim): + if not isinstance(dim, Real): msg = 'Unable to set RectLattice ID={0} dimension to {1} since ' \ - 'it is not an integer or floating point ' \ - 'value'.format(self._id, dim) + 'it is not a real number'.format(self._id, dim) raise ValueError(msg) elif dim < 0: @@ -936,10 +932,9 @@ class RectLattice(Lattice): @lower_left.setter def lower_left(self, lower_left): - if not isinstance(lower_left, (tuple, list, np.ndarray)): + if not isinstance(lower_left, Sequence): msg = 'Unable to set RectLattice ID={0} lower_left to {1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, lower_left) + 'it is not a sequence'.format(self._id, lower_left) raise ValueError(msg) elif len(lower_left) != 2 and len(lower_left) != 3: @@ -949,30 +944,27 @@ class RectLattice(Lattice): raise ValueError(msg) for dim in lower_left: - if not is_integer(dim) and not is_float(dim): + if not isinstance(dim, Real): msg = 'Unable to set RectLattice ID={0} lower_left to {1} since ' \ - 'it is is not an integer or floating point ' \ - 'value'.format(self._id, dim) + 'it is is not a real number'.format(self._id, dim) raise ValueError(msg) self._lower_left = lower_left @offsets.setter def offsets(self, offsets): - if not isinstance(offsets, (tuple, list, np.ndarray)): + if not isinstance(offsets, Sequence): msg = 'Unable to set Lattice ID={0} offsets to {1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, offsets) + 'it is not a sequence'.format(self._id, offsets) raise ValueError(msg) self._offsets = offsets @Lattice.pitch.setter def pitch(self, pitch): - if not isinstance(pitch, (tuple, list, np.ndarray)): + if not isinstance(pitch, Sequence): msg = 'Unable to set Lattice ID={0} pitch to {1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, pitch) + 'it is not a sequence'.format(self._id, pitch) raise ValueError(msg) elif len(pitch) != 2 and len(pitch) != 3: @@ -981,10 +973,9 @@ class RectLattice(Lattice): raise ValueError(msg) for dim in pitch: - if not is_integer(dim) and not is_float(dim): + if not isinstance(dim, Real): msg = 'Unable to set Lattice ID={0} pitch to {1} since ' \ - 'it is not an an integer or floating point ' \ - 'value'.format(self._id, dim) + 'it is not a real number'.format(self._id, dim) raise ValueError(msg) elif dim < 0: @@ -1184,8 +1175,7 @@ class HexLattice(Lattice): @num_rings.setter def num_rings(self, num_rings): - - if not is_integer(num_rings) and num_rings < 1: + if not isinstance(num_rings, Integral) and num_rings < 1: msg = 'Unable to set HexLattice ID={0} number of rings to {1} ' \ 'since it is not a positive integer'.format(self._id, num_rings) raise ValueError(msg) @@ -1194,7 +1184,7 @@ class HexLattice(Lattice): @num_axial.setter def num_axial(self, num_axial): - if not is_integer(num_axial) and num_axial < 1: + if not isinstance(num_axial, Integral) and num_axial < 1: msg = 'Unable to set HexLattice ID={0} number of axial to {1} ' \ 'since it is not a positive integer'.format(self._id, num_axial) raise ValueError(msg) @@ -1203,10 +1193,9 @@ class HexLattice(Lattice): @center.setter def center(self, center): - if not isinstance(center, (tuple, list, np.ndarray)): + if not isinstance(center, Sequence): msg = 'Unable to set HexLattice ID={0} dimension to {1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, center) + 'it is not a sequence'.format(self._id, center) raise ValueError(msg) elif len(center) != 2 and len(center) != 3: @@ -1216,20 +1205,18 @@ class HexLattice(Lattice): raise ValueError(msg) for dim in center: - if not is_integer(dim) and not is_float(dim): + if not isinstance(dim, Real): msg = 'Unable to set HexLattice ID={0} center to {1} since ' \ - 'it is not an integer or floating point ' \ - 'value'.format(self._id, dim) + 'it is not a real number'.format(self._id, dim) raise ValueError(msg) self._center = center @Lattice.pitch.setter def pitch(self, pitch): - if not isinstance(pitch, (tuple, list, np.ndarray)): + if not isinstance(pitch, Sequence): msg = 'Unable to set Lattice ID={0} pitch to {1} since ' \ - 'it is not a Python tuple/list or NumPy ' \ - 'array'.format(self._id, pitch) + 'it is not a sequence'.format(self._id, pitch) raise ValueError(msg) elif len(pitch) != 2 and len(pitch) != 3: @@ -1238,10 +1225,9 @@ class HexLattice(Lattice): raise ValueError(msg) for dim in pitch: - if not is_integer(dim) and not is_float(dim): + if not isinstance(dim, Real): msg = 'Unable to set Lattice ID={0} pitch to {1} since ' \ - 'it is not an an integer or floating point ' \ - 'value'.format(self._id, dim) + 'it is not a real number'.format(self._id, dim) raise ValueError(msg) elif dim < 0: