From 864affe9699d65c9a4d745e9985086ca77725832 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Thu, 22 Sep 2016 09:55:19 -0700 Subject: [PATCH] Using dictionary comprehensions in place of dict(). This is now possible after dropping support for Python 2.6. --- google/cloud/dns/client.py | 5 ++- google/cloud/logging/_gax.py | 4 +- google/cloud/storage/_helpers.py | 4 +- google/cloud/storage/bucket.py | 3 +- system_tests/logging_.py | 2 +- unit_tests/_testing.py | 2 +- unit_tests/dns/test_client.py | 8 ++-- unit_tests/logging/test__gax.py | 5 ++- unit_tests/monitoring/test_group.py | 4 +- unit_tests/monitoring/test_metric.py | 3 +- unit_tests/storage/test_blob.py | 60 ++++++++++++++-------------- 11 files changed, 51 insertions(+), 49 deletions(-) diff --git a/google/cloud/dns/client.py b/google/cloud/dns/client.py index 4058890b3397..05b67a279f4c 100644 --- a/google/cloud/dns/client.py +++ b/google/cloud/dns/client.py @@ -56,8 +56,9 @@ def quotas(self): path = '/projects/%s' % (self.project,) resp = self.connection.api_request(method='GET', path=path) - return dict([(key, int(value)) - for key, value in resp['quota'].items() if key != 'kind']) + return {key: int(value) + for key, value in resp['quota'].items() + if key != 'kind'} def list_zones(self, max_results=None, page_token=None): """List zones for the project associated with this client. diff --git a/google/cloud/logging/_gax.py b/google/cloud/logging/_gax.py index f84ef53b6f42..9edd976dc097 100644 --- a/google/cloud/logging/_gax.py +++ b/google/cloud/logging/_gax.py @@ -472,8 +472,8 @@ def _struct_pb_to_mapping(struct_pb): Performs "impedance matching" between the protobuf attrs and the keys expected in the JSON API. """ - return dict([(key, _value_pb_to_value(struct_pb.fields[key])) - for key in struct_pb.fields]) + return {key: _value_pb_to_value(struct_pb.fields[key]) + for key in struct_pb.fields} def _log_entry_pb_to_mapping(entry_pb): diff --git a/google/cloud/storage/_helpers.py b/google/cloud/storage/_helpers.py index 73f690959490..6988cb02abe8 100644 --- a/google/cloud/storage/_helpers.py +++ b/google/cloud/storage/_helpers.py @@ -120,8 +120,8 @@ def patch(self, client=None): client = self._require_client(client) # Pass '?projection=full' here because 'PATCH' documented not # to work properly w/ 'noAcl'. - update_properties = dict((key, self._properties[key]) - for key in self._changes) + update_properties = {key: self._properties[key] + for key in self._changes} api_response = client.connection.api_request( method='PATCH', path=self.path, data=update_properties, query_params={'projection': 'full'}, _target_object=self) diff --git a/google/cloud/storage/bucket.py b/google/cloud/storage/bucket.py index f9b0ae73f6f0..def628edd485 100644 --- a/google/cloud/storage/bucket.py +++ b/google/cloud/storage/bucket.py @@ -166,8 +166,7 @@ def create(self, client=None): """ client = self._require_client(client) query_params = {'project': client.project} - properties = dict( - (key, self._properties[key]) for key in self._changes) + properties = {key: self._properties[key] for key in self._changes} properties['name'] = self.name api_response = client.connection.api_request( method='POST', path='/b', query_params=query_params, diff --git a/system_tests/logging_.py b/system_tests/logging_.py index 5292c0afce64..40bfcf3f44d1 100644 --- a/system_tests/logging_.py +++ b/system_tests/logging_.py @@ -280,7 +280,7 @@ def test_update_metric(self): metric.description = NEW_DESCRIPTION metric.update() after_metrics, _ = Config.CLIENT.list_metrics() - after_info = dict((metric.name, metric) for metric in after_metrics) + after_info = {metric.name: metric for metric in after_metrics} after = after_info[METRIC_NAME] self.assertEqual(after.filter_, NEW_FILTER) self.assertEqual(after.description, NEW_DESCRIPTION) diff --git a/unit_tests/_testing.py b/unit_tests/_testing.py index f181b7c94a6b..bc1f952a0ad9 100644 --- a/unit_tests/_testing.py +++ b/unit_tests/_testing.py @@ -22,7 +22,7 @@ def __init__(self, module, **kw): self.module = module if len(kw) == 0: # pragma: NO COVER raise ValueError('_Monkey was used with nothing to monkey-patch') - self.to_restore = dict([(key, getattr(module, key)) for key in kw]) + self.to_restore = {key: getattr(module, key) for key in kw} for key, value in kw.items(): setattr(module, key, value) diff --git a/unit_tests/dns/test_client.py b/unit_tests/dns/test_client.py index f7c754fb5382..d737d3540960 100644 --- a/unit_tests/dns/test_client.py +++ b/unit_tests/dns/test_client.py @@ -55,8 +55,8 @@ def test_quotas_defaults(self): 'totalRrdataSizePerChange': str(TOTAL_SIZE), } } - CONVERTED = dict([(key, int(value)) - for key, value in DATA['quota'].items()]) + CONVERTED = {key: int(value) + for key, value in DATA['quota'].items()} creds = _Credentials() client = self._makeOne(self.PROJECT, creds) conn = client.connection = _Connection(DATA) @@ -88,8 +88,8 @@ def test_quotas_w_kind_key(self): 'totalRrdataSizePerChange': str(TOTAL_SIZE), } } - CONVERTED = dict([(key, int(value)) - for key, value in DATA['quota'].items()]) + CONVERTED = {key: int(value) + for key, value in DATA['quota'].items()} WITH_KIND = {'quota': DATA['quota'].copy()} WITH_KIND['quota']['kind'] = 'dns#quota' creds = _Credentials() diff --git a/unit_tests/logging/test__gax.py b/unit_tests/logging/test__gax.py index c27068c2d770..4fcd22f267d8 100644 --- a/unit_tests/logging/test__gax.py +++ b/unit_tests/logging/test__gax.py @@ -87,8 +87,9 @@ def test_list_entries_with_paging(self): TOKEN = 'TOKEN' NEW_TOKEN = 'NEW_TOKEN' PAYLOAD = {'message': 'MESSAGE', 'weather': 'sunny'} - struct_pb = _StructPB(dict([(key, Value(string_value=value)) - for key, value in PAYLOAD.items()])) + struct_pb = _StructPB({ + key: Value(string_value=value) for key, value in PAYLOAD.items() + }) response = _GAXPageIterator( [_LogEntryPB(self.LOG_NAME, json_payload=struct_pb)], NEW_TOKEN) gax_api = _GAXLoggingAPI(_list_log_entries_response=response) diff --git a/unit_tests/monitoring/test_group.py b/unit_tests/monitoring/test_group.py index aa02adf75ee1..5a26b102e73a 100644 --- a/unit_tests/monitoring/test_group.py +++ b/unit_tests/monitoring/test_group.py @@ -55,7 +55,7 @@ def setUp(self): self.PARENT_NAME = self.PATH + self.PARENT_ID FILTER_TEMPLATE = 'resource.metadata.tag."color"="%s"' - self.FILTER = FILTER_TEMPLATE % 'blue' + self.FILTER = FILTER_TEMPLATE % ('blue',) self.JSON_GROUP = { 'name': self.GROUP_NAME, @@ -227,7 +227,7 @@ def test_to_dict_defaults(self): def test_create(self): RESPONSE = self.JSON_GROUP - REQUEST = dict(RESPONSE) + REQUEST = RESPONSE.copy() REQUEST.pop('name') connection = _Connection(RESPONSE) diff --git a/unit_tests/monitoring/test_metric.py b/unit_tests/monitoring/test_metric.py index c6e6a5d8bfd6..76f61c802577 100644 --- a/unit_tests/monitoring/test_metric.py +++ b/unit_tests/monitoring/test_metric.py @@ -254,7 +254,8 @@ def test_create(self): 'valueType': VALUE_TYPE, 'description': DESCRIPTION, } - RESPONSE = dict(REQUEST, name=NAME) + RESPONSE = REQUEST.copy() + RESPONSE['name'] = NAME connection = _Connection(RESPONSE) client = _Client(project=PROJECT, connection=connection) diff --git a/unit_tests/storage/test_blob.py b/unit_tests/storage/test_blob.py index 001ae026851f..18aabbe42922 100644 --- a/unit_tests/storage/test_blob.py +++ b/unit_tests/storage/test_blob.py @@ -419,8 +419,8 @@ def test_download_to_filename_w_key(self): updatedTime = time.mktime(blob.updated.timetuple()) rq = connection.http._requested - headers = dict( - [(x.title(), str(y)) for x, y in rq[0]['headers'].items()]) + headers = { + x.title(): str(y) for x, y in rq[0]['headers'].items()} self.assertEqual(headers['X-Goog-Encryption-Algorithm'], 'AES256') self.assertEqual(headers['X-Goog-Encryption-Key'], HEADER_KEY_VALUE) self.assertEqual(headers['X-Goog-Encryption-Key-Sha256'], @@ -502,8 +502,8 @@ def _upload_from_file_simple_test_helper(self, properties=None, self.assertEqual(path, '/b/name/o') self.assertEqual(dict(parse_qsl(qs)), {'uploadType': 'media', 'name': BLOB_NAME}) - headers = dict( - [(x.title(), str(y)) for x, y in rq[0]['headers'].items()]) + headers = { + x.title(): str(y) for x, y in rq[0]['headers'].items()} self.assertEqual(headers['Content-Length'], '6') self.assertEqual(headers['Content-Type'], expected_content_type) @@ -558,8 +558,8 @@ def fileno_mock(): self.assertEqual(len(rq), 3) # Requested[0] - headers = dict( - [(x.title(), str(y)) for x, y in rq[0].pop('headers').items()]) + headers = { + x.title(): str(y) for x, y in rq[0].pop('headers').items()} self.assertEqual(headers['Content-Length'], '0') self.assertEqual(headers['X-Upload-Content-Type'], 'application/octet-stream') @@ -579,8 +579,8 @@ def fileno_mock(): }) # Requested[1] - headers = dict( - [(x.title(), str(y)) for x, y in rq[1].pop('headers').items()]) + headers = { + x.title(): str(y) for x, y in rq[1].pop('headers').items()} self.assertEqual(headers['Content-Range'], 'bytes 0-4/*') self.assertEqual(rq[1], { 'method': 'PUT', @@ -591,8 +591,8 @@ def fileno_mock(): }) # Requested[2] - headers = dict( - [(x.title(), str(y)) for x, y in rq[2].pop('headers').items()]) + headers = { + x.title(): str(y) for x, y in rq[2].pop('headers').items()} self.assertEqual(headers['Content-Range'], 'bytes */5') self.assertEqual(rq[2], { 'method': 'PUT', @@ -677,8 +677,8 @@ def test_upload_from_file_resumable(self): self.assertEqual(len(rq), 3) # Requested[0] - headers = dict( - [(x.title(), str(y)) for x, y in rq[0].pop('headers').items()]) + headers = { + x.title(): str(y) for x, y in rq[0].pop('headers').items()} self.assertEqual(headers['X-Upload-Content-Length'], '6') self.assertEqual(headers['X-Upload-Content-Type'], 'application/octet-stream') @@ -698,8 +698,8 @@ def test_upload_from_file_resumable(self): }) # Requested[1] - headers = dict( - [(x.title(), str(y)) for x, y in rq[1].pop('headers').items()]) + headers = { + x.title(): str(y) for x, y in rq[1].pop('headers').items()} self.assertEqual(headers['Content-Range'], 'bytes 0-4/6') self.assertEqual(rq[1], { 'method': 'PUT', @@ -710,8 +710,8 @@ def test_upload_from_file_resumable(self): }) # Requested[2] - headers = dict( - [(x.title(), str(y)) for x, y in rq[2].pop('headers').items()]) + headers = { + x.title(): str(y) for x, y in rq[2].pop('headers').items()} self.assertEqual(headers['Content-Range'], 'bytes 5-5/6') self.assertEqual(rq[2], { 'method': 'PUT', @@ -755,8 +755,8 @@ def test_upload_from_file_resumable_w_error(self): self.assertEqual(len(rq), 1) # Requested[0] - headers = dict( - [(x.title(), str(y)) for x, y in rq[0].pop('headers').items()]) + headers = { + x.title(): str(y) for x, y in rq[0].pop('headers').items()} self.assertEqual(headers['X-Upload-Content-Length'], '6') self.assertEqual(headers['X-Upload-Content-Type'], 'application/octet-stream') @@ -820,8 +820,8 @@ def test_upload_from_file_w_slash_in_name(self): self.assertEqual(path, '/b/name/o') self.assertEqual(dict(parse_qsl(qs)), {'uploadType': 'media', 'name': 'parent/child'}) - headers = dict( - [(x.title(), str(y)) for x, y in rq[0]['headers'].items()]) + headers = { + x.title(): str(y) for x, y in rq[0]['headers'].items()} self.assertEqual(headers['Content-Length'], '6') self.assertEqual(headers['Content-Type'], 'application/octet-stream') @@ -873,8 +873,8 @@ def test_upload_from_filename_w_key(self): self.assertEqual(path, '/b/name/o') self.assertEqual(dict(parse_qsl(qs)), {'uploadType': 'media', 'name': BLOB_NAME}) - headers = dict( - [(x.title(), str(y)) for x, y in rq[0]['headers'].items()]) + headers = { + x.title(): str(y) for x, y in rq[0]['headers'].items()} self.assertEqual(headers['X-Goog-Encryption-Algorithm'], 'AES256') self.assertEqual(headers['X-Goog-Encryption-Key'], HEADER_KEY_VALUE) self.assertEqual(headers['X-Goog-Encryption-Key-Sha256'], @@ -926,8 +926,8 @@ def _upload_from_filename_test_helper(self, properties=None, self.assertEqual(path, '/b/name/o') self.assertEqual(dict(parse_qsl(qs)), {'uploadType': 'media', 'name': BLOB_NAME}) - headers = dict( - [(x.title(), str(y)) for x, y in rq[0]['headers'].items()]) + headers = { + x.title(): str(y) for x, y in rq[0]['headers'].items()} self.assertEqual(headers['Content-Length'], '6') self.assertEqual(headers['Content-Type'], expected_content_type) @@ -988,8 +988,8 @@ def test_upload_from_string_w_bytes(self): self.assertEqual(path, '/b/name/o') self.assertEqual(dict(parse_qsl(qs)), {'uploadType': 'media', 'name': BLOB_NAME}) - headers = dict( - [(x.title(), str(y)) for x, y in rq[0]['headers'].items()]) + headers = { + x.title(): str(y) for x, y in rq[0]['headers'].items()} self.assertEqual(headers['Content-Length'], '6') self.assertEqual(headers['Content-Type'], 'text/plain') self.assertEqual(rq[0]['body'], DATA) @@ -1028,8 +1028,8 @@ def test_upload_from_string_w_text(self): self.assertEqual(path, '/b/name/o') self.assertEqual(dict(parse_qsl(qs)), {'uploadType': 'media', 'name': BLOB_NAME}) - headers = dict( - [(x.title(), str(y)) for x, y in rq[0]['headers'].items()]) + headers = { + x.title(): str(y) for x, y in rq[0]['headers'].items()} self.assertEqual(headers['Content-Length'], str(len(ENCODED))) self.assertEqual(headers['Content-Type'], 'text/plain') self.assertEqual(rq[0]['body'], ENCODED) @@ -1071,8 +1071,8 @@ def test_upload_from_string_text_w_key(self): self.assertEqual(path, '/b/name/o') self.assertEqual(dict(parse_qsl(qs)), {'uploadType': 'media', 'name': BLOB_NAME}) - headers = dict( - [(x.title(), str(y)) for x, y in rq[0]['headers'].items()]) + headers = { + x.title(): str(y) for x, y in rq[0]['headers'].items()} self.assertEqual(headers['X-Goog-Encryption-Algorithm'], 'AES256') self.assertEqual(headers['X-Goog-Encryption-Key'], HEADER_KEY_VALUE)