]> git.apps.os.sepia.ceph.com Git - ceph.git/commit
qa/tasks: use next(iter(..)) for accessing first element in a view
authorKefu Chai <kchai@redhat.com>
Tue, 31 Mar 2020 02:16:40 +0000 (10:16 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 2 Jun 2020 02:32:23 +0000 (10:32 +0800)
commit723f63edb1cf45eba653c2f6836ed0ceb4dfb6f4
tree307274c5f47b3019b15fb60bc290756fc81c1532
parentf3871da1792bd610f782655651596c4ed3441b86
qa/tasks: use next(iter(..)) for accessing first element in a view

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>
(cherry picked from commit d7258ea7fdcd81d4a88028a25b32ed5b278d0752)

Conflicts:
qa/tasks/barbican.py
qa/tasks/cbt.py
qa/tasks/vault.py: trivial resolutions
qa/tasks/cbt.py
qa/tasks/ceph_manager.py
qa/tasks/ceph_objectstore_tool.py
qa/tasks/cephfs/filesystem.py
qa/tasks/check_counter.py
qa/tasks/devstack.py
qa/tasks/dnsmasq.py
qa/tasks/filestore_idempotent.py
qa/tasks/mgr/test_crash.py
qa/tasks/radosgw_admin.py
qa/tasks/radosgw_admin_rest.py