Leverage dict comprehension in PEP-0274

PEP-0274 introduced dict comprehensions to replace dict constructor
with a sqeuence of key-pairs[1], these are benefits:
  First, it makes the code look neater.
  Second, it gains a micro-optimization.

Cinder dropped python 2.6 support in Kilo, we can leverage this now.
Note: This commit doesn't handle dict constructor with kwargs.
This commit also adds a hacking rule.

[1]http://legacy.python.org/dev/peps/pep-0274/

Change-Id: Ie65796438160b1aa1f47c8519fb9943e81bff3e9
This commit is contained in:
ChangBo Guo(gcb)
2014-12-24 22:42:58 +08:00
parent c2db466995
commit 5800f25f7e
19 changed files with 86 additions and 57 deletions

View File

@@ -470,8 +470,8 @@ def _dict_with_extra_specs(inst_type_query):
'extra_specs' : {'k1': 'v1'}
"""
inst_type_dict = dict(inst_type_query)
extra_specs = dict([(x['key'], x['value'])
for x in inst_type_query['extra_specs']])
extra_specs = {x['key']: x['value']
for x in inst_type_query['extra_specs']}
inst_type_dict['extra_specs'] = extra_specs
return inst_type_dict
@@ -742,7 +742,7 @@ def _get_quota_usages(context, session, project_id):
filter_by(project_id=project_id).\
with_lockmode('update').\
all()
return dict((row.resource, row) for row in rows)
return {row.resource: row for row in rows}
@require_context
@@ -877,8 +877,8 @@ def quota_reserve(context, resources, quotas, deltas, expire,
LOG.warning(_LW("Change will make usage less than 0 for the following "
"resources: %s"), unders)
if overs:
usages = dict((k, dict(in_use=v['in_use'], reserved=v['reserved']))
for k, v in usages.items())
usages = {k: dict(in_use=v['in_use'], reserved=v['reserved'])
for k, v in usages.items()}
raise exception.OverQuota(overs=sorted(overs), quotas=quotas,
usages=usages)