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

Fix error messages #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build/
dist/
genutil.egg-info/
tests/__pycache__/
tests/.last_failure
1 change: 1 addition & 0 deletions Lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions Lib/averager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -442,7 +435,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
#
Expand Down Expand Up @@ -490,6 +483,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])
Expand Down
10 changes: 1 addition & 9 deletions Lib/stats_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_genutil_averager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions tests/test_genutil_stats_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down