From: Wei Wang Date: Thu, 7 Aug 2014 08:16:37 +0000 (+0800) Subject: Use "if dict.get(key):" instead "if key in dict and dict[key]:" X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=e2066f34e1187e4b021ecf6d1ec18a1e55b6eb21;p=openstack-build%2Fneutron-build.git 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 --- diff --git a/neutron/db/db_base_plugin_v2.py b/neutron/db/db_base_plugin_v2.py index d9f56a3ef..99dbe76b4 100644 --- a/neutron/db/db_base_plugin_v2.py +++ b/neutron/db/db_base_plugin_v2.py @@ -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) diff --git a/neutron/db/l3_db.py b/neutron/db/l3_db.py index 6a3d1e118..0ef0b89e5 100644 --- a/neutron/db/l3_db.py +++ b/neutron/db/l3_db.py @@ -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, diff --git a/neutron/plugins/vmware/plugins/base.py b/neutron/plugins/vmware/plugins/base.py index 1c81ff451..4ca8ff118 100644 --- a/neutron/plugins/vmware/plugins/base.py +++ b/neutron/plugins/vmware/plugins/base.py @@ -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, diff --git a/neutron/tests/unit/nuage/fake_nuageclient.py b/neutron/tests/unit/nuage/fake_nuageclient.py index 463f16438..e8099a6bb 100644 --- a/neutron/tests/unit/nuage/fake_nuageclient.py +++ b/neutron/tests/unit/nuage/fake_nuageclient.py @@ -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: diff --git a/neutron/tests/unit/test_db_plugin.py b/neutron/tests/unit/test_db_plugin.py index 3bce709d5..4fdbe6483 100644 --- a/neutron/tests/unit/test_db_plugin.py +++ b/neutron/tests/unit/test_db_plugin.py @@ -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 diff --git a/neutron/tests/unit/test_l3_plugin.py b/neutron/tests/unit/test_l3_plugin.py index 24f82fec2..33a77eae6 100644 --- a/neutron/tests/unit/test_l3_plugin.py +++ b/neutron/tests/unit/test_l3_plugin.py @@ -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: diff --git a/neutron/tests/unit/vmware/extensions/test_networkgw.py b/neutron/tests/unit/vmware/extensions/test_networkgw.py index 58ec9ce8c..5aaebbc17 100644 --- a/neutron/tests/unit/vmware/extensions/test_networkgw.py +++ b/neutron/tests/unit/vmware/extensions/test_networkgw.py @@ -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) diff --git a/neutron/tests/unit/vmware/test_nsx_plugin.py b/neutron/tests/unit/vmware/test_nsx_plugin.py index 251ec1256..4dd422ba1 100644 --- a/neutron/tests/unit/vmware/test_nsx_plugin.py +++ b/neutron/tests/unit/vmware/test_nsx_plugin.py @@ -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):