Use "if dict.get(key):" instead "if key in dict and dict[key]:"

Use "if dict.get(key):" instead of "if key in dict and dict[key]:"
which makes code more clear and intelligible. Note this patch doesn't
change judging conditions, all "is not None" are retained.

Change-Id: Ieed57a21eb4b08c6f9a25b180a3625154a0d5fde
This commit is contained in:
Wei Wang 2014-08-07 16:16:37 +08:00 committed by armando-migliaccio
parent 0faafa17f0
commit e2066f34e1
8 changed files with 11 additions and 13 deletions

View File

@ -1233,7 +1233,7 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
s['id'] = db_subnet.id
self._validate_subnet(context, s, cur_subnet=db_subnet)
if 'gateway_ip' in s and s['gateway_ip'] is not None:
if s.get('gateway_ip') is not None:
allocation_pools = [{'start': p['first_ip'], 'end': p['last_ip']}
for p in db_subnet.allocation_pools]
self._validate_gw_out_of_pools(s["gateway_ip"], allocation_pools)

View File

@ -730,7 +730,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase):
raise n_exc.BadRequest(resource='floatingip', msg=msg)
internal_subnet_id = None
if 'fixed_ip_address' in fip and fip['fixed_ip_address']:
if fip.get('fixed_ip_address'):
internal_ip_address = fip['fixed_ip_address']
for ip in internal_port['fixed_ips']:
if ip['ip_address'] == internal_ip_address:
@ -775,11 +775,10 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase):
def _check_and_get_fip_assoc(self, context, fip, floatingip_db):
port_id = internal_ip_address = router_id = None
if (('fixed_ip_address' in fip and fip['fixed_ip_address']) and
not ('port_id' in fip and fip['port_id'])):
if fip.get('fixed_ip_address') and not fip.get('port_id'):
msg = _("fixed_ip_address cannot be specified without a port_id")
raise n_exc.BadRequest(resource='floatingip', msg=msg)
if 'port_id' in fip and fip['port_id']:
if fip.get('port_id'):
port_id, internal_ip_address, router_id = self.get_assoc_data(
context,
fip,

View File

@ -1823,12 +1823,11 @@ class NsxPluginV2(addr_pair_db.AllowedAddressPairsMixin,
ips_to_remove=nsx_floating_ips)
def _get_fip_assoc_data(self, context, fip, floatingip_db):
if (('fixed_ip_address' in fip and fip['fixed_ip_address']) and
not ('port_id' in fip and fip['port_id'])):
if fip.get('fixed_ip_address') and not fip.get('port_id'):
msg = _("fixed_ip_address cannot be specified without a port_id")
raise n_exc.BadRequest(resource='floatingip', msg=msg)
port_id = internal_ip = router_id = None
if 'port_id' in fip and fip['port_id']:
if fip.get('port_id'):
fip_qry = context.session.query(l3_db.FloatingIP)
port_id, internal_ip, router_id = self.get_assoc_data(
context,

View File

@ -141,7 +141,7 @@ class FakeNuageClient(object):
return result
def get_nuage_port_by_id(self, params):
if 'nuage_fip_id' in params and params['nuage_fip_id'] == '1':
if params.get('nuage_fip_id') == '1':
domain_id = uuidutils.generate_uuid()
else:
if 'nuage_router_id' in params:

View File

@ -306,7 +306,7 @@ class NeutronDbPluginV2TestCase(testlib_api.WebTestCase,
'dns_nameservers', 'host_routes',
'shared', 'ipv6_ra_mode', 'ipv6_address_mode'):
# Arg must be present and not null (but can be false)
if arg in kwargs and kwargs[arg] is not None:
if kwargs.get(arg) is not None:
data['subnet'][arg] = kwargs[arg]
if ('gateway_ip' in kwargs and

View File

@ -337,7 +337,7 @@ class L3NatTestCaseMixin(object):
data['router']['admin_state_up'] = admin_state_up
for arg in (('admin_state_up', 'tenant_id') + (arg_list or ())):
# Arg must be present and not empty
if arg in kwargs and kwargs[arg]:
if kwargs.get(arg):
data['router'][arg] = kwargs[arg]
router_req = self.new_create_request('routers', data, fmt)
if set_context and tenant_id:

View File

@ -372,7 +372,7 @@ class NetworkGatewayDbTestCase(test_db_plugin.NeutronDbPluginV2TestCase):
data[self.gw_resource]['name'] = name
for arg in arg_list or ():
# Arg must be present and not empty
if arg in kwargs and kwargs[arg]:
if kwargs.get(arg):
data[self.gw_resource][arg] = kwargs[arg]
nw_gw_req = self.new_create_request(networkgw.NETWORK_GATEWAYS,
data, fmt)

View File

@ -77,7 +77,7 @@ class NsxPluginV2TestCase(test_plugin.NeutronDbPluginV2TestCase):
for arg in (('admin_state_up', 'tenant_id', 'shared') +
(arg_list or ())):
# Arg must be present and not empty
if arg in kwargs and kwargs[arg]:
if kwargs.get(arg):
data['network'][arg] = kwargs[arg]
network_req = self.new_create_request('networks', data, fmt)
if (kwargs.get('set_context') and 'tenant_id' in kwargs):