]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
teuthology: use next(iter(..)) for accessing first element in a view
authorKefu Chai <kchai@redhat.com>
Fri, 3 Apr 2020 11:24:16 +0000 (19:24 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 3 Apr 2020 11:28:45 +0000 (19:28 +0800)
in python2, dict.values() and dict.keys() return lists. but in python3,
they return views, which cannot be indexed directly using an integer
index.

there are three use cases when we access these views in python3:

1. get the first element
2. get all the elements and then *might* want to access them by index
3. get the first element assuming there is only a single element in
   the view
4. iterate thru the view

in the 1st case, we cannot assume the number of elements, so to be
python3 compatible, we should use `next(iter(a_dict))` instead.

in the 2nd case, in this change, the view is materialized using
`list(a_dict)`.

in the 3rd case, we can just continue using the short hand of
```py
(first_element,) = a_dict.keys()
```
to unpack the view. this works in both python2 and python3.

in the 4th case, the existing code works in both python2 and python3, as
both list and view can be iterated using `iter`, and `len` works as
well.

Signed-off-by: Kefu Chai <kchai@redhat.com>
teuthology/lock/ops.py
teuthology/openstack/test/openstack-integration.py
teuthology/task/hadoop.py

index 8ba87299955ce43e8f433f26161efda2686be6eb..247ccfe667e206deb990d9400bfcbfc947c3a9f0 100644 (file)
@@ -143,7 +143,7 @@ def lock_many(ctx, num, machine_type, user=None, description=None,
                             p.spawn(teuthology.provision.reimage, ctx,
                                     machine, machine_type)
                             reimaged[machine] = machines[machine]
-                reimaged = do_update_keys(reimaged.keys())[1]
+                reimaged = do_update_keys(list(reimaged.keys()))[1]
                 update_nodes(reimaged)
                 return reimaged
             return machines
index 6ad49f48b1d05186dd9a880c82f872ffee5d3ef0..41dde59b223394624edc1926fc77225886d6ccbe 100644 (file)
@@ -264,7 +264,7 @@ class TestNuke(Integration):
                         '--machine-type', 'openstack']
 
     def test_nuke(self):
-        image = teuthology.openstack.OpenStack.image2url.keys()[0]
+        image = next(iter(teuthology.openstack.OpenStack.image2url.keys()))
 
         (os_type, os_version, arch) = image.split('-')
         args = scripts.lock.parse_args(self.options +
index 53f07d89bc13e529eb959409719383335bec5189..66949a3bbead1f450ffe4d87056bbb6df7f5b3c5 100644 (file)
@@ -171,7 +171,7 @@ def configure(ctx, config, hadoops):
         hadoop_dir = "{tdir}/hadoop/".format(tdir=testdir)
         masters = ctx.cluster.only(is_hadoop_type('master'))
         assert len(masters.remotes) == 1
-        master = masters.remotes.keys()[0]
+        master = next(iter(masters.remotes.keys()))
         master.run(
             args = [
                 hadoop_dir + "bin/hadoop",
@@ -340,7 +340,7 @@ def start_hadoop(ctx, config):
     hadoop_dir = "{tdir}/hadoop/".format(tdir=testdir)
     masters = ctx.cluster.only(is_hadoop_type('master'))
     assert len(masters.remotes) == 1
-    master = masters.remotes.keys()[0]
+    master = next(iter(masters.remotes.keys()))
 
     log.info("Stopping Hadoop daemons")
     master.run(