Skip to content

Commit

Permalink
Merge pull request #2386 from dhermes/use-dict-comprehensions
Browse files Browse the repository at this point in the history
Using dictionary comprehensions in place of dict().
  • Loading branch information
dhermes authored Sep 22, 2016
2 parents 6b5223d + 864affe commit 9e23e1c
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 49 deletions.
5 changes: 3 additions & 2 deletions google/cloud/dns/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions google/cloud/logging/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,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):
Expand Down
4 changes: 2 additions & 2 deletions google/cloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion system_tests/logging_.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,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)
Expand Down
2 changes: 1 addition & 1 deletion unit_tests/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions unit_tests/dns/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions unit_tests/logging/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions unit_tests/monitoring/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion unit_tests/monitoring/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
60 changes: 30 additions & 30 deletions unit_tests/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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')
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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')
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9e23e1c

Please sign in to comment.