Repository: ioos/compliance-checker

Issue:
IndexError: list index out of range in suite.py (running CF checks)
I'm still getting this error for a lot of files, e.g.

``` bash
$ cchecker.py -t=cf compliance_checker/tests/data/bad_data_type.nc 
Running Compliance Checker on the dataset from: compliance_checker/tests/data/bad_data_type.nc
Traceback (most recent call last):
  File "/home/mhidas/bin/cchecker.py", line 51, in <module>
    sys.exit(main())
  File "/home/mhidas/bin/cchecker.py", line 40, in main
    args.format)
  File "/home/mhidas/work/code/compliance-checker/compliance_checker/runner.py", line 33, in run_checker
    score_groups = cs.run(ds, *checker_names)
  File "/home/mhidas/work/code/compliance-checker/compliance_checker/suite.py", line 116, in run
    groups = self.scores(vals)
  File "/home/mhidas/work/code/compliance-checker/compliance_checker/suite.py", line 510, in scores
    grouped = self._group_raw(raw_scores)
  File "/home/mhidas/work/code/compliance-checker/compliance_checker/suite.py", line 569, in _group_raw
    cv = self._group_raw(map(trim_groups, v), k, level+1)
  File "/home/mhidas/work/code/compliance-checker/compliance_checker/suite.py", line 561, in _group_raw
    grouped = itertools.groupby(sorted(raw_scores, key=group_func), key=group_func)
  File "/home/mhidas/work/code/compliance-checker/compliance_checker/suite.py", line 552, in group_func
    retval = r.name[0:1][0]
IndexError: list index out of range
```

I think I first started seeing this issue after the new result naming scheme was introduced in #167 
As I mentioned before, I got as far as figuring out that it was probably due to multiple Result objects having exactly the same name.



Discussion:

Resolving patch:
diff --git a/compliance_checker/suite.py b/compliance_checker/suite.py
--- a/compliance_checker/suite.py
+++ b/compliance_checker/suite.py
@@ -549,7 +549,10 @@ def group_func(r):
             Slices off first element (if list/tuple) of classification or just returns it if scalar.
             """
             if isinstance(r.name, tuple) or isinstance(r.name, list):
-                retval = r.name[0:1][0]
+                if len(r.name) == 0:
+                    retval = ''
+                else:
+                    retval = r.name[0:1][0]
             else:
                 retval = r.name
             retval = retval.encode('ascii', 'ignore')


Test patch:
diff --git a/compliance_checker/tests/test_suite.py b/compliance_checker/tests/test_suite.py
--- a/compliance_checker/tests/test_suite.py
+++ b/compliance_checker/tests/test_suite.py
@@ -7,6 +7,7 @@
 static_files = {
         '2dim'                       : resource_filename('compliance_checker', 'tests/data/2dim-grid.nc'),
         'bad_region'                 : resource_filename('compliance_checker', 'tests/data/bad_region.nc'),
+        'bad_data_type'              : resource_filename('compliance_checker', 'tests/data/bad_data_type.nc'),
 }
 
 class TestSuite(unittest.TestCase):
@@ -50,3 +51,17 @@ def test_unicode_formatting(self):
             # This asserts that print is able to generate all of the unicode output
             cs.non_verbose_output_generation(score_list, groups, limit, points, out_of)
 
+    def test_group_func(self):
+        # This is checking for issue #183, where group_func results in
+        # IndexError: list index out of range
+        cs = CheckSuite()
+        cs.load_all_available_checkers()
+        ds = cs.load_dataset(static_files['bad_data_type'])
+        score_groups = cs.run(ds, 'cf')
+
+        limit = 2
+        for checker, rpair in score_groups.iteritems():
+            groups, errors = rpair
+            score_list, points, out_of = cs.standard_output(limit, checker, groups)
+            # This asserts that print is able to generate all of the unicode output
+            cs.non_verbose_output_generation(score_list, groups, limit, points, out_of)