adapt to python2 returning lists and python3 iterators
authorDiane Trout <diane@ghic.org>
Fri, 20 Mar 2015 23:14:42 +0000 (16:14 -0700)
committerDiane Trout <diane@ghic.org>
Fri, 20 Mar 2015 23:14:42 +0000 (16:14 -0700)
This leads to code like next(iter(obj.values())) instead of
obj.values()[0]. Honestly Im not sure which is less ugly.

Also there's a few places where I have to cast things with list()
to get my tests to pass.

htsworkflow/pipelines/srf.py
htsworkflow/pipelines/test/test_eland.py
htsworkflow/pipelines/test/test_retrieve_config.py
htsworkflow/submission/test/test_results.py

index 2663ee39bd6453a02b4c9043d096e41e152171a7..b029339c6aefb3f65839f8faca8da84f0a46a39d 100644 (file)
@@ -112,7 +112,7 @@ def create_qseq_patterns(bustard_dir):
       qseq_patterns = []
       # grab a lane from the dictionary
       # I don't think it matters which one.
-      k = lanes.keys()[0]
+      k = next(iter(lanes.keys()))
       # build the list of patterns
       for read in lanes[k]:
         read = int(read)
index 3e5820e30bc9303a09f2a865959e11b155a8ac61..f1b74d8e3f2969b3a416a31846333249a9ea13ee 100644 (file)
@@ -20,8 +20,8 @@ class MatchCodeTests(TestCase):
                        'R0':0, 'R1':0, 'R2':0,
                       }
         self.assertEqual(mc.keys(), match_codes.keys())
-        self.assertEqual(mc.items(), match_codes.items())
-        self.assertEqual(mc.values(), match_codes.values())
+        self.assertEqual(list(mc.items()), list(match_codes.items()))
+        self.assertEqual(list(mc.values()), list(match_codes.values()))
         self.assertRaises(KeyError, mc.__getitem__, 'foo')
 
     def test_addition(self):
@@ -53,7 +53,7 @@ class TestMappedReads(TestCase):
         mr1['chr9'] = 7
         self.assertEqual(list(mr1.keys()), ['chr9'])
         self.assertEqual(mr1['chr9'], 7)
-        self.assertEqual(mr1.items(), [('chr9', 7)])
+        self.assertEqual(list(mr1.items()), [('chr9', 7)])
         del mr1['chr9']
         self.assertEqual(len(mr1), 0)
 
@@ -238,7 +238,7 @@ class ElandTests(TestCase):
         e.results[sl3] = 'Lane3'
         e.results[sl1] = 'Lane1'
 
-        e_list = e.values()
+        e_list = list(e.values())
         self.assertEqual(e_list[0], 'Lane1')
         self.assertEqual(e_list[1], 'Lane3')
         self.assertEqual(e_list[2], 'Lane5')
@@ -251,15 +251,15 @@ class TestElandMatches(TestCase):
         em.add('s_1_sequence.txt')
         self.assertEqual(len(em), 1)
         self.assertEqual(len(em[key]), 1)
-        filename = iter(em[key]).next().filename
+        filename = next(iter(em[key])).filename
         self.assertEqual(filename, 's_1_sequence.txt')
-        self.assertEqual(em.keys(), [key])
+        self.assertEqual(list(em.keys()), [key])
         em.add('s_1_eland_result.txt')
         self.assertEqual(len(em), 1)
         self.assertEqual(len(em[key]), 1)
-        filename = iter(em[key]).next().filename
+        filename = next(iter(em[key])).filename
         self.assertEqual(filename, 's_1_eland_result.txt')
-        self.assertEqual(em.keys(), [key])
+        self.assertEqual(list(em.keys()), [key])
 
     def test_parts(self):
         key11111 = SampleKey(1, 1, '11111')
index ffb63dc0ac05afe3c191a8d37e653a8dbe03c8af..4ec9d3e755a9ac99cbde2330237f69c70e76fd87 100644 (file)
@@ -81,22 +81,22 @@ class RetrieveTestCases(TestCase):
 
         output.seek(0)
         sheet = list(csv.DictReader(output))
-        name1 = library1.id + '_index' + library1.index_sequences().keys()[0]
-        name2 = library2.id + '_index' + library2.index_sequences().keys()[0]
+        name1 = library1.id + '_index' + next(iter(library1.index_sequences()))
+        name2 = library2.id + '_index' + next(iter(library2.index_sequences()))
         expected = [{'SampleProject': name1,
-                     'Index': library1.index_sequences().values()[0],
+                     'Index': next(iter(library1.index_sequences().values())),
                      'Lane': '1',
                      },
                     {'SampleProject': name2,
-                     'Index': library2.index_sequences().values()[0],
+                     'Index': next(iter(library2.index_sequences().values())),
                      'Lane': '1',
                      },
                     {'SampleProject': name1,
-                     'Index': library1.index_sequences().values()[0],
+                     'Index': next(iter(library1.index_sequences().values())),
                      'Lane': '2',
                      },
                     {'SampleProject': name2,
-                     'Index': library2.index_sequences().values()[0],
+                     'Index': next(iter(library2.index_sequences().values())),
                      'Lane': '2',
                      },
                     ]
index a755f6c0e39fe3161b105c24cd6d48ab77b36b47..a7902fed1899306d3c686816783f11185b7b18a7 100644 (file)
@@ -24,7 +24,7 @@ class TestResultMap(TestCase):
         results['2000'] = 'dir2000'
         results['1500'] = 'dir1500'
 
-        self.failUnlessEqual(results.keys(), ['1000', '2000', '1500'])
+        self.failUnlessEqual(list(results.keys()), ['1000', '2000', '1500'])
         self.failUnlessEqual(list(results.values()),
                              ['dir1000', 'dir2000', 'dir1500'])
         self.failUnlessEqual(list(results.items()),