From 4206f7ffc0135261c5b453de7cf091fceec345a6 Mon Sep 17 00:00:00 2001 From: Jason Boutte Date: Sat, 29 Aug 2020 00:31:19 +0000 Subject: [PATCH 1/2] Fixes passing tuple or list for weights --- Lib/averager.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/averager.py b/Lib/averager.py index 196726f..d261ee5 100644 --- a/Lib/averager.py +++ b/Lib/averager.py @@ -442,7 +442,7 @@ def __check_weightoptions(x, axisoptions, weightoptions): if __DEBUG__: print('axislist = ', axislist) # - if not isinstance(weightoptions, list): + if not isinstance(weightoptions, (list, tuple)): # # We have either 1 axis only or multiple axes and one MV2 of weights # @@ -490,6 +490,11 @@ def __check_weightoptions(x, axisoptions, weightoptions): # # We have multiple axes to deal with each with a weight.... # + + # Ensure we have a mutable list, handles tuple (https://github.com/CDAT/genutil/issues/32 + if not isinstance(weightoptions, list): + weightoptions = list(weightoptions) + for i in range(len(axislist)): weightoptions[i] = __check_each_weight_option( x, axislist[i], axisindex[i], weightoptions[i]) From bbf7c211a9cf9347d2e6660d1e33ddb7225c42f0 Mon Sep 17 00:00:00 2001 From: Jason Boutte Date: Sat, 29 Aug 2020 18:25:23 +0000 Subject: [PATCH 2/2] Fixes erorr messages --- .gitignore | 5 +++++ Lib/__init__.py | 1 + Lib/averager.py | 9 +-------- Lib/stats_checker.py | 10 +--------- tests/test_genutil_averager.py | 7 +++++++ tests/test_genutil_stats_missing.py | 5 +++++ 6 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cc22ff1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build/ +dist/ +genutil.egg-info/ +tests/__pycache__/ +tests/.last_failure diff --git a/Lib/__init__.py b/Lib/__init__.py index 8b4315a..26dd70b 100644 --- a/Lib/__init__.py +++ b/Lib/__init__.py @@ -21,6 +21,7 @@ import os # noqa import sys # noqa import cdat_info +from .stats_checker import StatisticsError # noqa # udunits bits from .udunits import udunits, addBaseUnit, addDimensionlessUnit, addScaledUnit # noqa diff --git a/Lib/averager.py b/Lib/averager.py index d261ee5..0c62e1d 100644 --- a/Lib/averager.py +++ b/Lib/averager.py @@ -11,14 +11,7 @@ class AveragerError (Exception): - def __init__(self, args=None): - """Create an exception""" - self.args = args - - def __str__(self): - """Calculate the string representation""" - return str(self.args) - __repr__ = __str__ + pass def _check_axisoptions(x, axisoptions): diff --git a/Lib/stats_checker.py b/Lib/stats_checker.py index b663919..c2a978c 100644 --- a/Lib/stats_checker.py +++ b/Lib/stats_checker.py @@ -5,15 +5,7 @@ class StatisticsError(Exception): - def __init__(self, args=None): - """Create an exception""" - self.args = args - - def __str__(self): - """Calculate the string representation""" - return str(self.args) - - __repr__ = __str__ + pass def __makeweights(x, w, axes): diff --git a/tests/test_genutil_averager.py b/tests/test_genutil_averager.py index 3749d57..0504972 100644 --- a/tests/test_genutil_averager.py +++ b/tests/test_genutil_averager.py @@ -6,6 +6,13 @@ import unittest class GENUTIL(unittest.TestCase): + def testError(self): + error = AveragerError("Custom error message") + + print(str(error)) + + self.assertTrue(str(error) == "Custom error message") + def testAverager(self): f=cdms2.open(os.path.join(cdat_info.get_sampledata_path(),'tas_ukmo_con.nc')) x = f('tas') diff --git a/tests/test_genutil_stats_missing.py b/tests/test_genutil_stats_missing.py index 0d1e825..cc3fc0b 100644 --- a/tests/test_genutil_stats_missing.py +++ b/tests/test_genutil_stats_missing.py @@ -4,6 +4,11 @@ import numpy class GENUTIL(unittest.TestCase): + def testError(self): + error = genutil.StatisticsError("Custom error message") + + self.assertTrue(str(error) == "Custom error message") + def testStatsMissing(self): a=MV2.array([1,2,3,4,5],mask=[0,0,1,0,0]) self.assertTrue(numpy.allclose(genutil.statistics.std(a),1.58113883008))