From b0b8afaf55ffdadf5ee329edd92b9cac1be8c2d1 Mon Sep 17 00:00:00 2001 From: Chuck Short Date: Sun, 6 Oct 2013 22:39:41 -0400 Subject: [PATCH] python3: Refactor dict for python2/python3 compat Python3 changed the behavior of dict.keys such that it is now returns a dict_keys object, which is iterable but not indexable. You can get the python2 result back with an explicit call to list. Refactor list(*.keys()) so that it just uses list(). Change-Id: Ib2e9646ac967e9bd7cc4f47e2099f5d1358808a9 Signed-off-by: Chuck Short --- cinderclient/base.py | 2 +- cinderclient/client.py | 2 +- cinderclient/exceptions.py | 2 +- cinderclient/shell.py | 2 +- cinderclient/tests/fakes.py | 5 ++--- cinderclient/tests/v1/fakes.py | 22 +++++++++++----------- cinderclient/tests/v2/fakes.py | 22 +++++++++++----------- cinderclient/v1/quota_classes.py | 2 +- cinderclient/v1/quotas.py | 2 +- cinderclient/v1/shell.py | 10 +++++----- cinderclient/v2/quota_classes.py | 2 +- cinderclient/v2/quotas.py | 2 +- cinderclient/v2/shell.py | 10 +++++----- tools/colorizer.py | 2 +- 14 files changed, 43 insertions(+), 44 deletions(-) diff --git a/cinderclient/base.py b/cinderclient/base.py index e1f44eb5a..73bdec603 100644 --- a/cinderclient/base.py +++ b/cinderclient/base.py @@ -270,7 +270,7 @@ class Resource(object): return self.__dict__[k] def __repr__(self): - reprkeys = sorted(k for k in list(self.__dict__.keys()) if k[0] != '_' + reprkeys = sorted(k for k in self.__dict__ if k[0] != '_' and k != 'manager') info = ", ".join("%s=%s" % (k, getattr(self, k)) for k in reprkeys) return "<%s %s>" % (self.__class__.__name__, info) diff --git a/cinderclient/client.py b/cinderclient/client.py index 4846e4ace..625066ca2 100644 --- a/cinderclient/client.py +++ b/cinderclient/client.py @@ -402,7 +402,7 @@ def get_client_class(version): client_path = version_map[str(version)] except (KeyError, ValueError): msg = "Invalid client version '%s'. must be one of: %s" % ( - (version, ', '.join(list(version_map.keys())))) + (version, ', '.join(version_map))) raise exceptions.UnsupportedVersion(msg) return utils.import_class(client_path) diff --git a/cinderclient/exceptions.py b/cinderclient/exceptions.py index b84eefd6b..1e3050c75 100644 --- a/cinderclient/exceptions.py +++ b/cinderclient/exceptions.py @@ -166,7 +166,7 @@ def from_response(response, body): message = "n/a" details = "n/a" if hasattr(body, 'keys'): - error = body[list(body.keys())[0]] + error = body[list(body)[0]] message = error.get('message', None) details = error.get('details', None) return cls(code=response.status_code, message=message, details=details, diff --git a/cinderclient/shell.py b/cinderclient/shell.py index c9c152908..54615bbe9 100644 --- a/cinderclient/shell.py +++ b/cinderclient/shell.py @@ -478,7 +478,7 @@ class OpenStackCinderShell(object): options = set() for sc_str, sc in list(self.subcommands.items()): commands.add(sc_str) - for option in list(sc._optionals._option_string_actions.keys()): + for option in sc._optionals._option_string_actions: options.add(option) commands.remove('bash-completion') diff --git a/cinderclient/tests/fakes.py b/cinderclient/tests/fakes.py index 7d803500f..5a3937c58 100644 --- a/cinderclient/tests/fakes.py +++ b/cinderclient/tests/fakes.py @@ -23,12 +23,11 @@ from __future__ import print_function def assert_has_keys(dict, required=[], optional=[]): - keys = list(dict.keys()) for k in required: try: - assert k in keys + assert k in dict except AssertionError: - extra_keys = set(keys).difference(set(required + optional)) + extra_keys = set(dict).difference(set(required + optional)) raise AssertionError("found unexpected keys: %s" % list(extra_keys)) diff --git a/cinderclient/tests/v1/fakes.py b/cinderclient/tests/v1/fakes.py index 87c9b393f..cea11a37d 100644 --- a/cinderclient/tests/v1/fakes.py +++ b/cinderclient/tests/v1/fakes.py @@ -268,8 +268,8 @@ class FakeHTTPClient(base_client.HTTPClient): def post_snapshots_1234_action(self, body, **kw): _body = None resp = 202 - assert len(body.keys()) == 1 - action = body.keys()[0] + assert len(list(body)) == 1 + action = list(body)[0] if action == 'os-reset_status': assert 'status' in body['os-reset_status'] elif action == 'os-update_snapshot_status': @@ -313,10 +313,10 @@ class FakeHTTPClient(base_client.HTTPClient): def post_volumes_1234_action(self, body, **kw): _body = None resp = 202 - assert len(list(body.keys())) == 1 - action = list(body.keys())[0] + assert len(list(body)) == 1 + action = list(body)[0] if action == 'os-attach': - assert list(body[action].keys()) == ['instance_uuid', 'mountpoint'] + assert list(body[action]) == ['instance_uuid', 'mountpoint'] elif action == 'os-detach': assert body[action] is None elif action == 'os-reserve': @@ -324,10 +324,10 @@ class FakeHTTPClient(base_client.HTTPClient): elif action == 'os-unreserve': assert body[action] is None elif action == 'os-initialize_connection': - assert list(body[action].keys()) == ['connector'] + assert list(body[action]) == ['connector'] return (202, {}, {'connection_info': 'foos'}) elif action == 'os-terminate_connection': - assert list(body[action].keys()) == ['connector'] + assert list(body[action]) == ['connector'] elif action == 'os-begin_detaching': assert body[action] is None elif action == 'os-roll_detaching': @@ -335,7 +335,7 @@ class FakeHTTPClient(base_client.HTTPClient): elif action == 'os-reset_status': assert 'status' in body[action] elif action == 'os-extend': - assert body[action].keys() == ['new_size'] + assert list(body[action]) == ['new_size'] elif action == 'os-migrate_volume': assert 'host' in body[action] assert 'force_host_copy' in body[action] @@ -370,7 +370,7 @@ class FakeHTTPClient(base_client.HTTPClient): 'gigabytes': 1}}) def put_os_quota_sets_test(self, body, **kw): - assert list(body.keys()) == ['quota_set'] + assert list(body) == ['quota_set'] fakes.assert_has_keys(body['quota_set'], required=['tenant_id']) return (200, {}, {'quota_set': { @@ -393,7 +393,7 @@ class FakeHTTPClient(base_client.HTTPClient): 'gigabytes': 1}}) def put_os_quota_class_sets_test(self, body, **kw): - assert list(body.keys()) == ['quota_class_set'] + assert list(body) == ['quota_class_set'] fakes.assert_has_keys(body['quota_class_set'], required=['class_name']) return (200, {}, {'quota_class_set': { @@ -431,7 +431,7 @@ class FakeHTTPClient(base_client.HTTPClient): 'extra_specs': {}}}) def post_types_1_extra_specs(self, body, **kw): - assert list(body.keys()) == ['extra_specs'] + assert list(body) == ['extra_specs'] return (200, {}, {'extra_specs': {'k': 'v'}}) def delete_types_1_extra_specs_k(self, **kw): diff --git a/cinderclient/tests/v2/fakes.py b/cinderclient/tests/v2/fakes.py index d0574566c..f8485126d 100644 --- a/cinderclient/tests/v2/fakes.py +++ b/cinderclient/tests/v2/fakes.py @@ -275,8 +275,8 @@ class FakeHTTPClient(base_client.HTTPClient): def post_snapshots_1234_action(self, body, **kw): _body = None resp = 202 - assert len(body.keys()) == 1 - action = body.keys()[0] + assert len(list(body)) == 1 + action = list(body)[0] if action == 'os-reset_status': assert 'status' in body['os-reset_status'] elif action == 'os-update_snapshot_status': @@ -320,10 +320,10 @@ class FakeHTTPClient(base_client.HTTPClient): def post_volumes_1234_action(self, body, **kw): _body = None resp = 202 - assert len(list(body.keys())) == 1 - action = list(body.keys())[0] + assert len(list(body)) == 1 + action = list(body)[0] if action == 'os-attach': - assert list(body[action].keys()) == ['instance_uuid', 'mountpoint'] + assert list(body[action]) == ['instance_uuid', 'mountpoint'] elif action == 'os-detach': assert body[action] is None elif action == 'os-reserve': @@ -331,10 +331,10 @@ class FakeHTTPClient(base_client.HTTPClient): elif action == 'os-unreserve': assert body[action] is None elif action == 'os-initialize_connection': - assert list(body[action].keys()) == ['connector'] + assert list(body[action]) == ['connector'] return (202, {}, {'connection_info': 'foos'}) elif action == 'os-terminate_connection': - assert list(body[action].keys()) == ['connector'] + assert list(body[action]) == ['connector'] elif action == 'os-begin_detaching': assert body[action] is None elif action == 'os-roll_detaching': @@ -342,7 +342,7 @@ class FakeHTTPClient(base_client.HTTPClient): elif action == 'os-reset_status': assert 'status' in body[action] elif action == 'os-extend': - assert body[action].keys() == ['new_size'] + assert list(body[action]) == ['new_size'] elif action == 'os-migrate_volume': assert 'host' in body[action] assert 'force_host_copy' in body[action] @@ -377,7 +377,7 @@ class FakeHTTPClient(base_client.HTTPClient): 'gigabytes': 1}}) def put_os_quota_sets_test(self, body, **kw): - assert list(body.keys()) == ['quota_set'] + assert list(body) == ['quota_set'] fakes.assert_has_keys(body['quota_set'], required=['tenant_id']) return (200, {}, {'quota_set': { @@ -400,7 +400,7 @@ class FakeHTTPClient(base_client.HTTPClient): 'gigabytes': 1}}) def put_os_quota_class_sets_test(self, body, **kw): - assert list(body.keys()) == ['quota_class_set'] + assert list(body) == ['quota_class_set'] fakes.assert_has_keys(body['quota_class_set'], required=['class_name']) return (200, {}, {'quota_class_set': { @@ -438,7 +438,7 @@ class FakeHTTPClient(base_client.HTTPClient): 'extra_specs': {}}}) def post_types_1_extra_specs(self, body, **kw): - assert list(body.keys()) == ['extra_specs'] + assert list(body) == ['extra_specs'] return (200, {}, {'extra_specs': {'k': 'v'}}) def delete_types_1_extra_specs_k(self, **kw): diff --git a/cinderclient/v1/quota_classes.py b/cinderclient/v1/quota_classes.py index 1f880fbf8..9e81e2cca 100644 --- a/cinderclient/v1/quota_classes.py +++ b/cinderclient/v1/quota_classes.py @@ -39,7 +39,7 @@ class QuotaClassSetManager(base.Manager): def update(self, class_name, **updates): body = {'quota_class_set': {'class_name': class_name}} - for update in updates.keys(): + for update in updates: body['quota_class_set'][update] = updates[update] self._update('/os-quota-class-sets/%s' % (class_name), body) diff --git a/cinderclient/v1/quotas.py b/cinderclient/v1/quotas.py index a0028a991..87d34056c 100644 --- a/cinderclient/v1/quotas.py +++ b/cinderclient/v1/quotas.py @@ -40,7 +40,7 @@ class QuotaSetManager(base.Manager): def update(self, tenant_id, **updates): body = {'quota_set': {'tenant_id': tenant_id}} - for update in updates.keys(): + for update in updates: body['quota_set'][update] = updates[update] self._update('/os-quota-sets/%s' % (tenant_id), body) diff --git a/cinderclient/v1/shell.py b/cinderclient/v1/shell.py index b11782477..74f86626a 100644 --- a/cinderclient/v1/shell.py +++ b/cinderclient/v1/shell.py @@ -95,7 +95,7 @@ def _print_volume_image(image): def _translate_keys(collection, convert): for item in collection: - keys = list(item.__dict__.keys()) + keys = item.__dict__ for from_key, to_key in convert: if from_key in keys and to_key not in keys: setattr(item, to_key, item._info[from_key]) @@ -352,7 +352,7 @@ def do_metadata(cs, args): if args.action == 'set': cs.volumes.set_metadata(volume, metadata) elif args.action == 'unset': - cs.volumes.delete_metadata(volume, list(metadata.keys())) + cs.volumes.delete_metadata(volume, list(metadata)) @utils.arg( @@ -560,7 +560,7 @@ def do_type_key(cs, args): if args.action == 'set': vtype.set_keys(keypair) elif args.action == 'unset': - vtype.unset_keys(list(keypair.keys())) + vtype.unset_keys(list(keypair)) def do_endpoints(cs, args): @@ -582,7 +582,7 @@ _quota_resources = ['volumes', 'snapshots', 'gigabytes'] def _quota_show(quotas): quota_dict = {} - for resource in quotas._info.keys(): + for resource in quotas._info: good_name = False for name in _quota_resources: if resource.startswith(name): @@ -1198,7 +1198,7 @@ def do_qos_key(cs, args): if args.action == 'set': cs.qos_specs.set_keys(args.qos_specs, keypair) elif args.action == 'unset': - cs.qos_specs.unset_keys(args.qos_specs, list(keypair.keys())) + cs.qos_specs.unset_keys(args.qos_specs, list(keypair)) @utils.arg('qos_specs', metavar='', diff --git a/cinderclient/v2/quota_classes.py b/cinderclient/v2/quota_classes.py index 4e4491470..bf80db0f6 100644 --- a/cinderclient/v2/quota_classes.py +++ b/cinderclient/v2/quota_classes.py @@ -37,7 +37,7 @@ class QuotaClassSetManager(base.Manager): def update(self, class_name, **updates): body = {'quota_class_set': {'class_name': class_name}} - for update in updates.keys(): + for update in updates: body['quota_class_set'][update] = updates[update] self._update('/os-quota-class-sets/%s' % (class_name), body) diff --git a/cinderclient/v2/quotas.py b/cinderclient/v2/quotas.py index 09722104f..73844604b 100644 --- a/cinderclient/v2/quotas.py +++ b/cinderclient/v2/quotas.py @@ -38,7 +38,7 @@ class QuotaSetManager(base.Manager): def update(self, tenant_id, **updates): body = {'quota_set': {'tenant_id': tenant_id}} - for update in updates.keys(): + for update in updates: body['quota_set'][update] = updates[update] self._update('/os-quota-sets/%s' % (tenant_id), body) diff --git a/cinderclient/v2/shell.py b/cinderclient/v2/shell.py index 18a6bbb70..6ce4e8778 100644 --- a/cinderclient/v2/shell.py +++ b/cinderclient/v2/shell.py @@ -89,7 +89,7 @@ def _print_volume_image(image): def _translate_keys(collection, convert): for item in collection: - keys = list(item.__dict__.keys()) + keys = item.__dict__ for from_key, to_key in convert: if from_key in keys and to_key not in keys: setattr(item, to_key, item._info[from_key]) @@ -390,7 +390,7 @@ def do_metadata(cs, args): if args.action == 'set': cs.volumes.set_metadata(volume, metadata) elif args.action == 'unset': - cs.volumes.delete_metadata(volume, list(metadata.keys())) + cs.volumes.delete_metadata(volume, list(metadata)) @utils.arg('--all-tenants', @@ -616,7 +616,7 @@ def do_type_key(cs, args): if args.action == 'set': vtype.set_keys(keypair) elif args.action == 'unset': - vtype.unset_keys(list(keypair.keys())) + vtype.unset_keys(list(keypair)) def do_endpoints(cs, args): @@ -638,7 +638,7 @@ _quota_resources = ['volumes', 'snapshots', 'gigabytes'] def _quota_show(quotas): quota_dict = {} - for resource in quotas._info.keys(): + for resource in quotas._info: good_name = False for name in _quota_resources: if resource.startswith(name): @@ -1273,7 +1273,7 @@ def do_qos_key(cs, args): if args.action == 'set': cs.qos_specs.set_keys(args.qos_specs, keypair) elif args.action == 'unset': - cs.qos_specs.unset_keys(args.qos_specs, list(keypair.keys())) + cs.qos_specs.unset_keys(args.qos_specs, list(keypair)) @utils.arg('qos_specs', metavar='', diff --git a/tools/colorizer.py b/tools/colorizer.py index 1b6e57638..2a079fc33 100755 --- a/tools/colorizer.py +++ b/tools/colorizer.py @@ -267,7 +267,7 @@ class NovaTestResult(testtools.TestResult): if not self.last_written or (self._now() - time).total_seconds() > 2.0: diff = 3.0 while diff > 2.0: - classes =list(self.results.keys()) + classes =list(self.results) oldest = min(classes, key=lambda x: self.last_time[x]) diff = (self._now() - self.last_time[oldest]).total_seconds() self.writeTestCase(oldest)